View Javadoc

1   /*
2    * AbstractPropertyOperation.java
3    * Created on 21 Май 2006 г., 21:32
4    */
5   
6   package org.weda.property.impl;
7   
8   import java.beans.IntrospectionException;
9   import java.beans.PropertyDescriptor;
10  import java.lang.reflect.Method;
11  import java.util.ArrayList;
12  import java.util.List;
13  import org.apache.commons.lang.ObjectUtils;
14  import org.apache.commons.lang.StringUtils;
15  import org.weda.property.PropertyOperation;
16  
17  /**
18   *
19   * @author Mikhail Titov
20   */
21  public abstract class AbstractPropertyOperation<L> implements PropertyOperation<L> {
22      protected String propertyName;
23      protected Method method; 
24      protected List<L> listeners;
25      
26      public AbstractPropertyOperation(
27              Class propOwnerClass, String propertyName, boolean isGetter)
28          throws IntrospectionException
29      {
30          String methodName = 
31                  (isGetter?"get":"set")+StringUtils.capitalize(propertyName);
32          if (isGetter){
33              PropertyDescriptor desc = new PropertyDescriptor(
34                      propertyName, propOwnerClass, methodName, null);
35              method = desc.getReadMethod();
36          }else{
37              PropertyDescriptor desc = new PropertyDescriptor(
38                      propertyName, propOwnerClass, null, methodName);
39              method = desc.getWriteMethod();
40          }
41          this.propertyName = propertyName;
42      }
43          
44      public void addListener(L listener){
45          if (listeners==null)
46              listeners = new ArrayList<L>(1);
47          listeners.add(listener);
48      }
49      
50      public void removeListener(L listener){
51          if (listeners!=null)
52              listeners.remove(listener);
53      }    
54      
55      public Method getMethod(){
56          return method;
57      }
58      
59      public boolean equals(Object o){
60          if (o instanceof PropertyOperation){
61              return ObjectUtils.equals(
62                      method, ((PropertyOperation)o).getMethod());
63          }else
64              return false;
65      }
66      
67      public int hashCode() {
68          return method.hashCode();
69      }
70      
71  }