1 18 19 package sync4j.framework.engine; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 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 48 public class SyncItemImpl implements java.io.Serializable , SyncItem { 49 50 52 55 protected SyncItemKey key = null; 56 public SyncItemKey getKey() { 57 return this.key; 58 } 59 60 63 protected SyncItemKey mappedKey = null; 64 public SyncItemKey getMappedKey() { 65 return mappedKey; 66 } 67 68 75 public boolean isMapped() { 76 return (mappedKey != null); 77 } 78 79 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 97 protected HashMap properties = new HashMap (); 98 99 105 public Map getProperties() { 106 return (Map )this.properties.clone(); 107 } 108 109 117 public void setProperties(Map properties){ 118 this.properties.clear(); 119 120 Object value = null; 121 String name = null; 122 123 Iterator i = properties.keySet().iterator(); 124 while(i.hasNext()) { 125 name = (String )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 139 public void setProperty(SyncProperty property) { 140 properties.put(property.getName(), property); 141 } 142 143 149 public SyncProperty getProperty(String propertyName) { 150 return (SyncProperty)properties.get(propertyName); 151 } 152 153 156 protected SyncSource syncSource = null; 157 158 162 public SyncSource getSyncSource() { 163 return syncSource; 164 } 165 166 170 public void setSyncSource(SyncSource syncSource) { 171 if (syncSource == null) { 172 throw new NullPointerException ("syncSource cannot be null"); 173 } 174 175 this.syncSource = syncSource; 176 } 177 178 180 protected SyncItemImpl() {} 181 182 public SyncItemImpl(SyncSource syncSource, Object key) { 183 this(syncSource, key, SyncItemState.UNKNOWN); 184 } 185 186 194 public SyncItemImpl(SyncSource syncSource, Object key, char state) { 195 this(syncSource, key, null, state); 196 } 197 198 208 public SyncItemImpl(SyncSource syncSource, Object key, Object 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 220 221 226 public void setPropertyValue(String propertyName, Object propertyValue) { 227 SyncProperty property = (SyncProperty)properties.get(propertyName); 228 229 if (property != null) { 230 property.setValue(propertyValue); 231 } 232 } 233 234 241 public Object getPropertyValue(String propertyName) { 242 SyncProperty property = (SyncProperty)properties.get(propertyName); 243 244 return (property == null) ? null 245 : property.getValue() 246 ; 247 } 248 249 257 public boolean equals(Object o) { 258 if (!(o instanceof SyncItem)) return false; 259 260 return ((SyncItem)o).getKey().equals(key); 261 } 262 263 268 public int hashCode() { 269 return getKey().hashCode(); 270 } 271 272 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 290 public String 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 |