KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > engine > SyncItemImpl


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.engine;
20
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import sync4j.framework.engine.source.SyncSource;
26 import sync4j.framework.engine.SyncItem;
27 import sync4j.framework.engine.SyncItemKey;
28 import sync4j.framework.engine.SyncItemState;
29
30 import org.apache.commons.lang.builder.ToStringBuilder;
31
32
33 /**
34  * <i>SyncItem</i> is the indivisible entity that can be exchanged in a
35  * synchronization process. It is similar to an Item, but it doesn't contain
36  * any data, only status and addressing information. The idea is that a
37  * <i>SyncItem</i> represents status information about an item. Only if an item
38  * must be synchronized it needs also the real data.
39  * <p>
40  * The <i>SyncItemKey</i> uniquely identifies the item into the server. Client
41  * keys must be translated into server keys before create a <i>SyncItem</i>.
42  *
43  * @author Stefano Fornari @ Funambol
44  *
45  * @version $Id: SyncItemImpl.java,v 1.7 2005/03/02 20:57:37 harrie Exp $
46  *
47  */

48 public class SyncItemImpl implements java.io.Serializable JavaDoc, SyncItem {
49
50     // -------------------------------------------------------------- Properties
51

52     /**
53      * The SyncItem's unique identifier - read only
54      */

55     protected SyncItemKey key = null;
56     public SyncItemKey getKey() {
57         return this.key;
58     }
59     
60     /**
61      * The SyncItem's mapped identifier - read/write
62      */

63     protected SyncItemKey mappedKey = null;
64     public SyncItemKey getMappedKey() {
65         return mappedKey;
66     }
67     
68     /**
69      * A <i>SyncItem</i> is mapped if mappedKey is not null.
70      *
71      * @return <i>true</i> if the item is mapped to another source's item,
72      * <i>false</i> otherwise
73      *
74      */

75     public boolean isMapped() {
76         return (mappedKey != null);
77     }
78     
79     /**
80      * The state of this <i>SyncItem</i>
81      *
82      * @see sync4j.framework.engine.SyncItemState
83      */

84     protected char state = SyncItemState.UNKNOWN;
85     
86     public char getState(){
87         return state;
88     }
89
90     public void setState(char state){
91         this.state = state;
92     }
93
94     /**
95      * The <i>SyncItem</i>'s properties - read and write
96      */

97     protected HashMap JavaDoc properties = new HashMap JavaDoc();
98     
99     /**
100      * Returns the <i>properties</i> property. A cloned copy of the internal map
101      * is returned.
102      *
103      * @return the <i>properties</i> property.
104      */

105     public Map JavaDoc getProperties() {
106         return (Map JavaDoc)this.properties.clone();
107     }
108
109     /**
110      * Sets the <i>properties</i> property. All items in the given map are added
111      * to the internal map. <i>propertis</i> can contain String values or
112      * <i>SyncPorperty</i> values. In the former case, <i>new SyncProperty<i/>s are
113      * created.
114      *
115      * @param properties the new values
116      */

117     public void setProperties(Map JavaDoc properties){
118         this.properties.clear();
119         
120         Object JavaDoc value = null;
121         String JavaDoc name = null;
122         
123         Iterator JavaDoc i = properties.keySet().iterator();
124         while(i.hasNext()) {
125             name = (String JavaDoc)i.next();
126             value = properties.get(name);
127             
128             if (!(value instanceof SyncProperty)) {
129                 value = new SyncProperty(name, value.toString());
130             }
131             this.properties.put(name, value);
132         }
133     }
134
135     /** Sets/adds the given property to this <i>SyncItem</i>
136      *
137      * @param property The property to set/add
138      */

139     public void setProperty(SyncProperty property) {
140         properties.put(property.getName(), property);
141     }
142
143     /** Returns the property with the given name
144      *
145      * @param propertyName The property name
146      *
147      * @return the property with the given name if exists or null if not
148      */

149     public SyncProperty getProperty(String JavaDoc propertyName) {
150         return (SyncProperty)properties.get(propertyName);
151     }
152     
153         /**
154      * The SyncSource this item belongs to
155      */

156     protected SyncSource syncSource = null;
157     
158     /** Getter for property syncSource.
159      * @return Value of property syncSource.
160      *
161      */

162     public SyncSource getSyncSource() {
163         return syncSource;
164     }
165     
166     /** Setter for property syncSource.
167      * @param syncSource New value of property syncSource. NOT NULL
168      *
169      */

170     public void setSyncSource(SyncSource syncSource) {
171         if (syncSource == null) {
172             throw new NullPointerException JavaDoc("syncSource cannot be null");
173         }
174         
175         this.syncSource = syncSource;
176     }
177     
178     // ------------------------------------------------------------ Constructors
179

180     protected SyncItemImpl() {}
181     
182     public SyncItemImpl(SyncSource syncSource, Object JavaDoc key) {
183         this(syncSource, key, SyncItemState.UNKNOWN);
184     }
185     
186     /**
187      * Creates a new <i>SyncItem</i> belonging to the given source. After
188      * creating a new item, usually <i>setProperties()</i> should be invoked.
189      *
190      * @param syncSource the source this item belongs to
191      * @param key the item identifier
192      * @param state one of the state value defined in <i>SyncItemState</i>
193      */

194     public SyncItemImpl(SyncSource syncSource, Object JavaDoc key, char state) {
195         this(syncSource, key, null, state);
196     }
197     
198     /**
199      * Creates a new <i>SyncItem</i> belonging to the given source but mapped to
200      * another source. After creating a new item, usually <i>setProperties()</i>
201      * should be invoked.
202      *
203      * @param syncSource the source this item belongs to
204      * @param key the item identifier
205      * @param mappedKey the mapped key
206      * @param state one of the state value defined in <i>SyncItemState</i>
207      */

208     public SyncItemImpl(SyncSource syncSource, Object JavaDoc key, Object JavaDoc mappedKey, char state) {
209         this.syncSource = syncSource ;
210         this.key = new SyncItemKey(key);
211         this.state = state ;
212         
213         this.mappedKey = (mappedKey != null)
214                        ? new SyncItemKey(mappedKey)
215                        : null;
216     }
217     
218     // ---------------------------------------------------------- Public methods
219

220     
221     /** Sets the value of the property with the given name.
222      *
223      * @param propertyName The property's name
224      * @param propertyValue The new value
225      */

226     public void setPropertyValue(String JavaDoc propertyName, Object JavaDoc propertyValue) {
227         SyncProperty property = (SyncProperty)properties.get(propertyName);
228         
229         if (property != null) {
230             property.setValue(propertyValue);
231         }
232     }
233
234     /** Returns the value of the property with the given name.
235      *
236      * @param propertyName The property's name
237      *
238      * @return the property value if this <i>SyncItem</i> has the given
239      * property or null otherwise.
240      */

241     public Object JavaDoc getPropertyValue(String JavaDoc propertyName) {
242         SyncProperty property = (SyncProperty)properties.get(propertyName);
243     
244         return (property == null) ? null
245                                   : property.getValue()
246                                   ;
247     }
248     
249     /**
250      * Two <i>SyncItem</i>s are equal if their keys are equal.
251      *
252      * @param o the object this instance must be compared to.
253      *
254      * @return the given object is equal to this object
255      *
256      */

257     public boolean equals(Object JavaDoc o) {
258         if (!(o instanceof SyncItem)) return false;
259         
260         return ((SyncItem)o).getKey().equals(key);
261     }
262     
263     /**
264      * Returns the hash code of this object
265      *
266      * @return the hash code of this object
267      */

268     public int hashCode() {
269         return getKey().hashCode();
270     }
271     
272     /**
273      * Creates and returns a "not-existing" <i>SyncItem</i>. It is used internally to
274      * represent an item which has not a physical correspondance in a source.
275      *
276      * @param syncSource the <i>SyncSource</i> the not existing item belongs to
277      * @return the a "not-exisiting" <i>SyncItem</i>
278      */

279     public static SyncItem getNotExistingSyncItem(SyncSource syncSource) {
280         SyncItem notExisting = new SyncItemImpl(syncSource, "NOT_EXISTING");
281         
282         notExisting.setState(SyncItemState.NOT_EXISTING);
283         
284         return notExisting;
285     }
286     
287     /**
288      * @return a string representation of this SyncItem for debugging purposes
289      */

290     public String JavaDoc toString() {
291         return new ToStringBuilder(this).
292                    append("key" , key.toString() ).
293                    append("state" , state ).
294                    append("properties", properties.toString()).
295                    toString();
296     }
297 }
Popular Tags