View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gworkflowdl.structure;
9   
10  import org.apache.log4j.Logger;
11  
12  /***
13   * implementation of Token with Jdom element.
14   */
15  public final class StdToken implements Token {
16  
17      static Logger logger = Logger.getLogger(StdToken.class);
18  
19      private final GenericProperties properties;
20      private final Data data;
21      private final Boolean control;
22      private Transition lock;
23      private String ID;
24      private static long idCounter = 0;
25  
26      public StdToken() {
27          this.ID = "c"+idCounter++;
28          this.control = Token.DEFAULT_CONTROL;
29          this.data = null;
30          this.properties = Factory.newProperties();
31      }
32  
33      public StdToken(boolean value) {
34          this.ID = "c"+idCounter++;
35          this.control = value;
36          this.data = null;
37          this.properties = Factory.newProperties();
38      }
39  
40      public StdToken(boolean value, GenericProperties properties) {
41          this.ID = "c"+idCounter++;
42          this.control = value;
43          this.data = null;
44          this.properties = properties;
45      }
46  
47      public StdToken(String id, boolean value, GenericProperties properties) {
48          if (id==null) {
49              this.ID = "c"+idCounter++;
50          } else {
51              updateIdCounter(id);
52              this.ID = id;
53          }
54          this.control = value;
55          this.data = null;
56          this.properties = properties;
57      }
58  
59      /***
60       * Increase idCounter if identifier contains index that is greater or equal current counter.
61       * @param id token identifier string.
62       */
63      private synchronized void updateIdCounter(String id) {
64          if (id.length()>1) {
65              try {
66                  long counter = Long.parseLong(id.substring(1));
67                  if (counter >= idCounter) idCounter = counter + 1;
68              } catch(NumberFormatException e) {
69                  // do nothing. Format of id is not default format.
70              }
71          }
72      }
73  
74      public StdToken(Data data) {
75          this.ID = "d"+idCounter++;
76          this.data = data;
77          this.control = null;
78          this.properties = Factory.newProperties();
79      }
80  
81      public StdToken(Data data, GenericProperties properties) {
82          this.ID = "d"+idCounter++;
83          this.data = data;
84          this.control = null;
85          this.properties = properties;
86      }
87  
88      public StdToken(String id, Data data, GenericProperties properties) {
89          if (id==null) {
90              this.ID = "d"+idCounter++;
91          } else {
92              updateIdCounter(id);
93              this.ID = id;
94          }
95          this.data = data;
96          this.control = null;
97          this.properties = properties;
98      }
99  
100     public String getID() {
101         return ID;
102     }
103 
104     public void setID(final String id) {
105         updateIdCounter(id);
106         this.ID = id;
107     }
108 
109     public GenericProperties getProperties() {
110         return properties;
111     }
112 
113     public Data getData() {
114         return data;
115     }
116 
117     public Boolean getControl() {
118         return control;
119     }
120 
121     /***
122      * This equal() method only compares the data or control value and not the token properties.
123      * @param o The token to compare.
124      * @return <code>true</code> if both tokens contain the same data or control values, <code>false</code> otherwise.
125      */
126     public boolean equals(Object o) {
127         if (o instanceof Token) {
128             Token t = (Token) o;
129             if (control != null && t.getControl() != null) {
130                 return (control.equals(t.getControl()));
131             } else return data != null && t.getData() != null && data.equals(t.getData());
132         }
133         else return false;
134     }
135 
136     /***
137      * Cloned token will have same identifier as original token!
138      * @return cloned token.
139      */
140     public Object clone() {
141         Token ret = null;
142 
143         // clone properties
144         GenericProperties _properties = null;
145         if (properties != null) {
146             _properties = properties.deepCopy();
147         }
148 
149         // clone control or data
150         if (control != null) {
151             ret = Factory.newToken(ID,control,_properties);
152         } else if (data != null) {
153             ret = Factory.newToken(ID,(Data) data.clone(),_properties);
154         }
155         return ret;
156     }
157 
158     public void lock(Transition transition) {
159         lock = transition;
160     }
161 
162     public void unlock() {
163         lock = null;
164     }
165 
166     public boolean isLocked() {
167         return (lock != null);
168     }
169 
170     public boolean isLockedBy(Transition transition) {
171         return (lock == transition);
172     }
173     
174 }