changeset 1271:3e7717b6e2bc

Implement equals und hashCode for Recommendation and associates. flys-client/trunk@2841 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 27 Sep 2011 13:46:33 +0000
parents ea9a73782de4
children 2ce5d7ac9e60
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/shared/model/Recommendation.java
diffstat 2 files changed, 105 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Tue Sep 27 13:27:08 2011 +0000
+++ b/flys-client/ChangeLog	Tue Sep 27 13:46:33 2011 +0000
@@ -1,3 +1,12 @@
+2011-09-27	Felix Wolfsteller	<felix.wolfsteller@intevation.de> 
+
+	Implement equals and hashCode for Recommendation, Recommendation.Facet
+	and Recommendation.Filter .
+
+	* src/main/java/de/intevation/flys/client/shared/model/Recommendation.java
+	  (equals, hashCode, Filter.equals, Filter.hashCode, Facet.equals)
+	  (Facet.hashCode): New.
+
 2011-09-27  Ingo Weinzierl <ingo@intevation.de>
 
 	flys/issue321 (ÜSK: Aktualisieren der Parameterliste nach Beendigung der Berechnung)
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/Recommendation.java	Tue Sep 27 13:27:08 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/Recommendation.java	Tue Sep 27 13:46:33 2011 +0000
@@ -12,6 +12,7 @@
  */
 public class Recommendation implements Serializable {
 
+    /** Index and name of a facet. */
     public static class Facet implements Serializable {
 
         protected String name;
@@ -32,8 +33,34 @@
         public String getIndex() {
             return index;
         }
+
+
+        @Override
+        public int hashCode() {
+            int hash = 0;
+            if (getName() != null) {
+                hash += getName().hashCode();
+            }
+            if (getIndex() != null) {
+                hash += getIndex().hashCode();
+            }
+            return hash;
+        }
+
+
+        @Override
+        public boolean equals(Object other) {
+            if (!(other instanceof Facet) || other == null) {
+                return false;
+            }
+            Facet facet = (Facet) other;
+            return (same(facet.getIndex(), this.getIndex()))
+                && (same(facet.getName(),  this.getName()));
+        }
     } // class Facet
 
+
+    /** Mapping of outnames to Facet-Lists. */
     public static class Filter implements Serializable {
 
         protected Map<String, List<Facet>> outs;
@@ -49,6 +76,25 @@
         public Map<String, List<Facet>> getOuts() {
             return outs;
         }
+
+
+        @Override
+        public int hashCode() {
+            if (getOuts() != null) {
+                return getOuts().hashCode();
+            }
+            return 0;
+        }
+
+
+        @Override
+        public boolean equals(Object other) {
+            if (!(other instanceof Filter) || other == null) {
+                return false;
+            }
+            Filter filter = (Filter) other;
+            return Recommendation.same(filter.getOuts(), this.getOuts());
+        }
     } // class Filter
 
     protected String factory;
@@ -98,5 +144,55 @@
     public void setFilter(Filter filter) {
         this.filter = filter;
     }
+
+
+    @Override
+    public int hashCode() {
+        int hash = 0;
+        hash += (getFactory() != null)
+            ? getFactory().hashCode()
+            : 0;
+        hash += (getIDs() != null)
+            ? getIDs().hashCode()
+            : 0;
+        hash += (getFilter() != null)
+            ? getFilter().hashCode()
+            : 0;
+        hash += (getMasterArtifact() != null)
+            ? getMasterArtifact().hashCode()
+            : 0;
+        return hash;
+    }
+
+
+    /**
+     * Null-pointer guarded equals.
+     * Two null's are assumed equal (returns true);
+     * @param a Object to compare against parameter b.
+     * @param b Object to compare against parameter a.
+     * @return true if either a and b are null or a.equals(b) returns true.
+     */
+    protected static boolean same(Object a, Object b) {
+        // Do null-check.
+        if (a == null) {
+            return b == null;
+        } else if (b == null) {
+            return false;
+        }
+        return a.equals(b);
+    }
+
+
+    @Override
+    public boolean equals(Object other) {
+        if (!(other instanceof Recommendation) || other == null) {
+            return false;
+        }
+        Recommendation rec = (Recommendation) other;
+        return (same(this.getFactory(), rec.getFactory()))
+            && (same(this.getIDs(),     rec.getIDs()))
+            && (same(this.getFilter(),  rec.getFilter()))
+            && (same(this.getMasterArtifact(), rec.getMasterArtifact()));
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org