Merge branch 'develop' into task/JAL-2196pdbeProperties
[jalview.git] / src / jalview / datamodel / PDBEntry.java
index 1403595..6a6ccd0 100755 (executable)
@@ -22,10 +22,20 @@ package jalview.datamodel;
 
 import jalview.util.CaseInsensitiveString;
 
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.Hashtable;
 
 public class PDBEntry
 {
+
+  /**
+   * constant for storing chain code in properties table
+   */
+  private static final String CHAIN_ID = "chain_code";
+
+  private Hashtable<String, Object> properties;
+
   private static final int PDB_ID_LENGTH = 4;
 
   private String file;
@@ -67,12 +77,6 @@ public class PDBEntry
     }
   }
 
-  /**
-   * constant for storing chain code in properties table
-   */
-  private static final String CHAIN_ID = "chain_code";
-
-  Hashtable properties;
 
   /**
    * Answers true if obj is a PDBEntry with the same id and chain code (both
@@ -137,13 +141,13 @@ public class PDBEntry
   /**
    * @param pdbId
    * @param chain
-   * @param type
+   * @param entryType
    * @param filePath
    */
-  void init(String pdbId, String chain, PDBEntry.Type type, String filePath)
+  void init(String pdbId, String chain, PDBEntry.Type entryType, String filePath)
   {
     this.id = pdbId;
-    this.type = type == null ? null : type.toString();
+    this.type = entryType == null ? null : entryType.toString();
     this.file = filePath;
     setChainCode(chain);
   }
@@ -160,7 +164,7 @@ public class PDBEntry
     id = entry.id;
     if (entry.properties != null)
     {
-      properties = (Hashtable) entry.properties.clone();
+      properties = (Hashtable<String, Object>) entry.properties.clone();
     }
   }
 
@@ -193,9 +197,9 @@ public class PDBEntry
     init(pdbId, chainCode, null, null);
   }
 
-  public void setFile(String file)
+  public void setFile(String f)
   {
-    this.file = file;
+    this.file = f;
   }
 
   public String getFile()
@@ -228,14 +232,33 @@ public class PDBEntry
     return id;
   }
 
-  public void setProperty(Hashtable property)
+  public void setProperty(String key, Object value)
   {
-    this.properties = property;
+    if (this.properties == null)
+    {
+      this.properties = new Hashtable<String, Object>();
+    }
+    properties.put(key, value);
   }
 
-  public Hashtable getProperty()
+  public Object getProperty(String key)
   {
-    return properties;
+    return properties == null ? null : properties.get(key);
+  }
+
+  /**
+   * Returns an enumeration of the keys of this object's properties (or an empty
+   * enumeration if it has no properties)
+   * 
+   * @return
+   */
+  public Enumeration<String> getProperties()
+  {
+    if (properties == null)
+    {
+      return Collections.emptyEnumeration();
+    }
+    return properties.keys();
   }
 
   /**
@@ -248,24 +271,37 @@ public class PDBEntry
             : properties.get(CHAIN_ID).toString();
   }
 
+  /**
+   * Sets a non-case-sensitive property for the given chain code. Two PDBEntry
+   * objects which differ only in the case of their chain code are considered
+   * equal. This avoids duplication of objects in lists of PDB ids.
+   * 
+   * @param chainCode
+   */
   public void setChainCode(String chainCode)
   {
-    if (properties == null)
+    if (chainCode == null)
     {
-      if (chainCode == null)
-      {
-        // nothing to do.
-        return;
-      }
-      properties = new Hashtable();
+      deleteProperty(CHAIN_ID);
     }
-    if (chainCode == null)
+    else
+    {
+      setProperty(CHAIN_ID, new CaseInsensitiveString(chainCode));
+    }
+  }
+
+  /**
+   * Deletes the property with the given key, and returns the deleted value (or
+   * null)
+   */
+  Object deleteProperty(String key)
+  {
+    Object result = null;
+    if (properties != null)
     {
-      properties.remove(CHAIN_ID);
-      return;
+      result = properties.remove(key);
     }
-    // update property for non-null chainCode
-    properties.put(CHAIN_ID, new CaseInsensitiveString(chainCode));
+    return result;
   }
 
   @Override
@@ -275,6 +311,35 @@ public class PDBEntry
   }
 
   /**
+   * Getter provided for Castor binding only. Application code should call
+   * getProperty() or getProperties() instead.
+   * 
+   * @deprecated
+   * @see #getProperty(String)
+   * @see #getProperties()
+   * @see jalview.ws.dbsources.Uniprot#getUniprotEntries
+   * @return
+   */
+  @Deprecated
+  public Hashtable<String, Object> getProps()
+  {
+    return properties;
+  }
+
+  /**
+   * Setter provided for Castor binding only. Application code should call
+   * setProperty() instead.
+   * 
+   * @deprecated
+   * @return
+   */
+  @Deprecated
+  public void setProps(Hashtable<String, Object> props)
+  {
+    properties = props;
+  }
+
+  /**
    * Answers true if this object is either equivalent to, or can be 'improved'
    * by, the given entry.
    * <p>
@@ -285,7 +350,7 @@ public class PDBEntry
    * @param newEntry
    * @return true if modifications were made
    */
-  protected boolean updateFrom(PDBEntry newEntry)
+  public boolean updateFrom(PDBEntry newEntry)
   {
     if (this.equals(newEntry))
     {
@@ -299,7 +364,7 @@ public class PDBEntry
     }
 
     /*
-     * id (less any chain code) has to match (ignoring case)
+     * id has to match (ignoring case)
      */
     if (!getId().equalsIgnoreCase(newId))
     {
@@ -356,26 +421,20 @@ public class PDBEntry
     }
 
     /*
-     * copy any new properties; notice this may include chain_code,
-     * but we excluded differing chain codes earlier
+     * copy any new or modified properties
      */
-    if (newEntry.getProperty() != null)
+    Enumeration<String> newProps = newEntry.getProperties();
+    while (newProps.hasMoreElements())
     {
-      if (properties == null)
+      /*
+       * copy properties unless value matches; this defends against changing
+       * the case of chain_code which is wrapped in a CaseInsensitiveString
+       */
+      String key = newProps.nextElement();
+      Object value = newEntry.getProperty(key);
+      if (!value.equals(getProperty(key)))
       {
-        properties = new Hashtable();
-      }
-      for (Object p : newEntry.getProperty().keySet())
-      {
-        /*
-         * copy properties unless value matches; this defends against changing
-         * the case of chain_code which is wrapped in a CaseInsensitiveString
-         */
-        Object value = newEntry.getProperty().get(p);
-        if (!value.equals(properties.get(p)))
-        {
-          properties.put(p, newEntry.getProperty().get(p));
-        }
+        setProperty(key, value);
       }
     }
     return true;