| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.osm; |
|---|
| 3 | |
|---|
| 4 | import java.util.HashMap; |
|---|
| 5 | import java.util.HashSet; |
|---|
| 6 | import java.util.Iterator; |
|---|
| 7 | import java.util.Map; |
|---|
| 8 | import java.util.Set; |
|---|
| 9 | import java.util.Map.Entry; |
|---|
| 10 | |
|---|
| 11 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive; |
|---|
| 12 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * A ChangesetDataSet holds the content of a changeset. |
|---|
| 16 | */ |
|---|
| 17 | public class ChangesetDataSet { |
|---|
| 18 | |
|---|
| 19 | public static enum ChangesetModificationType { |
|---|
| 20 | CREATED, |
|---|
| 21 | UPDATED, |
|---|
| 22 | DELETED |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | public static interface ChangesetDataSetEntry { |
|---|
| 26 | public ChangesetModificationType getModificationType(); |
|---|
| 27 | public HistoryOsmPrimitive getPrimitive(); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | final private Map<PrimitiveId, HistoryOsmPrimitive> primitives = new HashMap<PrimitiveId, HistoryOsmPrimitive>(); |
|---|
| 31 | final private Map<PrimitiveId, ChangesetModificationType> modificationTypes = new HashMap<PrimitiveId, ChangesetModificationType>(); |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Remembers a history primitive with the given modification type |
|---|
| 35 | * |
|---|
| 36 | * @param primitive the primitive. Must not be null. |
|---|
| 37 | * @param cmt the modification type. Must not be null. |
|---|
| 38 | * @throws IllegalArgumentException thrown if primitive is null |
|---|
| 39 | * @throws IllegalArgumentException thrown if cmt is null |
|---|
| 40 | */ |
|---|
| 41 | public void put(HistoryOsmPrimitive primitive, ChangesetModificationType cmt) throws IllegalArgumentException{ |
|---|
| 42 | CheckParameterUtil.ensureParameterNotNull(primitive,"primitive"); |
|---|
| 43 | CheckParameterUtil.ensureParameterNotNull(cmt,"cmt"); |
|---|
| 44 | primitives.put(primitive.getPrimitiveId(), primitive); |
|---|
| 45 | modificationTypes.put(primitive.getPrimitiveId(), cmt); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Replies true if the changeset content contains the object with primitive <code>id</code>. |
|---|
| 50 | * @param id the id. |
|---|
| 51 | * @return true if the changeset content contains the object with primitive <code>id</code> |
|---|
| 52 | */ |
|---|
| 53 | public boolean contains(PrimitiveId id) { |
|---|
| 54 | if (id == null) return false; |
|---|
| 55 | return primitives.containsKey(id); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Replies the modification type for the object with id <code>id</code>. Replies null, if id is null or |
|---|
| 60 | * if the object with id <code>id</code> isn't in the changeset content. |
|---|
| 61 | * |
|---|
| 62 | * @param id the id |
|---|
| 63 | * @return the modification type |
|---|
| 64 | */ |
|---|
| 65 | public ChangesetModificationType getModificationType(PrimitiveId id) { |
|---|
| 66 | if (!contains(id)) return null; |
|---|
| 67 | return modificationTypes.get(id); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Replies true if the primitive with id <code>id</code> was created in this |
|---|
| 72 | * changeset. Replies false, if id is null. |
|---|
| 73 | * |
|---|
| 74 | * @param id the id |
|---|
| 75 | * @return true if the primitive with id <code>id</code> was created in this |
|---|
| 76 | * changeset. |
|---|
| 77 | */ |
|---|
| 78 | public boolean isCreated(PrimitiveId id) { |
|---|
| 79 | if (!contains(id)) return false; |
|---|
| 80 | return ChangesetModificationType.CREATED.equals(getModificationType(id)); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Replies true if the primitive with id <code>id</code> was updated in this |
|---|
| 85 | * changeset. Replies false, if id is null. |
|---|
| 86 | * |
|---|
| 87 | * @param id the id |
|---|
| 88 | * @return true if the primitive with id <code>id</code> was updated in this |
|---|
| 89 | * changeset. |
|---|
| 90 | */ |
|---|
| 91 | public boolean isUpdated(PrimitiveId id) { |
|---|
| 92 | if (!contains(id)) return false; |
|---|
| 93 | return ChangesetModificationType.UPDATED.equals(getModificationType(id)); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Replies true if the primitive with id <code>id</code> was deleted in this |
|---|
| 98 | * changeset. Replies false, if id is null. |
|---|
| 99 | * |
|---|
| 100 | * @param id the id |
|---|
| 101 | * @return true if the primitive with id <code>id</code> was deleted in this |
|---|
| 102 | * changeset. |
|---|
| 103 | */ |
|---|
| 104 | public boolean isDeleted(PrimitiveId id) { |
|---|
| 105 | if (!contains(id)) return false; |
|---|
| 106 | return ChangesetModificationType.DELETED.equals(getModificationType(id)); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Replies the set of primitives with a specific modification type |
|---|
| 111 | * |
|---|
| 112 | * @param cmt the modification type. Must not be null. |
|---|
| 113 | * @return the set of primitives |
|---|
| 114 | * @throws IllegalArgumentException thrown if cmt is null |
|---|
| 115 | */ |
|---|
| 116 | public Set<HistoryOsmPrimitive> getPrimitivesByModificationType(ChangesetModificationType cmt) throws IllegalArgumentException { |
|---|
| 117 | CheckParameterUtil.ensureParameterNotNull(cmt,"cmt"); |
|---|
| 118 | HashSet<HistoryOsmPrimitive> ret = new HashSet<HistoryOsmPrimitive>(); |
|---|
| 119 | for (Entry<PrimitiveId, ChangesetModificationType> entry: modificationTypes.entrySet()) { |
|---|
| 120 | if (entry.getValue().equals(cmt)) { |
|---|
| 121 | ret.add(primitives.get(entry.getKey())); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | return ret; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Replies the number of objects in the dataset |
|---|
| 129 | * |
|---|
| 130 | * @return the number of objects in the dataset |
|---|
| 131 | */ |
|---|
| 132 | public int size() { |
|---|
| 133 | return primitives.size(); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Replies the {@see HistoryOsmPrimitive} with id <code>id</code> from this |
|---|
| 138 | * dataset. null, if there is no such primitive in the data set. |
|---|
| 139 | * |
|---|
| 140 | * @param id the id |
|---|
| 141 | * @return the {@see HistoryOsmPrimitive} with id <code>id</code> from this |
|---|
| 142 | * dataset |
|---|
| 143 | */ |
|---|
| 144 | public HistoryOsmPrimitive getPrimitive(PrimitiveId id) { |
|---|
| 145 | if (id == null) return null; |
|---|
| 146 | return primitives.get(id); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | public Iterator<ChangesetDataSetEntry> iterator() { |
|---|
| 150 | return new DefaultIterator(); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | private static class DefaultChangesetDataSetEntry implements ChangesetDataSetEntry { |
|---|
| 154 | private ChangesetModificationType modificationType; |
|---|
| 155 | private HistoryOsmPrimitive primitive; |
|---|
| 156 | |
|---|
| 157 | public DefaultChangesetDataSetEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive) { |
|---|
| 158 | this.modificationType = modificationType; |
|---|
| 159 | this.primitive = primitive; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | public ChangesetModificationType getModificationType() { |
|---|
| 163 | return modificationType; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public HistoryOsmPrimitive getPrimitive() { |
|---|
| 167 | return primitive; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | private class DefaultIterator implements Iterator<ChangesetDataSetEntry> { |
|---|
| 172 | private Iterator<Entry<PrimitiveId, ChangesetModificationType>> typeIterator; |
|---|
| 173 | |
|---|
| 174 | public DefaultIterator() { |
|---|
| 175 | typeIterator = modificationTypes.entrySet().iterator(); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | public boolean hasNext() { |
|---|
| 179 | return typeIterator.hasNext(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | public ChangesetDataSetEntry next() { |
|---|
| 183 | Entry<PrimitiveId, ChangesetModificationType> next = typeIterator.next(); |
|---|
| 184 | ChangesetModificationType type = next.getValue(); |
|---|
| 185 | HistoryOsmPrimitive primitive = primitives.get(next.getKey()); |
|---|
| 186 | return new DefaultChangesetDataSetEntry(type, primitive); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | public void remove() { |
|---|
| 190 | throw new UnsupportedOperationException(); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|