Changeset 4100 in josm


Ignore:
Timestamp:
2011-05-29T21:04:54+02:00 (8 years ago)
Author:
bastiK
Message:

use IPrimitive to make upload code work for both OsmPrimitive and PrimitiveData

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
22 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/APIDataSet.java

    r3530 r4100  
    2424import org.openstreetmap.josm.data.osm.RelationMember;
    2525import org.openstreetmap.josm.data.osm.Way;
     26import org.openstreetmap.josm.tools.Utils;
    2627
    2728/**
     
    286287    public void adjustRelationUploadOrder() throws CyclicUploadDependencyException{
    287288        LinkedList<OsmPrimitive> newToAdd = new LinkedList<OsmPrimitive>();
    288         newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Node.class));
    289         newToAdd.addAll(OsmPrimitive.getFilteredList(toAdd, Way.class));
    290 
    291         List<Relation> relationsToAdd = OsmPrimitive.getFilteredList(toAdd, Relation.class);
     289        newToAdd.addAll(Utils.filteredCollection(toAdd, Node.class));
     290        newToAdd.addAll(Utils.filteredCollection(toAdd, Way.class));
     291
     292        List<Relation> relationsToAdd = new ArrayList<Relation>(Utils.filteredCollection(toAdd, Relation.class));
    292293        List<Relation> noProblemRelations = filterRelationsNotReferringToNewRelations(relationsToAdd);
    293294        newToAdd.addAll(noProblemRelations);
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r4099 r4100  
    1010import java.util.HashMap;
    1111import java.util.HashSet;
     12import java.util.Locale;
    1213import java.util.Map;
    1314import java.util.Map.Entry;
     
    163164     * @see #isUndeleted()
    164165     */
     166    @Override
    165167    public boolean isNewOrUndeleted() {
    166168        return (id <= 0) || ((flags & (FLAG_VISIBLE + FLAG_DELETED)) == 0);
     
    261263     * @return the unique primitive id for this primitive
    262264     */
     265    @Override
    263266    public PrimitiveId getPrimitiveId() {
    264267        return new SimplePrimitiveId(getUniqueId(), getType());
     
    638641    abstract protected void keysChangedImpl(Map<String, String> originalKeys);
    639642   
     643    /**
     644     * Replies the name of this primitive. The default implementation replies the value
     645     * of the tag <tt>name</tt> or null, if this tag is not present.
     646     *
     647     * @return the name of this primitive
     648     */
     649    @Override
     650    public String getName() {
     651        return get("name");
     652    }
     653
     654    /**
     655     * Replies the a localized name for this primitive given by the value of the tags (in this order)
     656     * <ul>
     657     *   <li>name:lang_COUNTRY_Variant  of the current locale</li>
     658     *   <li>name:lang_COUNTRY of the current locale</li>
     659     *   <li>name:lang of the current locale</li>
     660     *   <li>name of the current locale</li>
     661     * </ul>
     662     *
     663     * null, if no such tag exists
     664     *
     665     * @return the name of this primitive
     666     */
     667    @Override
     668    public String getLocalName() {
     669        String key = "name:" + Locale.getDefault().toString();
     670        if (get(key) != null)
     671            return get(key);
     672        key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
     673        if (get(key) != null)
     674            return get(key);
     675        key = "name:" + Locale.getDefault().getLanguage();
     676        if (get(key) != null)
     677            return get(key);
     678        return getName();
     679    }
     680   
     681    /**
     682     * Replies the display name of a primitive formatted by <code>formatter</code>
     683     *
     684     * @return the display name
     685     */
     686    @Override
     687    public abstract String getDisplayName(NameFormatter formatter);
     688
    640689}
  • trunk/src/org/openstreetmap/josm/data/osm/IPrimitive.java

    r4098 r4100  
    33
    44import java.util.Date;
     5
     6import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    57
    68/**
     
    1618    void setDeleted(boolean deleted);
    1719    boolean isIncomplete();
     20    boolean isNewOrUndeleted();
    1821    long getId();
     22    PrimitiveId getPrimitiveId();
    1923    int getVersion();
    2024    void setOsmId(long id, int version);
     
    2630    int getChangesetId();
    2731    void setChangesetId(int changesetId);
     32   
     33    void visit(PrimitiveVisitor visitor);
     34    String getName();
     35    String getLocalName();
     36    String getDisplayName(NameFormatter formatter);
    2837
    2938}
  • trunk/src/org/openstreetmap/josm/data/osm/IWay.java

    r4098 r4100  
    66    int getNodesCount();
    77    long getNodeId(int idx);
    8    
     8    boolean isClosed();
     9
    910}
  • trunk/src/org/openstreetmap/josm/data/osm/NameFormatter.java

    r4070 r4100  
    55
    66public interface NameFormatter {
    7     String format(Node node);
    8     String format(Way way);
    9     String format(Relation relation);
     7    String format(INode node);
     8    String format(IWay way);
     9    String format(IRelation relation);
    1010    String format(Changeset changeset);
    1111
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r4098 r4100  
    55import org.openstreetmap.josm.data.coor.EastNorth;
    66import org.openstreetmap.josm.data.coor.LatLon;
     7import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    78import org.openstreetmap.josm.data.osm.visitor.Visitor;
    89
     
    138139
    139140    @Override public void visit(Visitor visitor) {
     141        visitor.visit(this);
     142    }
     143
     144    @Override public void visit(PrimitiveVisitor visitor) {
    140145        visitor.visit(this);
    141146    }
  • trunk/src/org/openstreetmap/josm/data/osm/NodeData.java

    r4099 r4100  
    55import org.openstreetmap.josm.data.coor.EastNorth;
    66import org.openstreetmap.josm.data.coor.LatLon;
     7import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    78
    89public class NodeData extends PrimitiveData implements INode {
     
    5354        return OsmPrimitiveType.NODE;
    5455    }
     56   
     57    @Override
     58    public void visit(PrimitiveVisitor visitor) {
     59        visitor.visit(this);
     60    }
     61
     62    @Override
     63    public String getDisplayName(NameFormatter formatter) {
     64        return formatter.format(this);
     65    }
     66
    5567}
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r4099 r4100  
    1414import java.util.LinkedList;
    1515import java.util.List;
    16 import java.util.Locale;
    1716import java.util.Map;
    1817import java.util.Set;
     
    10131012
    10141013    /**
    1015      * Replies the name of this primitive. The default implementation replies the value
    1016      * of the tag <tt>name</tt> or null, if this tag is not present.
    1017      *
    1018      * @return the name of this primitive
    1019      */
    1020     public String getName() {
    1021         return get("name");
    1022     }
    1023 
    1024     /**
    1025      * Replies the a localized name for this primitive given by the value of the tags (in this order)
    1026      * <ul>
    1027      *   <li>name:lang_COUNTRY_Variant  of the current locale</li>
    1028      *   <li>name:lang_COUNTRY of the current locale</li>
    1029      *   <li>name:lang of the current locale</li>
    1030      *   <li>name of the current locale</li>
    1031      * </ul>
    1032      *
    1033      * null, if no such tag exists
    1034      *
    1035      * @return the name of this primitive
    1036      */
    1037     public String getLocalName() {
    1038         String key = "name:" + Locale.getDefault().toString();
    1039         if (get(key) != null)
    1040             return get(key);
    1041         key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
    1042         if (get(key) != null)
    1043             return get(key);
    1044         key = "name:" + Locale.getDefault().getLanguage();
    1045         if (get(key) != null)
    1046             return get(key);
    1047         return getName();
    1048     }
    1049 
    1050     /**
    1051      * Replies the display name of a primitive formatted by <code>formatter</code>
    1052      *
    1053      * @return the display name
    1054      */
    1055     public abstract String getDisplayName(NameFormatter formatter);
    1056 
    1057     /**
    10581014     * Loads (clone) this primitive from provided PrimitiveData
    10591015     * @param data
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveType.java

    r3905 r4100  
    4545    }
    4646
    47     public static OsmPrimitiveType from(OsmPrimitive obj) {
    48         return from(obj.getClass());
    49     }
    50 
    51     public static OsmPrimitiveType from(Class<? extends OsmPrimitive> cls) {
    52         if (cls.equals(Node.class)) return NODE;
    53         if (cls.equals(Way.class)) return WAY;
    54         if (cls.equals(Relation.class)) return RELATION;
    55         throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' is not an acceptable class. Got ''{1}''.", "cls", cls.toString()));
    56     }
    57 
    58     public static OsmPrimitiveType fromData(Class<? extends PrimitiveData> cls) {
    59         if (cls.equals(NodeData.class)) return NODE;
    60         if (cls.equals(WayData.class)) return WAY;
    61         if (cls.equals(RelationData.class)) return RELATION;
    62         throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' is not an acceptable class. Got ''{1}''.", "cls", cls.toString()));
    63     }
    64 
    65     public static OsmPrimitiveType fromData(PrimitiveData data) {
    66         return fromData(data.getClass());
     47    public static OsmPrimitiveType from(IPrimitive obj) {
     48        if (obj instanceof INode) return NODE;
     49        if (obj instanceof IWay) return WAY;
     50        if (obj instanceof IRelation) return RELATION;
     51        throw new IllegalArgumentException();
    6752    }
    6853
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r4098 r4100  
    1010
    1111import org.openstreetmap.josm.Main;
     12import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    1213import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1314import org.openstreetmap.josm.tools.CopyList;
     
    162163
    163164    @Override public void visit(Visitor visitor) {
     165        visitor.visit(this);
     166    }
     167
     168    @Override public void visit(PrimitiveVisitor visitor) {
    164169        visitor.visit(this);
    165170    }
  • trunk/src/org/openstreetmap/josm/data/osm/RelationData.java

    r4098 r4100  
    44import java.util.ArrayList;
    55import java.util.List;
     6
     7import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    68
    79public class RelationData extends PrimitiveData implements IRelation {
     
    6062        return OsmPrimitiveType.RELATION;
    6163    }
     64   
     65    @Override
     66    public void visit(PrimitiveVisitor visitor) {
     67        visitor.visit(this);
     68    }
     69
     70    @Override
     71    public String getDisplayName(NameFormatter formatter) {
     72        return formatter.format(this);
     73    }
     74
    6275}
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r4098 r4100  
    1010
    1111import org.openstreetmap.josm.Main;
     12import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    1213import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1314import org.openstreetmap.josm.tools.CopyList;
     
    160161        visitor.visit(this);
    161162    }
     163   
     164    @Override public void visit(PrimitiveVisitor visitor) {
     165        visitor.visit(this);
     166    }
    162167
    163168    protected Way(long id, boolean allowNegative) {
     
    409414    }
    410415
     416    @Override
    411417    public boolean isClosed() {
    412418        if (isIncomplete()) return false;
  • trunk/src/org/openstreetmap/josm/data/osm/WayData.java

    r4098 r4100  
    44import java.util.ArrayList;
    55import java.util.List;
     6import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    67
    78public class WayData extends PrimitiveData implements IWay {
     
    3233    }
    3334
     35    @Override
     36    public boolean isClosed() {
     37        if (isIncomplete()) return false;
     38        return nodes.get(0).equals(nodes.get(nodes.size() - 1));
     39    }
     40
    3441    public void setNodes(List<Long> nodes) {
    3542        this.nodes = new ArrayList<Long>(nodes);
     
    5057        return OsmPrimitiveType.WAY;
    5158    }
     59   
     60    @Override
     61    public void visit(PrimitiveVisitor visitor) {
     62        visitor.visit(this);
     63    }
     64
     65    @Override
     66    public String getDisplayName(NameFormatter formatter) {
     67        return formatter.format(this);
     68    }
     69
    5270}
  • trunk/src/org/openstreetmap/josm/gui/DefaultNameFormatter.java

    r4070 r4100  
    1717import org.openstreetmap.josm.data.coor.CoordinateFormat;
    1818import org.openstreetmap.josm.data.osm.Changeset;
     19import org.openstreetmap.josm.data.osm.INode;
     20import org.openstreetmap.josm.data.osm.IPrimitive;
     21import org.openstreetmap.josm.data.osm.IRelation;
     22import org.openstreetmap.josm.data.osm.IWay;
    1923import org.openstreetmap.josm.data.osm.NameFormatter;
    2024import org.openstreetmap.josm.data.osm.Node;
     
    8387     * @return the decorated name
    8488     */
    85     protected String decorateNameWithId(String name, OsmPrimitive primitive) {
     89    protected String decorateNameWithId(String name, IPrimitive primitive) {
    8690        if (Main.pref.getBoolean("osm-primitives.showid"))
    8791            if (Main.pref.getBoolean("osm-primitives.showid.new-primitives"))
     
    99103     * @return the name
    100104     */
    101     public String format(Node node) {
     105    public String format(INode node) {
    102106        String name = "";
    103107        if (node.isIncomplete()) {
     
    157161     * @return the name
    158162     */
    159     public String format(Way way) {
     163    public String format(IWay way) {
    160164        String name = "";
    161165        if (way.isIncomplete()) {
     
    233237     * @return the name
    234238     */
    235     public String format(Relation relation) {
     239    public String format(IRelation relation) {
    236240        String name;
    237241        if (relation.isIncomplete()) {
     
    250254            name += trn("{0} member", "{0} members", mbno, mbno);
    251255
    252             if (relationHasIncompleteMember(relation)) {
    253                 name += ", "+tr("incomplete");
     256            if (relation instanceof Relation) {
     257                if (relationHasIncompleteMember((Relation) relation)) {
     258                    name += ", "+tr("incomplete");
     259                }
    254260            }
    255261
     
    325331    }
    326332
    327     private String getRelationTypeName(Relation relation) {
     333    private String getRelationTypeName(IRelation relation) {
    328334        String name = trc("Relation type", relation.get("type"));
    329335        if (name == null) {
     
    353359    }
    354360
    355     private String getNameTagValue(Relation relation, String nameTag) {
     361    private String getNameTagValue(IRelation relation, String nameTag) {
    356362        if (nameTag.equals("name")) {
    357363            if (Main.pref.getBoolean("osm-primitives.localize-name", true))
     
    369375    }
    370376
    371     private String getRelationName(Relation relation) {
     377    private String getRelationName(IRelation relation) {
    372378        String nameTag = null;
    373379        for (String n : getNamingtagsForRelations()) {
     
    403409     * @return the tooltip text
    404410     */
    405     public String buildDefaultToolTip(OsmPrimitive primitive) {
     411    public String buildDefaultToolTip(IPrimitive primitive) {
    406412        StringBuilder sb = new StringBuilder();
    407413        sb.append("<html>");
  • trunk/src/org/openstreetmap/josm/gui/io/UploadLayerTask.java

    r3995 r4100  
    9393            //
    9494            System.out.println(tr("Warning: object ''{0}'' is already deleted on the server. Skipping this object and retrying to upload.", p.getDisplayName(DefaultNameFormatter.getInstance())));
    95             processedPrimitives.addAll(writer.getProcessedPrimitives());
     95            processedPrimitives.addAll((Collection) writer.getProcessedPrimitives());
    9696            processedPrimitives.add(p);
    9797            toUpload.removeAll(processedPrimitives);
     
    124124                    if (isCancelled()) return;
    125125                    writer.uploadOsm(strategy, toUpload, changeset, m);
    126                     processedPrimitives.addAll(writer.getProcessedPrimitives());
     126                    processedPrimitives.addAll((Collection) writer.getProcessedPrimitives()); // OsmPrimitive in => OsmPrimitive out
    127127                    break;
    128128                } catch(OsmApiPrimitiveGoneException e) {
  • trunk/src/org/openstreetmap/josm/gui/io/UploadPrimitivesTask.java

    r3757 r4100  
    99import java.io.IOException;
    1010import java.lang.reflect.InvocationTargetException;
     11import java.util.Collection;
    1112import java.util.HashSet;
    1213import java.util.logging.Logger;
     
    194195            System.out.println(tr("Warning: object ''{0}'' is already deleted on the server. Skipping this object and retrying to upload.", p.getDisplayName(DefaultNameFormatter.getInstance())));
    195196            monitor.appendLogMessage(tr("Object ''{0}'' is already deleted. Skipping object in upload.",p.getDisplayName(DefaultNameFormatter.getInstance())));
    196             processedPrimitives.addAll(writer.getProcessedPrimitives());
     197            processedPrimitives.addAll((Collection) writer.getProcessedPrimitives());
    197198            processedPrimitives.add(p);
    198199            toUpload.removeProcessed(processedPrimitives);
     
    248249                    recoverFromGoneOnServer(e, getProgressMonitor());
    249250                } catch(ChangesetClosedException e) {
    250                     processedPrimitives.addAll(writer.getProcessedPrimitives());
     251                    processedPrimitives.addAll((Collection) writer.getProcessedPrimitives()); // OsmPrimitive in => OsmPrimitive out
    251252                    changeset.setOpen(false);
    252253                    switch(e.getSource()) {
     
    273274                } finally {
    274275                    if (writer != null) {
    275                         processedPrimitives.addAll(writer.getProcessedPrimitives());
     276                        processedPrimitives.addAll((Collection) writer.getProcessedPrimitives());
    276277                    }
    277278                    synchronized(this) {
  • trunk/src/org/openstreetmap/josm/io/DiffResultProcessor.java

    r3422 r4100  
    1717
    1818import org.openstreetmap.josm.data.osm.Changeset;
    19 import org.openstreetmap.josm.data.osm.OsmPrimitive;
     19import org.openstreetmap.josm.data.osm.IPrimitive;
    2020import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2121import org.openstreetmap.josm.data.osm.PrimitiveId;
     
    4646     * is set
    4747     */
    48     private Set<OsmPrimitive> processed;
     48    private Set<IPrimitive> processed;
    4949    /**
    5050     * the collection of primitives being uploaded
    5151     */
    52     private Collection<OsmPrimitive> primitives;
     52    private Collection<? extends IPrimitive> primitives;
    5353
    5454    /**
     
    5858     * assumes an empty collection.
    5959     */
    60     public DiffResultProcessor(Collection<OsmPrimitive> primitives) {
     60    public DiffResultProcessor(Collection<? extends IPrimitive> primitives) {
    6161        if (primitives == null) {
    6262            primitives = Collections.emptyList();
    6363        }
    6464        this.primitives = primitives;
    65         this.processed = new HashSet<OsmPrimitive>();
     65        this.processed = new HashSet<IPrimitive>();
    6666    }
    6767
     
    108108     * @return the collection of processed primitives
    109109     */
    110     protected Set<OsmPrimitive> postProcess(Changeset cs,ProgressMonitor monitor) {
     110    protected Set<IPrimitive> postProcess(Changeset cs, ProgressMonitor monitor) {
    111111        if (monitor == null) {
    112112            monitor = NullProgressMonitor.INSTANCE;
     
    116116            monitor.setTicksCount(primitives.size());
    117117            monitor.setTicks(0);
    118             for (OsmPrimitive p: primitives) {
     118            for (IPrimitive p : primitives) {
    119119                monitor.worked(1);
    120120                DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r3993 r4100  
    3030import org.openstreetmap.josm.Main;
    3131import org.openstreetmap.josm.data.osm.Changeset;
    32 import org.openstreetmap.josm.data.osm.OsmPrimitive;
     32import org.openstreetmap.josm.data.osm.IPrimitive;
    3333import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    3434import org.openstreetmap.josm.gui.layer.Layer;
     
    230230     * @return XML string
    231231     */
    232     private String toXml(OsmPrimitive o, boolean addBody) {
     232    private String toXml(IPrimitive o, boolean addBody) {
    233233        swriter.getBuffer().setLength(0);
    234234        osmWriter.setWithBody(addBody);
     
    250250        swriter.getBuffer().setLength(0);
    251251        osmWriter.header();
    252         s.visit(osmWriter);
     252        osmWriter.visit(s);
    253253        osmWriter.footer();
    254254        osmWriter.out.flush();
     
    280280     * @throws OsmTransferException if something goes wrong
    281281     */
    282     public void createPrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
     282    public void createPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
    283283        String ret = "";
    284284        try {
     
    300300     * @throws OsmTransferException if something goes wrong
    301301     */
    302     public void modifyPrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
     302    public void modifyPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
    303303        String ret = null;
    304304        try {
     
    320320     * @throws OsmTransferException if something goes wrong
    321321     */
    322     public void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
     322    public void deletePrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException {
    323323        ensureValidChangeset();
    324324        initialize(monitor);
     
    440440     * @throws OsmTransferException if something is wrong
    441441     */
    442     public Collection<OsmPrimitive> uploadDiff(Collection<OsmPrimitive> list, ProgressMonitor monitor) throws OsmTransferException {
     442    public Collection<IPrimitive> uploadDiff(Collection<? extends IPrimitive> list, ProgressMonitor monitor) throws OsmTransferException {
    443443        try {
    444444            monitor.beginTask("", list.size() * 2);
  • trunk/src/org/openstreetmap/josm/io/OsmChangeBuilder.java

    r3366 r4100  
    99
    1010import org.openstreetmap.josm.data.osm.Changeset;
    11 import org.openstreetmap.josm.data.osm.OsmPrimitive;
     11import org.openstreetmap.josm.data.osm.IPrimitive;
    1212
    1313/**
     
    3838    }
    3939
    40     protected void write(OsmPrimitive p) {
     40    protected void write(IPrimitive p) {
    4141        if (p.isDeleted()) {
    4242            switchMode("delete");
     
    8181
    8282    /**
    83      * Appends a collection of {@see OsmPrimitive}s to the OsmChange document.
     83     * Appends a collection of Primitives to the OsmChange document.
    8484     *
    8585     * @param primitives the collection of primitives. Ignored if null.
    8686     * @throws IllegalStateException thrown if the prologs has not been written yet
    8787     * @see #start()
    88      * @see #append(OsmPrimitive)
     88     * @see #append(IPrimitive)
    8989     */
    90     public void append(Collection<OsmPrimitive> primitives) throws IllegalStateException{
     90    public void append(Collection<? extends IPrimitive> primitives) throws IllegalStateException{
    9191        if (primitives == null) return;
    9292        if (!prologWritten)
    9393            throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write frst."));
    94         for (OsmPrimitive p : primitives) {
     94        for (IPrimitive p : primitives) {
    9595            write(p);
    9696        }
     
    9898
    9999    /**
    100      * Appends an {@see OsmPrimitive} to the OsmChange document.
     100     * Appends an Primitive to the OsmChange document.
    101101     *
    102102     * @param p the primitive. Ignored if null.
     
    106106
    107107     */
    108     public void append(OsmPrimitive p) {
     108    public void append(IPrimitive p) {
    109109        if (p == null) return;
    110110        if (!prologWritten)
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r2990 r4100  
    1414
    1515import org.openstreetmap.josm.data.osm.Changeset;
    16 import org.openstreetmap.josm.data.osm.OsmPrimitive;
     16import org.openstreetmap.josm.data.osm.IPrimitive;
    1717import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1818import org.openstreetmap.josm.gui.io.UploadStrategySpecification;
     
    3939     * than where passed in the list to upload*.
    4040     */
    41     private Collection<OsmPrimitive> processed;
     41    private Collection<IPrimitive> processed;
    4242
    4343    private OsmApi api = OsmApi.getOsmApi();
     
    7676     * @throws OsmTransferException thrown if an exception occurs
    7777     */
    78     protected void uploadChangesIndividually(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
     78    protected void uploadChangesIndividually(Collection<? extends IPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
    7979        try {
    8080            progressMonitor.beginTask(tr("Starting to upload with one request per primitive ..."));
    8181            progressMonitor.setTicksCount(primitives.size());
    8282            uploadStartTime = System.currentTimeMillis();
    83             for (OsmPrimitive osm : primitives) {
     83            for (IPrimitive osm : primitives) {
    8484                int progress = progressMonitor.getTicks();
    8585                String time_left_str = timeLeft(progress, primitives.size());
     
    118118     * @throws OsmTransferException thrown if an exception occurs
    119119     */
    120     protected void uploadChangesAsDiffUpload(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
     120    protected void uploadChangesAsDiffUpload(Collection<? extends IPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
    121121        try {
    122122            progressMonitor.beginTask(tr("Starting to upload in one request ..."));
     
    138138     * @throws OsmTransferException thrown if an exception occurs
    139139     */
    140     protected void uploadChangesInChunks(Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor, int chunkSize) throws OsmTransferException, IllegalArgumentException {
     140    protected void uploadChangesInChunks(Collection<? extends IPrimitive> primitives, ProgressMonitor progressMonitor, int chunkSize) throws OsmTransferException, IllegalArgumentException {
    141141        if (chunkSize <=0)
    142142            throw new IllegalArgumentException(tr("Value >0 expected for parameter ''{0}'', got {1}", "chunkSize", chunkSize));
    143143        try {
    144144            progressMonitor.beginTask(tr("Starting to upload in chunks..."));
    145             List<OsmPrimitive> chunk = new ArrayList<OsmPrimitive>(chunkSize);
    146             Iterator<OsmPrimitive> it = primitives.iterator();
     145            List<IPrimitive> chunk = new ArrayList<IPrimitive>(chunkSize);
     146            Iterator<? extends IPrimitive> it = primitives.iterator();
    147147            int numChunks = (int)Math.ceil((double)primitives.size() / (double)chunkSize);
    148148            int i= 0;
     
    181181     * @throws OsmTransferException thrown if something goes wrong
    182182     */
    183     public void uploadOsm(UploadStrategySpecification strategy, Collection<OsmPrimitive> primitives, Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
     183    public void uploadOsm(UploadStrategySpecification strategy, Collection<? extends IPrimitive> primitives, Changeset changeset, ProgressMonitor monitor) throws OsmTransferException {
    184184        CheckParameterUtil.ensureParameterNotNull(changeset, "changeset");
    185         processed = new LinkedList<OsmPrimitive>();
     185        processed = new LinkedList<IPrimitive>();
    186186        monitor = monitor == null ? NullProgressMonitor.INSTANCE : monitor;
    187187        monitor.beginTask(tr("Uploading data ..."));
     
    214214    }
    215215
    216     void makeApiRequest(OsmPrimitive osm, ProgressMonitor progressMonitor) throws OsmTransferException {
     216    void makeApiRequest(IPrimitive osm, ProgressMonitor progressMonitor) throws OsmTransferException {
    217217        if (osm.isDeleted()) {
    218218            api.deletePrimitive(osm, progressMonitor);
     
    236236     * @return the collection of successfully processed primitives
    237237     */
    238     public Collection<OsmPrimitive> getProcessedPrimitives() {
     238    public Collection<IPrimitive> getProcessedPrimitives() {
    239239        return processed;
    240240    }
  • trunk/src/org/openstreetmap/josm/io/OsmWriter.java

    r3336 r4100  
    1616import org.openstreetmap.josm.data.osm.DataSet;
    1717import org.openstreetmap.josm.data.osm.DataSource;
     18import org.openstreetmap.josm.data.osm.INode;
     19import org.openstreetmap.josm.data.osm.IPrimitive;
     20import org.openstreetmap.josm.data.osm.IRelation;
     21import org.openstreetmap.josm.data.osm.IWay;
    1822import org.openstreetmap.josm.data.osm.Node;
    1923import org.openstreetmap.josm.data.osm.OsmPrimitive;
    20 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2124import org.openstreetmap.josm.data.osm.Relation;
    22 import org.openstreetmap.josm.data.osm.RelationMember;
    2325import org.openstreetmap.josm.data.osm.Tagged;
    2426import org.openstreetmap.josm.data.osm.Way;
    25 import org.openstreetmap.josm.data.osm.visitor.Visitor;
     27import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
    2628import org.openstreetmap.josm.tools.DateUtils;
    2729
     
    3133 * @author imi
    3234 */
    33 public class OsmWriter extends XmlWriter implements Visitor {
     35public class OsmWriter extends XmlWriter implements PrimitiveVisitor {
    3436
    3537    public static final String DEFAULT_API_VERSION = "0.6";
     
    6769
    6870    private static final Comparator<OsmPrimitive> byIdComparator = new Comparator<OsmPrimitive>() {
    69         public int compare(OsmPrimitive o1, OsmPrimitive o2) {
     71        @Override public int compare(OsmPrimitive o1, OsmPrimitive o2) {
    7072            return (o1.getUniqueId()<o2.getUniqueId() ? -1 : (o1.getUniqueId()==o2.getUniqueId() ? 0 : 1));
    7173        }
     
    112114    }
    113115
    114     public void visit(Node n) {
     116    @Override
     117    public void visit(INode n) {
    115118        if (n.isIncomplete()) return;
    116119        addCommon(n, "node");
     
    123126    }
    124127
    125     public void visit(Way w) {
     128    @Override
     129    public void visit(IWay w) {
    126130        if (w.isIncomplete()) return;
    127131        addCommon(w, "way");
     
    130134        } else {
    131135            out.println(">");
    132             for (Node n : w.getNodes()) {
    133                 out.println("    <nd ref='"+n.getUniqueId()+"' />");
     136            for (int i=0; i<w.getNodesCount(); ++i) {
     137                out.println("    <nd ref='"+w.getNodeId(i) +"' />");
    134138            }
    135139            addTags(w, "way", false);
     
    137141    }
    138142
    139     public void visit(Relation e) {
     143    @Override
     144    public void visit(IRelation e) {
    140145        if (e.isIncomplete()) return;
    141146        addCommon(e, "relation");
     
    144149        } else {
    145150            out.println(">");
    146             for (RelationMember em : e.getMembers()) {
     151            for (int i=0; i<e.getMembersCount(); ++i) {
    147152                out.print("    <member type='");
    148                 out.print(OsmPrimitiveType.from(em.getMember()).getAPIName());
    149                 out.println("' ref='"+em.getMember().getUniqueId()+"' role='" +
    150                         XmlWriter.encode(em.getRole()) + "' />");
     153                out.print(e.getMemberType(i));
     154                out.println("' ref='"+e.getMemberId(i)+"' role='" +
     155                        XmlWriter.encode(e.getRole(i)) + "' />");
    151156            }
    152157            addTags(e, "relation", false);
     
    181186
    182187    private static final Comparator<Entry<String, String>> byKeyComparator = new Comparator<Entry<String,String>>() {
    183         public int compare(Entry<String, String> o1, Entry<String, String> o2) {
     188        @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) {
    184189            return o1.getKey().compareTo(o2.getKey());
    185190        }
     
    211216     * id, action, user, and visible.
    212217     */
    213     private void addCommon(OsmPrimitive osm, String tagname) {
     218    private void addCommon(IPrimitive osm, String tagname) {
    214219        out.print("  <"+tagname);
    215220        if (osm.getUniqueId() != 0) {
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r4087 r4100  
    4747        }
    4848        return null;
     49    }
     50   
     51    /**
     52     * Filter a collection by (sub)class.
     53     * This is an efficient read-only implementation.
     54     */
     55    public static <S, T extends S> SubclassFilteredCollection<S, T> filteredCollection(Collection<S> collection, final Class<T> klass) {
     56        return new SubclassFilteredCollection<S, T>(collection, new Predicate<S>() {
     57            @Override
     58            public boolean evaluate(S o) {
     59                return klass.isInstance(o);
     60            }
     61        });
    4962    }
    5063
Note: See TracChangeset for help on using the changeset viewer.