Skip to content

Commit

Permalink
Removed redundant code from ChemObj clone - the existing code did exa…
Browse files Browse the repository at this point in the history
…ctly what the copy constructor of HashMap does and thus provides a cleaner implementation

Change-Id: I3815bd122d8e1dd943076da434dcda4f40bee35c
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Nov 2, 2012
1 parent d1e8fa6 commit 8f7c0ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
19 changes: 8 additions & 11 deletions src/main/org/openscience/cdk/ChemObject.java
Expand Up @@ -300,20 +300,17 @@ public Map<Object,Object> getProperties()
public Object clone() throws CloneNotSupportedException
{
ChemObject clone = (ChemObject)super.clone();

// clone the flags
clone.flags = new boolean[CDKConstants.MAX_FLAG_INDEX + 1];
System.arraycopy(flags, 0, clone.flags, 0, flags.length);
// clone the properties
if (properties != null) {
Map<Object, Object> clonedHashtable = new HashMap<Object, Object>();
Iterator<Object> keys = properties.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
Object value = properties.get(key);
clonedHashtable.put(key, value);
}
clone.properties = clonedHashtable;
}

// clone the properties - using the HashMap copy constructor
// this doesn't deep clone the keys/values but this wasn't happening
// already
if (properties != null)
clone.properties = new HashMap<Object, Object>(getProperties());

// delete all listeners
clone.chemObjectListeners = null;
return clone;
Expand Down
15 changes: 6 additions & 9 deletions src/main/org/openscience/cdk/silent/ChemObject.java
Expand Up @@ -237,17 +237,14 @@ public Object clone() throws CloneNotSupportedException
// clone the flags
clone.flags = new boolean[CDKConstants.MAX_FLAG_INDEX + 1];
System.arraycopy(flags, 0, clone.flags, 0, flags.length);
// clone the properties

// clone the properties - using the HashMap copy constructor
// this does not deep copy all objects but this was not done
// originally
if (properties != null) {
Map<Object, Object> clonedHashtable = new HashMap<Object, Object>();
Iterator<Object> keys = properties.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
Object value = properties.get(key);
clonedHashtable.put(key, value);
}
clone.properties = clonedHashtable;
clone.properties = new HashMap<Object, Object>(getProperties());
}

return clone;
}

Expand Down

0 comments on commit 8f7c0ab

Please sign in to comment.