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

Last change on this file since 5171 was 5171, checked in by Don-vip, 12 years ago

history/osmChange handling: fix an issue when downloading osmChange that could lead to the creation of empty ways (0 node) + fix spelling of a getter

  • Property svn:eol-style set to native
File size: 7.3 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 public int compare(HistoryOsmPrimitive o1, HistoryOsmPrimitive o2) {
70 return o1.compareTo(o2);
71 }
72 }
73 );
74 return new History(id, type, copy);
75 }
76
77 public History sortDescending() {
78 ArrayList<HistoryOsmPrimitive> copy = new ArrayList<HistoryOsmPrimitive>(versions);
79 Collections.sort(
80 copy,
81 new Comparator<HistoryOsmPrimitive>() {
82 public int compare(HistoryOsmPrimitive o1, HistoryOsmPrimitive o2) {
83 return o2.compareTo(o1);
84 }
85 }
86 );
87 return new History(id, type,copy);
88 }
89
90 public History from(final Date fromDate) {
91 return filter(
92 this,
93 new FilterPredicate() {
94 public boolean matches(HistoryOsmPrimitive primitive) {
95 return primitive.getTimestamp().compareTo(fromDate) >= 0;
96 }
97 }
98 );
99 }
100
101 public History until(final Date untilDate) {
102 return filter(
103 this,
104 new FilterPredicate() {
105 public boolean matches(HistoryOsmPrimitive primitive) {
106 return primitive.getTimestamp().compareTo(untilDate) <= 0;
107 }
108 }
109 );
110 }
111
112 public History between(Date fromDate, Date untilDate) {
113 return this.from(fromDate).until(untilDate);
114 }
115
116 public History from(final long fromVersion) {
117 return filter(
118 this,
119 new FilterPredicate() {
120 public boolean matches(HistoryOsmPrimitive primitive) {
121 return primitive.getVersion() >= fromVersion;
122 }
123 }
124 );
125 }
126
127 public History until(final long untilVersion) {
128 return filter(
129 this,
130 new FilterPredicate() {
131 public boolean matches(HistoryOsmPrimitive primitive) {
132 return primitive.getVersion() <= untilVersion;
133 }
134 }
135 );
136 }
137
138 public History between(long fromVersion, long untilVersion) {
139 return this.from(fromVersion).until(untilVersion);
140 }
141
142 public History forUserId(final long uid) {
143 return filter(
144 this,
145 new FilterPredicate() {
146 public boolean matches(HistoryOsmPrimitive primitive) {
147 return primitive.getUser() != null && primitive.getUser().getId() == uid;
148 }
149 }
150 );
151 }
152
153 public long getId() {
154 return id;
155 }
156
157 /**
158 * Replies the primitive id for this history.
159 *
160 * @return the primitive id
161 */
162 public PrimitiveId getPrimitiveId() {
163 return new SimplePrimitiveId(id, type);
164 }
165
166 public boolean contains(long version){
167 for (HistoryOsmPrimitive primitive: versions) {
168 if (primitive.matches(id,version))
169 return true;
170 }
171 return false;
172 }
173
174 /**
175 * Replies the history primitive with version <code>version</code>. null,
176 * if no such primitive exists.
177 *
178 * @param version the version
179 * @return the history primitive with version <code>version</code>
180 */
181 public HistoryOsmPrimitive getByVersion(long version) {
182 for (HistoryOsmPrimitive primitive: versions) {
183 if (primitive.matches(id,version))
184 return primitive;
185 }
186 return null;
187 }
188
189 public HistoryOsmPrimitive getByDate(Date date) {
190 History h = sortAscending();
191
192 if (h.versions.isEmpty())
193 return null;
194 if (h.get(0).getTimestamp().compareTo(date)> 0)
195 return null;
196 for (int i = 1; i < h.versions.size();i++) {
197 if (h.get(i-1).getTimestamp().compareTo(date) <= 0
198 && h.get(i).getTimestamp().compareTo(date) >= 0)
199 return h.get(i);
200 }
201 return h.getLatest();
202 }
203
204 public HistoryOsmPrimitive get(int idx) {
205 if (idx < 0 || idx >= versions.size())
206 throw new IndexOutOfBoundsException(MessageFormat.format("Parameter ''{0}'' in range 0..{1} expected. Got ''{2}''.", "idx", versions.size()-1, idx));
207 return versions.get(idx);
208 }
209
210 public HistoryOsmPrimitive getEarliest() {
211 if (isEmpty())
212 return null;
213 return sortAscending().versions.get(0);
214 }
215
216 public HistoryOsmPrimitive getLatest() {
217 if (isEmpty())
218 return null;
219 return sortDescending().versions.get(0);
220 }
221
222 public int getNumVersions() {
223 return versions.size();
224 }
225
226 public boolean isEmpty() {
227 return versions.isEmpty();
228 }
229
230 public OsmPrimitiveType getType() {
231 return type;
232 }
233}
Note: See TracBrowser for help on using the repository browser.