Revision: 14493
          http://gate.svn.sourceforge.net/gate/?rev=14493&view=rev
Author:   markagreenwood
Date:     2011-11-04 12:29:42 +0000 (Fri, 04 Nov 2011)
Log Message:
-----------
fixed some equals methods so they obey the contract and check for null objects

Modified Paths:
--------------
    gate/trunk/src/gate/corpora/MimeType.java
    gate/trunk/src/gate/creole/ResourceData.java
    gate/trunk/src/gate/fsm/FSMInstance.java
    gate/trunk/src/gate/jape/JdmAttribute.java
    gate/trunk/src/gate/util/Pair.java

Modified: gate/trunk/src/gate/corpora/MimeType.java
===================================================================
--- gate/trunk/src/gate/corpora/MimeType.java   2011-11-04 02:17:34 UTC (rev 
14492)
+++ gate/trunk/src/gate/corpora/MimeType.java   2011-11-04 12:29:42 UTC (rev 
14493)
@@ -31,7 +31,7 @@
   public MimeType(String type, String subType){
     this.type = type;
     this.subtype = subType;
-    parameters = new HashMap();
+    parameters = new HashMap<String,String>();
   }
 
   /**
@@ -40,7 +40,7 @@
    * @return true if the two MIME Types are the same.
    */
   public boolean equals(Object other){
-    return type.equals(((MimeType)other).getType()) &&
+    return other != null && type.equals(((MimeType)other).getType()) &&
            subtype.equals(((MimeType)other).getSubtype());
   }
 
@@ -91,7 +91,7 @@
    * @param param the name of the parameter.
    * @param value the value of the parameter.
    */
-  public void addParameter(java.lang.String param, java.lang.String value){
+  public void addParameter(String param, String value){
     parameters.put(param, value);
   }
 
@@ -100,8 +100,8 @@
    * @param name the name of the parameter.
    * @return a {@link java.lang.String} value.
    */
-  public java.lang.String getParameterValue(java.lang.String name){
-    return (String)parameters.get(name);
+  public String getParameterValue(String name){
+    return parameters.get(name);
   }
 
   /**
@@ -109,7 +109,7 @@
    * @param name the name of the parameter.
    * @return a boolean value.
    */
-  public boolean hasParameter(java.lang.String name){
+  public boolean hasParameter(String name){
     return parameters.containsKey(name);
   }
 
@@ -126,5 +126,5 @@
   /**
    * The parameters map.
    */
-  protected Map parameters;
+  protected Map<String,String> parameters;
 }
\ No newline at end of file

Modified: gate/trunk/src/gate/creole/ResourceData.java
===================================================================
--- gate/trunk/src/gate/creole/ResourceData.java        2011-11-04 02:17:34 UTC 
(rev 14492)
+++ gate/trunk/src/gate/creole/ResourceData.java        2011-11-04 12:29:42 UTC 
(rev 14493)
@@ -84,6 +84,7 @@
     * same name
     */
   public boolean equals(Object other) {
+    if (other == null) return false;
     if(name.equals(((ResourceData) other).getName()))
       return true;
     return false;

Modified: gate/trunk/src/gate/fsm/FSMInstance.java
===================================================================
--- gate/trunk/src/gate/fsm/FSMInstance.java    2011-11-04 02:17:34 UTC (rev 
14492)
+++ gate/trunk/src/gate/fsm/FSMInstance.java    2011-11-04 12:29:42 UTC (rev 
14493)
@@ -143,6 +143,7 @@
   }
 
   public boolean equals(Object other){
+    if (other == null) return false;
     if(other instanceof FSMInstance){
       FSMInstance otherFSM = (FSMInstance)other;
       boolean result = length == otherFSM.length &&
@@ -152,7 +153,9 @@
              FSMPosition.getAction().equals(otherFSM.FSMPosition.getAction());
       return result;
     }else{
-      throw new ClassCastException(other.getClass().toString());
+      //equals should never throw an exception it should just return null
+      //throw new ClassCastException(other.getClass().toString());
+      return false;
     }
   }
 

Modified: gate/trunk/src/gate/jape/JdmAttribute.java
===================================================================
--- gate/trunk/src/gate/jape/JdmAttribute.java  2011-11-04 02:17:34 UTC (rev 
14492)
+++ gate/trunk/src/gate/jape/JdmAttribute.java  2011-11-04 12:29:42 UTC (rev 
14493)
@@ -71,9 +71,28 @@
        return value.getClass().getName();
   }
 
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((name == null) ? 0 : name.hashCode());
+    result = prime * result + ((value == null) ? 0 : value.hashCode());
+    return result;
+  }
+
+  @Override
   public boolean equals(Object obj) {
-    JdmAttribute a = (JdmAttribute) obj;
-    return a.getName().equals(name) && a.getValue().equals(value);
+    if(this == obj) return true;
+    if(obj == null) return false;
+    if(getClass() != obj.getClass()) return false;
+    JdmAttribute other = (JdmAttribute)obj;
+    if(name == null) {
+      if(other.name != null) return false;
+    } else if(!name.equals(other.name)) return false;
+    if(value == null) {
+      if(other.value != null) return false;
+    } else if(!value.equals(other.value)) return false;
+    return true;
   }
 
   public String toString() {

Modified: gate/trunk/src/gate/util/Pair.java
===================================================================
--- gate/trunk/src/gate/util/Pair.java  2011-11-04 02:17:34 UTC (rev 14492)
+++ gate/trunk/src/gate/util/Pair.java  2011-11-04 12:29:42 UTC (rev 14493)
@@ -33,18 +33,32 @@
   public Pair(Pair p0) {first = p0.first; second = p0.second; }
 
   // Methods
-  public int hashCode() { return first.hashCode() ^ second.hashCode(); }
   public String toString() { return "<" + first.toString() +
                                     ", " + second.toString() + ">" ;}
-  public boolean equals(Object p0) {
-    if (!p0.getClass().equals(this.getClass()))
-      return false;
-    return equals((Pair) p0);
-  }//equals
-  public boolean equals(Pair p0) {
-    if (p0.first.equals(first)&& p0.second.equals(second))
-      return true;
-    return false;
-  } //equals
+  
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((first == null) ? 0 : first.hashCode());
+    result = prime * result + ((second == null) ? 0 : second.hashCode());
+    return result;
+  }
+  
+  @Override
+  public boolean equals(Object obj) {
+    if(this == obj) return true;
+    if(obj == null) return false;
+    if(getClass() != obj.getClass()) return false;
+    Pair other = (Pair)obj;
+    if(first == null) {
+      if(other.first != null) return false;
+    } else if(!first.equals(other.first)) return false;
+    if(second == null) {
+      if(other.second != null) return false;
+    } else if(!second.equals(other.second)) return false;
+    return true;
+  }
+  
   public synchronized Object clone() { return new Pair(first, second); }
 }
\ No newline at end of file

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to