KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spds > 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.syncclient.spds.engine;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 import sync4j.syncclient.spds.engine.*;
25
26 /**
27  * <i>SyncItemImpl</i> is a basic implemenation of a <i>SyncItem</i>.
28  *
29  * @author Fabio Maggi
30  *
31  * @version $Id: SyncItemImpl.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
32  */

33 public class SyncItemImpl implements SyncItem {
34
35     // -------------------------------------------------------------- Properties
36

37     /**
38      * The SyncItem's uique identifier - read only
39      */

40     protected SyncItemKey key = null;
41     public SyncItemKey getKey() {
42         return this.key;
43     }
44
45     /**
46      * The state of this <i>SyncItem</i>
47      *
48      */

49     protected char state = SyncItemState.UNKNOWN;
50
51     public char getState(){
52         return state;
53     }
54
55     public void setState(char state){
56         this.state = state;
57     }
58
59     /**
60      * The <i>SyncItem</i>'s properties - read and write
61      */

62     protected Hashtable JavaDoc properties = new Hashtable JavaDoc();
63
64     /**
65      * Returns the <i>properties</i> property. A cloned copy of the internal map
66      * is returned.
67      *
68      * @return the <i>properties</i> property.
69      */

70     public Hashtable JavaDoc getProperties() {
71         return this.properties;
72     }
73
74     /**
75      * Sets the <i>properties</i> property. All items in the given map are added
76      * to the internal map.
77      *
78      * @param properties the new values
79      */

80     public void setProperties(Hashtable JavaDoc properties){
81         this.properties.clear();
82
83         String JavaDoc name = null;
84
85         Enumeration JavaDoc i = properties.keys();
86         while(i.hasMoreElements()) {
87             name = (String JavaDoc)i.nextElement();
88             this.properties.put(
89                 name,
90                 new SyncItemProperty(name, properties.get(name))
91             );
92         }
93     }
94
95     /** Sets/adds the given property to this <i>SyncItem</i>
96      *
97      * @param property The property to set/add
98      */

99     public void setProperty(SyncItemProperty property) {
100         properties.put(property.getName(), property);
101     }
102
103     /** Returns the property with the given name
104      *
105      * @param propertyName The property name
106      *
107      * @return the property with the given name if exists or null if not
108      */

109     public SyncItemProperty getProperty(String JavaDoc propertyName) {
110         return (SyncItemProperty)properties.get(propertyName);
111     }
112
113         /**
114      * The SyncSource this item belongs to
115      */

116     protected SyncSource syncSource = null;
117
118     /** Getter for property syncSource.
119      * @return Value of property syncSource.
120      *
121      */

122     public SyncSource getSyncSource() {
123         return syncSource;
124     }
125
126     /** Setter for property syncSource.
127      * @param syncSource New value of property syncSource. NOT NULL
128      *
129      */

130     public void setSyncSource(SyncSource syncSource) {
131         if (syncSource == null) {
132             throw new NullPointerException JavaDoc("syncSource cannot be null");
133         }
134
135         this.syncSource = syncSource;
136     }
137
138     // ------------------------------------------------------------ Constructors
139

140     public SyncItemImpl(SyncSource syncSource, Object JavaDoc key) {
141         this(syncSource, key, SyncItemState.UNKNOWN);
142     }
143
144     /**
145      * Creates a new <i>SyncItem</i> belonging to the given source. After
146      * creating a new item, usually <i>setProperties()</i> should be invoked.
147      *
148      * @param syncSource the source this item belongs to
149      * @param key the item identifier
150      * @param state one of the state value defined in <i>SyncItemState</i>
151      */

152     public SyncItemImpl(SyncSource syncSource, Object JavaDoc key, char state) {
153         this.syncSource = syncSource ;
154         this.key = new SyncItemKey(key);
155         this.state = state ;
156     }
157
158     // ---------------------------------------------------------- Public methods
159

160
161     /** Sets the value of the property with the given name.
162      *
163      * @param propertyName The property's name
164      * @param propertyValue The new value
165      */

166     public void setPropertyValue(String JavaDoc propertyName, String JavaDoc propertyValue) {
167         SyncItemProperty property = (SyncItemProperty)properties.get(propertyName);
168
169         if (property != null) {
170             property.setValue(propertyValue);
171         }
172     }
173
174     /** Returns the value of the property with the given name.
175      *
176      * @param propertyName The property's name
177      *
178      * @return the property value if this <i>SyncItem</i> has the given
179      * property or null otherwise.
180      */

181     public Object JavaDoc getPropertyValue(String JavaDoc propertyName) {
182         SyncItemProperty property = (SyncItemProperty)properties.get(propertyName);
183
184         return (property == null) ? null
185                                   : property.getValue()
186                                   ;
187     }
188
189     /**
190      * Two <i>SyncItem</i>s are equal if their keys are equal.
191      *
192      * @param o the object this instance must be compared to.
193      *
194      * @return the given object is equal to this object
195      *
196      */

197     public boolean equals(Object JavaDoc o) {
198         if (!(o instanceof SyncItem)) return false;
199
200         return ((SyncItem)o).getKey().equals(key);
201     }
202
203     /**
204      * Creates and returns a "not-existing" <i>SyncItem</i>. It is used internally to
205      * represent an item which has not a physical correspondance in a source.
206      *
207      * @param syncSource the <i>SyncSource</i> the not existing item belongs to
208      * @return the a "not-exisiting" <i>SyncItem</i>
209      */

210     public static SyncItem getNotExistingSyncItem(SyncSource syncSource) {
211         SyncItem notExisting = new SyncItemImpl(syncSource, "NOT_EXISTING");
212
213         notExisting.setState(SyncItemState.NOT_EXISTING);
214
215         return notExisting;
216     }
217 }
Popular Tags