| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.LinkedList;
|
|---|
| 5 | import java.util.List;
|
|---|
| 6 |
|
|---|
| 7 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Represents a collection of {@see OsmPrimitive}s which should be uploaded to the
|
|---|
| 12 | * API.
|
|---|
| 13 | * The collection is derived from the modified primitives of an {@see DataSet}.
|
|---|
| 14 | *
|
|---|
| 15 | * FIXME: use to optimize the upload order before uploading, see various tickets in trac
|
|---|
| 16 | *
|
|---|
| 17 | */
|
|---|
| 18 | public class APIDataSet {
|
|---|
| 19 | private LinkedList<OsmPrimitive> toAdd;
|
|---|
| 20 | private LinkedList<OsmPrimitive> toUpdate;
|
|---|
| 21 | private LinkedList<OsmPrimitive> toDelete;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * creates a new empty data set
|
|---|
| 25 | */
|
|---|
| 26 | public APIDataSet() {
|
|---|
| 27 | toAdd = new LinkedList<OsmPrimitive>();
|
|---|
| 28 | toUpdate = new LinkedList<OsmPrimitive>();
|
|---|
| 29 | toDelete = new LinkedList<OsmPrimitive>();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * initializes the API data set with the modified primitives in <code>ds</ds>
|
|---|
| 34 | *
|
|---|
| 35 | * @param ds the data set. Ignore, if null.
|
|---|
| 36 | */
|
|---|
| 37 | public void init(DataSet ds) {
|
|---|
| 38 | if (ds == null) return;
|
|---|
| 39 | toAdd.clear();
|
|---|
| 40 | toUpdate.clear();
|
|---|
| 41 | toDelete.clear();
|
|---|
| 42 |
|
|---|
| 43 | for (OsmPrimitive osm :ds.allPrimitives()) {
|
|---|
| 44 | if (osm.get("josm/ignore") != null) {
|
|---|
| 45 | continue;
|
|---|
| 46 | }
|
|---|
| 47 | if (osm.getId() == 0 && !osm.isDeleted()) {
|
|---|
| 48 | toAdd.addLast(osm);
|
|---|
| 49 | } else if (osm.isModified() && !osm.isDeleted()) {
|
|---|
| 50 | toUpdate.addLast(osm);
|
|---|
| 51 | } else if (osm.isDeleted() && osm.getId() != 0) {
|
|---|
| 52 | toDelete.addFirst(osm);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * initializes the API data set with the modified primitives in <code>ds</ds>
|
|---|
| 59 | *
|
|---|
| 60 | * @param ds the data set. Ignore, if null.
|
|---|
| 61 | */
|
|---|
| 62 | public APIDataSet(DataSet ds) {
|
|---|
| 63 | this();
|
|---|
| 64 | init(ds);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Replies true if there are no primitives to upload
|
|---|
| 69 | *
|
|---|
| 70 | * @return true if there are no primitives to upload
|
|---|
| 71 | */
|
|---|
| 72 | public boolean isEmpty() {
|
|---|
| 73 | return toAdd.isEmpty() && toUpdate.isEmpty() && toDelete.isEmpty();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Replies the primitives which should be added to the OSM database
|
|---|
| 78 | *
|
|---|
| 79 | * @return the primitives which should be added to the OSM database
|
|---|
| 80 | */
|
|---|
| 81 | public List<OsmPrimitive> getPrimitivesToAdd() {
|
|---|
| 82 | return toAdd;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Replies the primitives which should be updated in the OSM database
|
|---|
| 87 | *
|
|---|
| 88 | * @return the primitives which should be updated in the OSM database
|
|---|
| 89 | */
|
|---|
| 90 | public List<OsmPrimitive> getPrimitivesToUpdate() {
|
|---|
| 91 | return toUpdate;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Replies the primitives which should be deleted in the OSM database
|
|---|
| 96 | *
|
|---|
| 97 | * @return the primitives which should be deleted in the OSM database
|
|---|
| 98 | */
|
|---|
| 99 | public List<OsmPrimitive> getPrimitivesToDelete() {
|
|---|
| 100 | return toDelete;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Replies all primitives
|
|---|
| 105 | *
|
|---|
| 106 | * @return all primitives
|
|---|
| 107 | */
|
|---|
| 108 | public List<OsmPrimitive> getPrimitives() {
|
|---|
| 109 | LinkedList<OsmPrimitive> ret = new LinkedList<OsmPrimitive>();
|
|---|
| 110 | ret.addAll(toAdd);
|
|---|
| 111 | ret.addAll(toUpdate);
|
|---|
| 112 | ret.addAll(toDelete);
|
|---|
| 113 | return ret;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|