comparison flys-client/src/main/java/de/intevation/flys/client/shared/model/Recommendation.java @ 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 4db672cdacb2
children 46a4b74d87bf
comparison
equal deleted inserted replaced
1270:ea9a73782de4 1271:3e7717b6e2bc
10 /** 10 /**
11 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 11 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
12 */ 12 */
13 public class Recommendation implements Serializable { 13 public class Recommendation implements Serializable {
14 14
15 /** Index and name of a facet. */
15 public static class Facet implements Serializable { 16 public static class Facet implements Serializable {
16 17
17 protected String name; 18 protected String name;
18 protected String index; 19 protected String index;
19 20
30 } 31 }
31 32
32 public String getIndex() { 33 public String getIndex() {
33 return index; 34 return index;
34 } 35 }
36
37
38 @Override
39 public int hashCode() {
40 int hash = 0;
41 if (getName() != null) {
42 hash += getName().hashCode();
43 }
44 if (getIndex() != null) {
45 hash += getIndex().hashCode();
46 }
47 return hash;
48 }
49
50
51 @Override
52 public boolean equals(Object other) {
53 if (!(other instanceof Facet) || other == null) {
54 return false;
55 }
56 Facet facet = (Facet) other;
57 return (same(facet.getIndex(), this.getIndex()))
58 && (same(facet.getName(), this.getName()));
59 }
35 } // class Facet 60 } // class Facet
36 61
62
63 /** Mapping of outnames to Facet-Lists. */
37 public static class Filter implements Serializable { 64 public static class Filter implements Serializable {
38 65
39 protected Map<String, List<Facet>> outs; 66 protected Map<String, List<Facet>> outs;
40 67
41 public Filter() { 68 public Filter() {
46 outs.put(out, facets); 73 outs.put(out, facets);
47 } 74 }
48 75
49 public Map<String, List<Facet>> getOuts() { 76 public Map<String, List<Facet>> getOuts() {
50 return outs; 77 return outs;
78 }
79
80
81 @Override
82 public int hashCode() {
83 if (getOuts() != null) {
84 return getOuts().hashCode();
85 }
86 return 0;
87 }
88
89
90 @Override
91 public boolean equals(Object other) {
92 if (!(other instanceof Filter) || other == null) {
93 return false;
94 }
95 Filter filter = (Filter) other;
96 return Recommendation.same(filter.getOuts(), this.getOuts());
51 } 97 }
52 } // class Filter 98 } // class Filter
53 99
54 protected String factory; 100 protected String factory;
55 protected String ids; 101 protected String ids;
96 } 142 }
97 143
98 public void setFilter(Filter filter) { 144 public void setFilter(Filter filter) {
99 this.filter = filter; 145 this.filter = filter;
100 } 146 }
147
148
149 @Override
150 public int hashCode() {
151 int hash = 0;
152 hash += (getFactory() != null)
153 ? getFactory().hashCode()
154 : 0;
155 hash += (getIDs() != null)
156 ? getIDs().hashCode()
157 : 0;
158 hash += (getFilter() != null)
159 ? getFilter().hashCode()
160 : 0;
161 hash += (getMasterArtifact() != null)
162 ? getMasterArtifact().hashCode()
163 : 0;
164 return hash;
165 }
166
167
168 /**
169 * Null-pointer guarded equals.
170 * Two null's are assumed equal (returns true);
171 * @param a Object to compare against parameter b.
172 * @param b Object to compare against parameter a.
173 * @return true if either a and b are null or a.equals(b) returns true.
174 */
175 protected static boolean same(Object a, Object b) {
176 // Do null-check.
177 if (a == null) {
178 return b == null;
179 } else if (b == null) {
180 return false;
181 }
182 return a.equals(b);
183 }
184
185
186 @Override
187 public boolean equals(Object other) {
188 if (!(other instanceof Recommendation) || other == null) {
189 return false;
190 }
191 Recommendation rec = (Recommendation) other;
192 return (same(this.getFactory(), rec.getFactory()))
193 && (same(this.getIDs(), rec.getIDs()))
194 && (same(this.getFilter(), rec.getFilter()))
195 && (same(this.getMasterArtifact(), rec.getMasterArtifact()));
196 }
101 } 197 }
102 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 198 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org