GWT Autobean set initial value of created interface

646 Views Asked by At

We are using AutoBeans to create our Pojo objects for use in RPC-Calls. What is the recommended approach for the Pojo to have a default value or other class initialization?

For example

 public interface SamplePojo {
        // should default to 5
        int getSampleProperty();
        void setSampleProperty(int sampleProperty);
    }


    public interface ModelFactory extends AutoBeanFactory {
        AutoBean<SamplePojo> getSamplePojo();   
    }

And SamplePojo has a int property that we always want to default to 5.

2

There are 2 best solutions below

2
On BEST ANSWER

AutoBeans should be seen as low-level, mapping straight to/from JSON. With that in mind, you don't want getSampleProperty() to be 5, you rather want to detect the absence of specific value for the property and use 5 in that case.

So, if 0 (the default value of an int) is not an acceptable value for the property, then simply "use 5 if the property is 0". Otherwise, change the return type to Integer and "use 5 if the property is null".

0
On

Would this work?

public interface SamplePojo {
        // should default to 5
        int getSampleProperty();
        void setSampleProperty(int sampleProperty);
    }

public class SamplePojoImpl implements SamplePojo{
    private int sampleProperty = 5
    // getters/setters
    int getSampleProperty(){ return sampleProperty;}
    void setSampleProperty(int sampleProperty){this.sampleProperty = sampleProperty;}

}

public interface ModelFactory extends AutoBeanFactory {
    AutoBean<SamplePojo> getSamplePojo(SamplePojoImpl samplePojo );   
}