Don't like ads? PRO users don't see any ads ;-)
Guest

JavaBeans alternatives

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 2.61 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Customer {
  2.     public Property<String> name = new Property();
  3. }
  4.        
  5. public class Property<T> {
  6.     public final String name;
  7.     T value;
  8.     private final PropertyChangeSupport support;
  9.  
  10.     public static <T> Property<T> newInstance(String name, T value,
  11.                                               PropertyChangeSupport support) {
  12.         return new Property<T>(name, value, support);
  13.     }
  14.  
  15.     public static <T> Property<T> newInstance(String name, T value) {
  16.         return newInstance(name, value, null);
  17.     }
  18.  
  19.     public Property(String name, T value, PropertyChangeSupport support) {
  20.         this.name = name;
  21.         this.value = value;
  22.         this.support = support;
  23.     }
  24.  
  25.     public T getValue() { return value; }
  26.  
  27.     public void setValue(T value) {
  28.         T old = this.value;
  29.         this.value = value;
  30.         if(support != null)
  31.             support.firePropertyChange(name, old, this.value);
  32.     }
  33.  
  34.     public String toString() { return value.toString(); }
  35. }
  36.        
  37. public class Customer {
  38.     private final PropertyChangeSupport support = new PropertyChangeSupport();
  39.  
  40.     public final Property<String> name = Property.newInstance("name", "", support);
  41.     public final Property<Integer> age = Property.newInstance("age", 0, support);
  42.  
  43.     ... declare add/remove listenener ...
  44. }
  45.  
  46.  
  47. Customer c = new Customer();
  48. c.name.setValue("Hyrum");
  49. c.age.setValue(49);
  50. System.out.println("%s : %s", c.name, c.age);
  51.        
  52. @Bean(
  53.   properties={
  54.     @Property(name="name"),
  55.     @Property(name="phone", bound=true),
  56.     @Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
  57.   }
  58. )
  59. public class Person extends PersonGen {}
  60.        
  61. interface IListenable {
  62.     void addPropertyChangeListener( PropertyChangeListener listener );
  63.     void removePropertyChangeListener( PropertyChangeListener listener );
  64. }
  65.  
  66. abstract class MyBean extends IListenable {
  67.     public abstract void setName(String name);
  68.     public abstract String getName();
  69.  
  70.     // more things
  71. }
  72.  
  73. public class JavaBeanFactory {
  74.  
  75.    public <T> Class<T> generate(Class<T> clazz) {
  76.       // I used here CGLIB to generate dynamically a class that implements the methods:
  77.       // getters
  78.       // setters
  79.       // addPropertyChangeListener
  80.       // removePropertyChangeListener
  81.    }
  82. }
  83.        
  84. public class Foo {
  85.     @Inject
  86.     public Provider<MyBean> myBeanProvider;
  87.  
  88.     public MyBean createHook(MyBean a) {
  89.         final MyBean b  = myBeanProvider.get();
  90.         a.addPropertyChangeListener(new PropertyChangeListener() {
  91.              public void propertyChange(PropertyChangeEvent evt) {
  92.                  b.setName((String) evt.getNewValue());
  93.              }
  94.         });
  95.         return b;
  96.     }
  97. }