Permalink
Browse files

Removed redundant code from ChemObj clone - the existing code did exa…

…ctly what the copy constructor of HashMap does and thus provides a cleaner implementation
  • Loading branch information...
1 parent 7fe738c commit 83f0f891eee875ea628bde799bb7138decfe872b @johnmay committed Oct 31, 2012
Showing with 14 additions and 20 deletions.
  1. +8 −11 src/main/org/openscience/cdk/ChemObject.java
  2. +6 −9 src/main/org/openscience/cdk/silent/ChemObject.java
View
19 src/main/org/openscience/cdk/ChemObject.java
@@ -300,20 +300,17 @@ public Object getProperty(Object description)
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;
View
15 src/main/org/openscience/cdk/silent/ChemObject.java
@@ -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;
}

0 comments on commit 83f0f89

Please sign in to comment.