Skip to content

Commit

Permalink
use IPrimitive in upload code
Browse files Browse the repository at this point in the history
  • Loading branch information
bastik committed May 26, 2011
1 parent 780d186 commit 7b1d245
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 137 deletions.
7 changes: 4 additions & 3 deletions src/org/openstreetmap/josm/data/APIDataSet.java
Expand Up @@ -23,6 +23,7 @@
import org.openstreetmap.josm.data.osm.Relation;
import org.openstreetmap.josm.data.osm.RelationMember;
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.tools.Utils;

/**
* Represents a collection of {@see OsmPrimitive}s which should be uploaded to the
Expand Down Expand Up @@ -285,10 +286,10 @@ public void removeProcessed(Collection<OsmPrimitive> processed) {
*/
public void adjustRelationUploadOrder() throws CyclicUploadDependencyException{
LinkedList<OsmPrimitive> newToAdd = new LinkedList<OsmPrimitive>();
newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Node.class));
newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Way.class));
newToAdd.addAll(Utils.filteredCollection(toAdd, Node.class));
newToAdd.addAll(Utils.filteredCollection(toAdd, Way.class));

List<Relation> relationsToAdd = OsmPrimitive.getFilteredList(toAdd, Relation.class);
List<Relation> relationsToAdd = new ArrayList<Relation>(Utils.filteredCollection(toAdd, Relation.class));
List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
newToAdd.addAll(noProblemRelations);
relationsToAdd.removeAll(noProblemRelations);
Expand Down
47 changes: 47 additions & 0 deletions src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java
Expand Up @@ -9,6 +9,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -156,6 +157,7 @@ public boolean isNew() {
* @see #isNew()
* @see #isUndeleted()
*/
@Override
public boolean isNewOrUndeleted() {
return (id <= 0) || ((flags & (FLAG_VISIBLE + FLAG_DELETED)) == 0);
}
Expand Down Expand Up @@ -257,6 +259,7 @@ public void setChangesetId(int changesetId) throws IllegalStateException, Illega
*
* @return the unique primitive id for this primitive
*/
@Override
public PrimitiveId getPrimitiveId() {
return new SimplePrimitiveId(getUniqueId(), getType());
}
Expand Down Expand Up @@ -634,4 +637,48 @@ public boolean hasSameTags(OsmPrimitive other) {

abstract protected void keysChangedImpl(Map<String, String> originalKeys);

/**
* Replies the name of this primitive. The default implementation replies the value
* of the tag <tt>name</tt> or null, if this tag is not present.
*
* @return the name of this primitive
*/
public String getName() {
return get("name");
}

/**
* Replies the a localized name for this primitive given by the value of the tags (in this order)
* <ul>
* <li>name:lang_COUNTRY_Variant of the current locale</li>
* <li>name:lang_COUNTRY of the current locale</li>
* <li>name:lang of the current locale</li>
* <li>name of the current locale</li>
* </ul>
*
* null, if no such tag exists
*
* @return the name of this primitive
*/
public String getLocalName() {
String key = "name:" + Locale.getDefault().toString();
if (get(key) != null)
return get(key);
key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
if (get(key) != null)
return get(key);
key = "name:" + Locale.getDefault().getLanguage();
if (get(key) != null)
return get(key);
return getName();
}

/**
* Replies the display name of a primitive formatted by <code>formatter</code>
*
* @return the display name
*/
@Override
public abstract String getDisplayName(NameFormatter formatter);

}
9 changes: 9 additions & 0 deletions src/org/openstreetmap/josm/data/osm/IPrimitive.java
Expand Up @@ -3,6 +3,8 @@

import java.util.Date;

import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;

public interface IPrimitive extends Tagged, PrimitiveId {

boolean isModified();
Expand All @@ -12,7 +14,9 @@ public interface IPrimitive extends Tagged, PrimitiveId {
boolean isDeleted();
void setDeleted(boolean deleted);
boolean isIncomplete();
boolean isNewOrUndeleted();
long getId();
PrimitiveId getPrimitiveId();
int getVersion();
void setOsmId(long id, int version);
User getUser();
Expand All @@ -22,5 +26,10 @@ public interface IPrimitive extends Tagged, PrimitiveId {
boolean isTimestampEmpty();
int getChangesetId();
void setChangesetId(int changesetId);

void visit(PrimitiveVisitor visitor);
String getName();
String getLocalName();
String getDisplayName(NameFormatter formatter);

}
3 changes: 2 additions & 1 deletion src/org/openstreetmap/josm/data/osm/IWay.java
Expand Up @@ -5,5 +5,6 @@ public interface IWay extends IPrimitive {

int getNodesCount();
long getNodeId(int idx);

boolean isClosed();

}
6 changes: 3 additions & 3 deletions src/org/openstreetmap/josm/data/osm/NameFormatter.java
Expand Up @@ -4,9 +4,9 @@
import java.util.Comparator;

public interface NameFormatter {
String format(Node node);
String format(Way way);
String format(Relation relation);
String format(INode node);
String format(IWay way);
String format(IRelation relation);
String format(Changeset changeset);

Comparator<Node> getNodeComparator();
Expand Down
5 changes: 5 additions & 0 deletions src/org/openstreetmap/josm/data/osm/Node.java
Expand Up @@ -4,6 +4,7 @@
import org.openstreetmap.josm.data.coor.CachedLatLon;
import org.openstreetmap.josm.data.coor.EastNorth;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
import org.openstreetmap.josm.data.osm.visitor.Visitor;

/**
Expand Down Expand Up @@ -140,6 +141,10 @@ void setDataset(DataSet dataSet) {
visitor.visit(this);
}

@Override public void visit(PrimitiveVisitor visitor) {
visitor.visit(this);
}

@Override public void cloneFrom(OsmPrimitive osm) {
boolean locked = writeLock();
try {
Expand Down
12 changes: 12 additions & 0 deletions src/org/openstreetmap/josm/data/osm/NodeData.java
Expand Up @@ -4,6 +4,7 @@
import org.openstreetmap.josm.data.coor.CachedLatLon;
import org.openstreetmap.josm.data.coor.EastNorth;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;

public class NodeData extends PrimitiveData implements INode {

Expand Down Expand Up @@ -52,4 +53,15 @@ public String toString() {
public OsmPrimitiveType getType() {
return OsmPrimitiveType.NODE;
}

@Override
public void visit(PrimitiveVisitor visitor) {
visitor.visit(this);
}

@Override
public String getDisplayName(NameFormatter formatter) {
return formatter.format(this);
}

}
47 changes: 0 additions & 47 deletions src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
Expand Up @@ -9,16 +9,12 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.actions.search.SearchCompiler;
Expand Down Expand Up @@ -1010,49 +1006,6 @@ && isVisible() == other.isVisible()
&& changesetId == other.changesetId;
}

/**
* Replies the name of this primitive. The default implementation replies the value
* of the tag <tt>name</tt> or null, if this tag is not present.
*
* @return the name of this primitive
*/
public String getName() {
return get("name");
}

/**
* Replies the a localized name for this primitive given by the value of the tags (in this order)
* <ul>
* <li>name:lang_COUNTRY_Variant of the current locale</li>
* <li>name:lang_COUNTRY of the current locale</li>
* <li>name:lang of the current locale</li>
* <li>name of the current locale</li>
* </ul>
*
* null, if no such tag exists
*
* @return the name of this primitive
*/
public String getLocalName() {
String key = "name:" + Locale.getDefault().toString();
if (get(key) != null)
return get(key);
key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
if (get(key) != null)
return get(key);
key = "name:" + Locale.getDefault().getLanguage();
if (get(key) != null)
return get(key);
return getName();
}

/**
* Replies the display name of a primitive formatted by <code>formatter</code>
*
* @return the display name
*/
public abstract String getDisplayName(NameFormatter formatter);

/**
* Loads (clone) this primitive from provided PrimitiveData
* @param data
Expand Down
25 changes: 5 additions & 20 deletions src/org/openstreetmap/josm/data/osm/OsmPrimitiveType.java
Expand Up @@ -44,26 +44,11 @@ public static OsmPrimitiveType fromApiTypeName(String typeName) {
throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' is not a valid type name. Got ''{1}''.", "typeName", typeName));
}

public static OsmPrimitiveType from(OsmPrimitive obj) {
return from(obj.getClass());
}

public static OsmPrimitiveType from(Class<? extends OsmPrimitive> cls) {
if (cls.equals(Node.class)) return NODE;
if (cls.equals(Way.class)) return WAY;
if (cls.equals(Relation.class)) return RELATION;
throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' is not an acceptable class. Got ''{1}''.", "cls", cls.toString()));
}

public static OsmPrimitiveType fromData(Class<? extends PrimitiveData> cls) {
if (cls.equals(NodeData.class)) return NODE;
if (cls.equals(WayData.class)) return WAY;
if (cls.equals(RelationData.class)) return RELATION;
throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' is not an acceptable class. Got ''{1}''.", "cls", cls.toString()));
}

public static OsmPrimitiveType fromData(PrimitiveData data) {
return fromData(data.getClass());
public static OsmPrimitiveType from(IPrimitive obj) {
if (obj instanceof INode) return NODE;
if (obj instanceof IWay) return WAY;
if (obj instanceof IRelation) return RELATION;
throw new IllegalArgumentException();
}

public static OsmPrimitiveType from(String value) {
Expand Down
5 changes: 5 additions & 0 deletions src/org/openstreetmap/josm/data/osm/Relation.java
Expand Up @@ -9,6 +9,7 @@
import java.util.Set;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
import org.openstreetmap.josm.data.osm.visitor.Visitor;
import org.openstreetmap.josm.tools.CopyList;

Expand Down Expand Up @@ -164,6 +165,10 @@ public OsmPrimitiveType getMemberType(int idx) {
visitor.visit(this);
}

@Override public void visit(PrimitiveVisitor visitor) {
visitor.visit(this);
}

protected Relation(long id, boolean allowNegative) {
super(id, allowNegative);
}
Expand Down
12 changes: 12 additions & 0 deletions src/org/openstreetmap/josm/data/osm/RelationData.java
Expand Up @@ -3,6 +3,7 @@

import java.util.ArrayList;
import java.util.List;
import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;

public class RelationData extends PrimitiveData implements IRelation {

Expand Down Expand Up @@ -59,4 +60,15 @@ public String toString() {
public OsmPrimitiveType getType() {
return OsmPrimitiveType.RELATION;
}

@Override
public void visit(PrimitiveVisitor visitor) {
visitor.visit(this);
}

@Override
public String getDisplayName(NameFormatter formatter) {
return formatter.format(this);
}

}
6 changes: 6 additions & 0 deletions src/org/openstreetmap/josm/data/osm/Way.java
Expand Up @@ -9,6 +9,7 @@
import java.util.Set;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
import org.openstreetmap.josm.data.osm.visitor.Visitor;
import org.openstreetmap.josm.tools.CopyList;
import org.openstreetmap.josm.tools.Pair;
Expand Down Expand Up @@ -159,6 +160,10 @@ public List<Pair<Node,Node>> getNodePairs(boolean sort) {
@Override public void visit(Visitor visitor) {
visitor.visit(this);
}

@Override public void visit(PrimitiveVisitor visitor) {
visitor.visit(this);
}

protected Way(long id, boolean allowNegative) {
super(id, allowNegative);
Expand Down Expand Up @@ -407,6 +412,7 @@ public void setDeleted(boolean deleted) {
}
}

@Override
public boolean isClosed() {
if (isIncomplete()) return false;

Expand Down
18 changes: 18 additions & 0 deletions src/org/openstreetmap/josm/data/osm/WayData.java
Expand Up @@ -3,6 +3,7 @@

import java.util.ArrayList;
import java.util.List;
import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;

public class WayData extends PrimitiveData implements IWay {

Expand Down Expand Up @@ -31,6 +32,12 @@ public long getNodeId(int idx) {
return nodes.get(idx);
}

@Override
public boolean isClosed() {
if (isIncomplete()) return false;
return nodes.get(0).equals(nodes.get(nodes.size() - 1));
}

public void setNodes(List<Long> nodes) {
this.nodes = new ArrayList<Long>(nodes);
}
Expand All @@ -49,4 +56,15 @@ public String toString() {
public OsmPrimitiveType getType() {
return OsmPrimitiveType.WAY;
}

@Override
public void visit(PrimitiveVisitor visitor) {
visitor.visit(this);
}

@Override
public String getDisplayName(NameFormatter formatter) {
return formatter.format(this);
}

}

0 comments on commit 7b1d245

Please sign in to comment.