View Javadoc

1   package org.lsst.ccs.config;
2   
3   import javax.persistence.MappedSuperclass;
4   import java.io.Serializable;
5   import java.util.List;
6   
7   /**
8    * abstract class for all parameter configurations
9    * @author bamade
10   */
11  // Date: 11/04/12
12  
13      @MappedSuperclass
14  public abstract class ParameterConfiguration  implements Serializable, PathObject{
15      //TODO : a list of ValueEvent object start, (next?)
16      protected String value ;
17  
18  
19      ///////////////////////// CONSTRUCTORS
20  
21      protected ParameterConfiguration() {
22  
23      }
24      protected ParameterConfiguration(String value) {
25          this.value = value;
26      }
27  
28  
29      /////////////////////////// ACCESSORS/MUTATORS
30  
31      public abstract long getId() ;
32      protected abstract void setId(long id) ;
33  
34      public boolean isReadOnly() {
35          return getId() != 0L ;
36      }
37  
38      public abstract ParameterDescription getDescription() ;
39  
40      public abstract List<? extends ValueEvent> getValueEvents() ;
41  
42      // public abstract void addValueEvent(ValueEvent event) ;
43  
44      public String getValue() {
45          return value;
46      }
47      void setValue(String value) {
48          /* if( isReadOnly()) {
49              throw new ImmutableStateException("read only value") ;
50          } */
51          this.value = value;
52      }
53  
54      public Object checkValue(String value) throws IllegalArgumentException{
55          ParameterDescription description = this.getDescription();
56          return description.checkValue(value) ;
57          /*
58          return Constraints.check(description.getParameterBase().getTypeName(),
59                  value, description.getConstraints()) ;
60          */
61      }
62  
63      /**
64       * the value is modifiable only on objects not yet registered in the database
65       * @param value
66       * @throws ImmutableStateException if modification is for a registered object
67       * @throws IllegalArgumentException if value is not coherent with type and constraints
68       */
69      public void modifyValue(String value) {
70          if( isReadOnly()) {
71              throw new ImmutableStateException("read only value") ;
72          }
73          if(checkValue(value) != null) {
74              setValue(value);
75          }
76      }
77  
78      /**
79       * method for two phases commit: call only if check is ok.
80       * @param value
81       */
82      public void modifyValueAfterCheck(String value) {
83          if( isReadOnly()) {
84              throw new ImmutableStateException("read only value") ;
85          }
86          setValue(value);
87      }
88      ///////////////////////////// IDENT METHODS
89      // TODO: add id?
90  
91      @Override
92      public boolean equals(Object o) {
93          if (this == o) return true;
94          if (!(o instanceof PathObject)) return false;
95  
96          PathObject that = (PathObject) o;
97          ParameterDescription description = getDescription();
98          if (!description.getPath().equals(that.getPath())) return false;
99  
100         return true;
101     }
102 
103     @Override
104     public String toString() {
105         return  this.getId() + "{" + getId() +
106                 ": description=" + getDescription() +
107                 ", value='" + value + '\'' +
108                 '}';
109     }
110 
111     @Override
112     public int hashCode() {
113         return getDescription().hashCode();
114     }
115 
116     @Override
117     public ParameterPath getPath() {
118         return getDescription().getPath();
119     }
120 }