Source code: jtemporal/AbstractTemporalUnaryMap.java
1 /*
2 Copyright (C) 2002 Thomas A Beck (http://thomas.beck.name)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 package jtemporal;
19
20 import java.util.Set;
21 import java.util.Iterator;
22
23 /**
24 * Features common to the UnaryMaps.
25 * @author Thomas Beck
26 * @version $Id$
27 */
28
29 abstract public class AbstractTemporalUnaryMap implements TemporalUnaryMap {
30 abstract public void clear();
31 abstract public int size();
32 abstract public boolean isEmpty();
33 abstract public Object get(Instant instant);
34 abstract public Period getPeriod(Instant instant);
35 abstract public boolean containsInstant(Instant instant);
36 abstract public boolean containsValue(Object value);
37 abstract public boolean put(Period p, Object value);
38 abstract public boolean remove(Period p);
39 abstract public Set periodSet();
40 abstract public Instant firstInstant();
41 abstract public Instant lastInstant();
42 abstract public Period firstPeriod();
43 abstract public Period lastPeriod();
44 abstract public TemporalUnaryMap subMap(Period p);
45
46 public boolean putAll(TemporalUnaryMap tm) {
47 boolean ret = false;
48 Iterator i = tm.periodSet().iterator();
49 while (i.hasNext()) {
50 Period p = (Period) i.next();
51 ret |= this.put(p, tm.get(p.getStart())) ;
52 }
53 return ret;
54 }
55
56 public Period extent() {
57 Instant first = firstInstant(); // throws NoSuchElementException
58 Instant last = lastInstant(); // throws NoSuchElementException
59 return new Period(first, last);
60 }
61
62 // Comparison and hashing
63
64 /**
65 * Compares the specified object with this map for content equality.
66 * Returns
67 * <tt>true</tt> if the given object contains the same mappings.
68 *
69 * <BR>
70 * This implementation first checks if the specified object is this map;
71 * if so it returns <tt>true</tt>. Then, it checks if the specified
72 * object is a map whose size is identical to the size of this set; if
73 * not, it it returns <tt>false</tt>. If so, it iterates over this map's
74 * Periods and Values, and checks them for equality.
75 * <BR>
76 * The method equals is not overridden in order to keep the default hashcode()
77 * method. Overriding hashcode() would lead to poor performances when adding
78 * this map to a hashmap (which is done by the TemporalMultiMap).
79 *
80 * @param o object to be compared for equality with this map.
81 * @return <tt>true</tt> if the specified object is equal to this map.
82 */
83 public boolean equalContent(Object o) {
84 if (o == this)
85 return true;
86
87 if (!(o instanceof AbstractTemporalUnaryMap))
88 return false;
89 AbstractTemporalUnaryMap t = (AbstractTemporalUnaryMap) o;
90 int size = this.size();
91 if (t.size() != size)
92 return false;
93 if (size == 0) return true;
94
95 try {
96 Iterator i = periodSet().iterator();
97 while (i.hasNext()) {
98 Period p = (Period) i.next();
99 Object value = this.get(p.getStart());
100 if (value == null) {
101 throw new IllegalStateException(); // should normally never happen
102 } else {
103 if (!p.equals(t.getPeriod(p.getStart())))
104 return false;
105
106 if (!value.equals(t.get(p.getStart())))
107 return false;
108 }
109 }
110 } catch(ClassCastException unused) {
111 return false;
112 } catch(NullPointerException unused) {
113 return false;
114 }
115 return true;
116 }
117
118 /**
119 * Returns the hash code value for this map. The hash code of a map is
120 * defined to be the sum of the hash codes of each entry in the map's
121 * <tt>entrySet()</tt> view. This ensures that <tt>t1.equals(t2)</tt>
122 * implies that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps
123 * <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of
124 * Object.hashCode.<p>
125 *
126 * This implementation iterates over <tt>entrySet()</tt>, calling
127 * <tt>hashCode</tt> on each element (entry) in the Collection, and adding
128 * up the results.
129 *
130 * @return the hash code value for this map.
131 * @see Map.Entry#hashCode()
132 * @see Object#hashCode()
133 * @see Object#equals(Object)
134 * @see Set#equals(Object)
135 */
136 /* public int hashCode() {
137 throw new UnsupportedOperationException();
138 int h = 0;
139 Iterator i = entrySet().iterator();
140 while (i.hasNext())
141 h += i.next().hashCode();
142 return h;
143 } */
144
145
146 }