On 06/10/2010 09:27 AM, Scott Carey wrote:
I propose:
* We use getters/setters, and allow setters and constructors to be
package protected with an option so the objects can be safely passed
around outside the package without stateful wrapper objects. Users
can write factory methods or other static helper methods to abstract
away as much as they would like from there.
* Setters should not allow invalid data to be set.
I'd be okay with these changes. +1
* Provide some built-in mechanism to resolve unions on a read and
verify them on a write. Perhaps a inner static enum for each union,
and a getter for each branch.
So a schema like:
{"type":"record", "name":"Foo", "fields":[
{"name":"bar", "type":["int", "float"]}]}
might generate something like:
public class Foo {
public enum BarType { INT, FLOAT }
private BarType bar$Type;
private Object bar;
public void setBar(int value) { bar = value; bar$Type = INT; }
public void setBar(float value) { bar = value; bar$Type = FLOAT; }
public int getBar$Int() {
if (bar$Type != INT) throw new ...;
return (int)bar;
}
public float getBar$Float() {
if (bar$Type != FLOAT) throw new ...;
return (float)bar;
}
}
Is that the sort of thing you have in mind? I've added dollar signs to
avoid potential conflicts, e.g., if the user had a field named 'barType'
or 'barInt'. I think its best to always add the dollars, since
otherwise it could get awkward if a field named 'barInt' were added later.
* Unions with one type and NULL should not translate to Object, but
rather be that type and allow null.
That's already the case, no?
Also, you didn't provide a proposal for constructors, yet, right?
Perhaps all of the setters could be on a generated builder class, with
an instance only returned when it's completely populated?
Doug