KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > cache > CacheProperty


1 package org.sapia.regis.cache;
2
3 import org.sapia.regis.Property;
4 import org.sapia.regis.impl.PropertyImpl;
5 import org.sapia.regis.type.BuiltinTypes;
6 import org.sapia.regis.type.Value;
7
8 public class CacheProperty extends PropertyImpl{
9   
10   static final long serialVersionUID = 1L;
11   
12   private static final int BOOLEAN = 0;
13   private static final int INT = 1;
14   private static final int LONG = 2;
15   private static final int FLOAT = 3;
16   private static final int DOUBLE = 4;
17
18   private transient CacheNode node;
19   private long lastModified;
20   
21   private Value[] values = new Value[]{
22     Value.newInstance(BuiltinTypes.BOOLEAN_TYPE),
23     Value.newInstance(BuiltinTypes.INT_TYPE),
24     Value.newInstance(BuiltinTypes.LONG_TYPE),
25     Value.newInstance(BuiltinTypes.FLOAT_TYPE),
26     Value.newInstance(BuiltinTypes.DOUBLE_TYPE)
27   };
28   
29   CacheProperty(String JavaDoc name, String JavaDoc value, CacheNode node){
30     super(name, value);
31     this.node = node;
32     this.lastModified = node.lastModifChecksum();
33   }
34   
35   public String JavaDoc getValue() {
36     refresh();
37     super.checkNull();
38     return super.getValue();
39   }
40   
41   public boolean asBoolean() {
42     refresh();
43     super.checkNull();
44     return ((Boolean JavaDoc)doGetValue(BOOLEAN)).booleanValue();
45   }
46   
47   public int asInt() {
48     refresh();
49     super.checkNull();
50     return ((Integer JavaDoc)doGetValue(INT)).intValue();
51   }
52   
53   public long asLong() {
54     refresh();
55     super.checkNull();
56     return ((Long JavaDoc)doGetValue(LONG)).longValue();
57   }
58   
59   public float asFloat() {
60     refresh();
61     super.checkNull();
62     return ((Float JavaDoc)doGetValue(FLOAT)).floatValue();
63   }
64   
65   public double asDouble() {
66     refresh();
67     super.checkNull();
68     return ((Double JavaDoc)doGetValue(DOUBLE)).floatValue();
69   }
70   
71   public String JavaDoc asString() {
72     refresh();
73     return super.asString();
74   }
75   
76   private Object JavaDoc doGetValue(int index){
77     Object JavaDoc value = values[index].get();
78     if(value == null){
79       String JavaDoc thisValue = _value;
80       Value v = values[index];
81       values[index].set(v.getType().parse(thisValue));
82       value = values[index].get();
83     }
84     return value;
85   }
86   
87   private void refresh(){
88     if(node == null){
89       return;
90     }
91     long checkSum = node.lastModifChecksum();
92     if(lastModified != checkSum){
93       synchronized(this){
94         if(lastModified != checkSum){
95           Property prop = node.getProperty(getKey());
96           super._value = prop.getValue();
97           for(int i = 0; i < values.length; i++){
98             values[i].nullify();
99           }
100           lastModified = checkSum;
101         }
102       }
103     }
104   }
105
106 }
107
Popular Tags