source: josm/trunk/src/org/openstreetmap/josm/data/osm/history/History.java@ 6084

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.history;
3
4import java.text.MessageFormat;
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.Comparator;
8import java.util.Date;
9import java.util.List;
10
11import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
12import org.openstreetmap.josm.data.osm.PrimitiveId;
13import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15
16/**
17 * Represents the history of an OSM primitive. The history consists
18 * of a list of object snapshots with a specific version.
19 *
20 */
21public class History{
22 private static interface FilterPredicate {
23 boolean matches(HistoryOsmPrimitive primitive);
24 }
25
26 private static History filter(History history, FilterPredicate predicate) {
27 ArrayList<HistoryOsmPrimitive> out = new ArrayList<HistoryOsmPrimitive>();
28 for (HistoryOsmPrimitive primitive: history.versions) {
29 if (predicate.matches(primitive)) {
30 out.add(primitive);
31 }
32 }
33 return new History(history.id, history.type,out);
34 }
35
36 /** the list of object snapshots */
37 private ArrayList<HistoryOsmPrimitive> versions;
38 /** the object id */
39 private final long id;
40 private final OsmPrimitiveType type;
41
42 /**
43 * Creates a new history for an OSM primitive
44 *
45 * @param id the id. >0 required.
46 * @param type the primitive type. Must not be null.
47 * @param versions a list of versions. Can be null.
48 * @throws IllegalArgumentException thrown if id <= 0
49 * @throws IllegalArgumentException if type is null
50 *
51 */
52 protected History(long id, OsmPrimitiveType type, List<HistoryOsmPrimitive> versions) {
53 if (id <= 0)
54 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected, got {1}", "id", id));
55 CheckParameterUtil.ensureParameterNotNull(type, "type");
56 this.id = id;
57 this.type = type;
58 this.versions = new ArrayList<HistoryOsmPrimitive>();
59 if (versions != null) {
60 this.versions.addAll(versions);
61 }
62 }
63
64 public History sortAscending() {
65 ArrayList<HistoryOsmPrimitive> copy = new ArrayList<HistoryOsmPrimitive>(versions);
66 Collections.sort(
67 copy,
68 new Comparator<HistoryOsmPrimitive>() {
69 @Override
70 public int compare(HistoryOsmPrimitive o1, HistoryOsmPrimitive o2) {
71 return o1.compareTo(o2);
72 }
73 }
74 );
75 return new History(id, type, copy);
76 }
77
78 public History sortDescending() {
79 ArrayList<HistoryOsmPrimitive> copy = new ArrayList<HistoryOsmPrimitive>(versions);
80 Collections.sort(
81 copy,
82 new Comparator<HistoryOsmPrimitive>() {
83 @Override
84 public int compare(HistoryOsmPrimitive o1, HistoryOsmPrimitive o2) {
85 return o2.compareTo(o1);
86 }
87 }
88 );
89 return new History(id, type,copy);
90 }
91
92 public History from(final Date fromDate) {
93 return filter(
94 this,
95 new FilterPredicate() {
96 @Override
97 public boolean matches(HistoryOsmPrimitive primitive) {
98 return primitive.getTimestamp().compareTo(fromDate) >= 0;
99 }
100 }
101 );
102 }
103
104 public History until(final Date untilDate) {
105 return filter(
106 this,
107 new FilterPredicate() {
108 @Override
109 public boolean matches(HistoryOsmPrimitive primitive) {
110 return primitive.getTimestamp().compareTo(untilDate) <= 0;
111 }
112 }
113 );
114 }
115
116 public History between(Date fromDate, Date untilDate) {
117 return this.from(fromDate).until(untilDate);
118 }
119
120 public History from(final long fromVersion) {
121 return filter(
122 this,
123 new FilterPredicate() {
124 @Override
125 public boolean matches(HistoryOsmPrimitive primitive) {
126 return primitive.getVersion() >= fromVersion;
127 }
128 }
129 );
130 }
131
132 public History until(final long untilVersion) {
133 return filter(
134 this,
135 new FilterPredicate() {
136 @Override
137 public boolean matches(HistoryOsmPrimitive primitive) {
138 return primitive.getVersion() <= untilVersion;
139 }
140 }
141 );
142 }
143
144 public History between(long fromVersion, long untilVersion) {
145 return this.from(fromVersion).until(untilVersion);
146 }
147
148 public History forUserId(final long uid) {
149 return filter(
150 this,
151 new FilterPredicate() {
152 @Override
153 public boolean matches(HistoryOsmPrimitive primitive) {
154 return primitive.getUser() != null && primitive.getUser().getId() == uid;
155 }
156 }
157 );
158 }
159
160 public long getId() {
161 return id;
162 }
163
164 /**
165 * Replies the primitive id for this history.
166 *
167 * @return the primitive id
168 */
169 public PrimitiveId getPrimitiveId() {
170 return new SimplePrimitiveId(id, type);
171 }
172
173 public boolean contains(long version){
174 for (HistoryOsmPrimitive primitive: versions) {
175 if (primitive.matches(id,version))
176 return true;
177 }
178 return false;
179 }
180
181 /**
182 * Replies the history primitive with version <code>version</code>. null,
183 * if no such primitive exists.
184 *
185 * @param version the version
186 * @return the history primitive with version <code>version</code>
187 */
188 public HistoryOsmPrimitive getByVersion(long version) {
189 for (HistoryOsmPrimitive primitive: versions) {
190 if (primitive.matches(id,version))
191 return primitive;
192 }
193 return null;
194 }
195
196 public HistoryOsmPrimitive getByDate(Date date) {
197 History h = sortAscending();
198
199 if (h.versions.isEmpty())
200 return null;
201 if (h.get(0).getTimestamp().compareTo(date)> 0)
202 return null;
203 for (int i = 1; i < h.versions.size();i++) {
204 if (h.get(i-1).getTimestamp().compareTo(date) <= 0
205 && h.get(i).getTimestamp().compareTo(date) >= 0)
206 return h.get(i);
207 }
208 return h.getLatest();
209 }
210
211 public HistoryOsmPrimitive get(int idx) {
212 if (idx < 0 || idx >= versions.size())
213 throw new IndexOutOfBoundsException(MessageFormat.format("Parameter ''{0}'' in range 0..{1} expected. Got ''{2}''.", "idx", versions.size()-1, idx));
214 return versions.get(idx);
215 }
216
217 public HistoryOsmPrimitive getEarliest() {
218 if (isEmpty())
219 return null;
220 return sortAscending().versions.get(0);
221 }
222
223 public HistoryOsmPrimitive getLatest() {
224 if (isEmpty())
225 return null;
226 return sortDescending().versions.get(0);
227 }
228
229 public int getNumVersions() {
230 return versions.size();
231 }
232
233 public boolean isEmpty() {
234 return versions.isEmpty();
235 }
236
237 public OsmPrimitiveType getType() {
238 return type;
239 }
240
241 @Override
242 public String toString() {
243 String result = "History ["
244 + (type != null ? "type=" + type + ", " : "") + "id=" + id;
245 if (versions != null) {
246 result += ", versions=\n";
247 for (HistoryOsmPrimitive v : versions) {
248 result += "\t" + v + ",\n";
249 }
250 }
251 result += "]";
252 return result;
253 }
254}
Note: See TracBrowser for help on using the repository browser.