Changeset 4098 in josm


Ignore:
Timestamp:
2011-05-29T19:45:59+02:00 (10 years ago)
Author:
bastiK
Message:

extract common functionality of OsmPrimitive & PrimitiveData to an interface

Location:
trunk/src/org/openstreetmap/josm/data/osm
Files:
4 added
8 edited

Legend:

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

    r3653 r4098  
    1212 * @author imi
    1313 */
    14 public final class Node extends OsmPrimitive {
     14public final class Node extends OsmPrimitive implements INode {
    1515
    1616    private CachedLatLon coor;
    1717
     18    @Override
    1819    public final void setCoor(LatLon coor) {
    1920        if(coor != null){
     
    2223    }
    2324
     25    @Override
    2426    public final void setEastNorth(EastNorth eastNorth) {
    2527        if(eastNorth != null) {
     
    4143    }
    4244
     45    @Override
    4346    public final LatLon getCoor() {
    4447        return coor;
    4548    }
    4649
     50    @Override
    4751    public final EastNorth getEastNorth() {
    4852        return coor != null ? coor.getEastNorth() : null;
     
    210214    }
    211215
     216    @Override
    212217    public int compareTo(OsmPrimitive o) {
    213218        return o instanceof Node ? Long.valueOf(getUniqueId()).compareTo(o.getUniqueId()) : 1;
     
    219224    }
    220225
     226    @Override
    221227    public OsmPrimitiveType getType() {
    222228        return OsmPrimitiveType.NODE;
  • trunk/src/org/openstreetmap/josm/data/osm/NodeData.java

    r3083 r4098  
    66import org.openstreetmap.josm.data.coor.LatLon;
    77
    8 public class NodeData extends PrimitiveData {
     8public class NodeData extends PrimitiveData implements INode {
    99
    1010    private final CachedLatLon coor = new CachedLatLon(0, 0);
     
    2828    }
    2929
     30    @Override
    3031    public LatLon getCoor() {
    3132        return coor;
    3233    }
    3334
     35    @Override
    3436    public void setCoor(LatLon coor) {
    3537        this.coor.setCoor(coor);
    3638    }
    3739
     40    @Override
    3841    public EastNorth getEastNorth() {
    3942        return this.coor.getEastNorth();
    4043    }
    4144
     45    @Override
    4246    public void setEastNorth(EastNorth eastNorth) {
    4347        this.coor.setEastNorth(eastNorth);
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r4074 r4098  
    4040 * @author imi
    4141 */
    42 abstract public class OsmPrimitive implements Comparable<OsmPrimitive>, Tagged, PrimitiveId {
     42abstract public class OsmPrimitive implements Comparable<OsmPrimitive>, IPrimitive {
    4343
    4444    private static final AtomicLong idCounter = new AtomicLong(0);
     
    188188     */
    189189    public static final Predicate<OsmPrimitive> isUsablePredicate = new Predicate<OsmPrimitive>() {
    190         public boolean evaluate(OsmPrimitive primitive) {
     190        @Override public boolean evaluate(OsmPrimitive primitive) {
    191191            return primitive.isUsable();
    192192        }
     
    194194
    195195    public static final Predicate<OsmPrimitive> isSelectablePredicate = new Predicate<OsmPrimitive>() {
    196         public boolean evaluate(OsmPrimitive primitive) {
     196        @Override public boolean evaluate(OsmPrimitive primitive) {
    197197            return primitive.isSelectable();
    198198        }
     
    200200
    201201    public static final Predicate<OsmPrimitive> nonDeletedPredicate = new Predicate<OsmPrimitive>() {
    202         public boolean evaluate(OsmPrimitive primitive) {
     202        @Override public boolean evaluate(OsmPrimitive primitive) {
    203203            return !primitive.isDeleted();
    204204        }
     
    206206
    207207    public static final Predicate<OsmPrimitive> nonDeletedCompletePredicate = new Predicate<OsmPrimitive>() {
    208         public boolean evaluate(OsmPrimitive primitive) {
     208        @Override public boolean evaluate(OsmPrimitive primitive) {
    209209            return !primitive.isDeleted() && !primitive.isIncomplete();
    210210        }
     
    212212
    213213    public static final Predicate<OsmPrimitive> nonDeletedPhysicalPredicate = new Predicate<OsmPrimitive>() {
    214         public boolean evaluate(OsmPrimitive primitive) {
     214        @Override public boolean evaluate(OsmPrimitive primitive) {
    215215            return !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation);
    216216        }
     
    218218
    219219    public static final Predicate<OsmPrimitive> modifiedPredicate = new Predicate<OsmPrimitive>() {
    220         public boolean evaluate(OsmPrimitive primitive) {
     220        @Override public boolean evaluate(OsmPrimitive primitive) {
    221221            return primitive.isModified();
    222222        }
     
    224224
    225225    public static final Predicate<OsmPrimitive> nodePredicate = new Predicate<OsmPrimitive>() {
    226         public boolean evaluate(OsmPrimitive primitive) {
     226        @Override public boolean evaluate(OsmPrimitive primitive) {
    227227            return primitive.getClass() == Node.class;
    228228        }
     
    230230
    231231    public static final Predicate<OsmPrimitive> wayPredicate = new Predicate<OsmPrimitive>() {
    232         public boolean evaluate(OsmPrimitive primitive) {
     232        @Override public boolean evaluate(OsmPrimitive primitive) {
    233233            return primitive.getClass() == Way.class;
    234234        }
     
    236236
    237237    public static final Predicate<OsmPrimitive> relationPredicate = new Predicate<OsmPrimitive>() {
    238         public boolean evaluate(OsmPrimitive primitive) {
     238        @Override public boolean evaluate(OsmPrimitive primitive) {
    239239            return primitive.getClass() == Relation.class;
    240240        }
     
    242242
    243243    public static final Predicate<OsmPrimitive> allPredicate = new Predicate<OsmPrimitive>() {
    244         public boolean evaluate(OsmPrimitive primitive) {
     244        @Override public boolean evaluate(OsmPrimitive primitive) {
    245245            return true;
    246246        }
     
    397397     * @see #setVersion(int)
    398398     */
    399     public long getVersion() {
     399    @Override
     400    public int getVersion() {
    400401        return version;
    401402    }
     
    406407     * @return the id of this primitive.
    407408     */
     409    @Override
    408410    public long getId() {
    409411        long id = this.id;
     
    415417     * @return Osm id if primitive already exists on the server. Unique negative value if primitive is new
    416418     */
     419    @Override
    417420    public long getUniqueId() {
    418421        return id;
     
    423426     * @return True if primitive is new (not yet uploaded the server, id <= 0)
    424427     */
     428    @Override
    425429    public boolean isNew() {
    426430        return id <= 0;
     
    449453     * @throws DataIntegrityProblemException If id is changed and primitive was already added to the dataset
    450454     */
     455    @Override
    451456    public void setOsmId(long id, int version) {
    452457        boolean locked = writeLock();
     
    496501     * @return the user who has last touched this object. May be null.
    497502     */
     503    @Override
    498504    public User getUser() {
    499505        return user;
     
    505511     * @param user the user
    506512     */
     513    @Override
    507514    public void setUser(User user) {
    508515        boolean locked = writeLock();
     
    521528     * @return the id of the changeset this primitive was last uploaded to.
    522529     */
     530    @Override
    523531    public int getChangesetId() {
    524532        return changesetId;
     
    533541     * @throws IllegalArgumentException thrown if id < 0
    534542     */
     543    @Override
    535544    public void setChangesetId(int changesetId) throws IllegalStateException, IllegalArgumentException {
    536545        boolean locked = writeLock();
     
    566575    }
    567576
     577    @Override
    568578    public void setTimestamp(Date timestamp) {
    569579        boolean locked = writeLock();
     
    581591     *
    582592     */
     593    @Override
    583594    public Date getTimestamp() {
    584595        return new Date(timestamp * 1000l);
    585596    }
    586597
     598    @Override
    587599    public boolean isTimestampEmpty() {
    588600        return timestamp == 0;
     
    674686     * @param modified true, if this primitive is to be modified
    675687     */
     688    @Override
    676689    public void setModified(boolean modified) {
    677690        updateFlags(FLAG_MODIFIED, modified);
     
    688701     * the server
    689702     */
     703    @Override
    690704    public boolean isModified() {
    691705        return (flags & FLAG_MODIFIED) != 0;
     
    698712     * @see #setDeleted(boolean)
    699713     */
     714    @Override
    700715    public boolean isDeleted() {
    701716        return (flags & FLAG_DELETED) != 0;
     
    737752     * @see #setVisible(boolean)
    738753     */
     754    @Override
    739755    public boolean isVisible() {
    740756        return (flags & FLAG_VISIBLE) != 0;
     
    749765     * id==0
    750766     */
     767    @Override
    751768    public void setVisible(boolean visible) throws IllegalStateException{
    752769        boolean locked = writeLock();
     
    767784     * @param deleted  true, if this primitive is deleted; false, otherwise
    768785     */
     786    @Override
    769787    public void setDeleted(boolean deleted) {
    770788        boolean locked = writeLock();
     
    805823    }
    806824
     825    @Override
    807826    public boolean isIncomplete() {
    808827        return (flags & FLAG_INCOMPLETE) != 0;
     
    9951014     * back to the primitive, use setKeys() to modify the keys
    9961015     */
     1016    @Override
    9971017    public Map<String, String> getKeys() {
    9981018        Map<String, String> result = new HashMap<String, String>();
     
    10121032     * @param keys the key/value pairs to set. If null, removes all existing key/value pairs.
    10131033     */
     1034    @Override
    10141035    public void setKeys(Map<String, String> keys) {
    10151036        boolean locked = writeLock();
     
    10431064     * @see #remove(String)
    10441065     */
     1066    @Override
    10451067    public final void put(String key, String value) {
    10461068        boolean locked = writeLock();
     
    10811103     * @param key  the key to be removed. Ignored, if key is null.
    10821104     */
     1105    @Override
    10831106    public final void remove(String key) {
    10841107        boolean locked = writeLock();
     
    11131136     * @since 1843
    11141137     */
     1138    @Override
    11151139    public final void removeAll() {
    11161140        boolean locked = writeLock();
     
    11331157     * @return the value for key <code>key</code>.
    11341158     */
     1159    @Override
    11351160    public final String get(String key) {
    11361161        String[] keys = this.keys;
     
    11451170    }
    11461171
     1172    @Override
    11471173    public final Collection<String> keySet() {
    11481174        String[] keys = this.keys;
     
    11621188     *   otherwise
    11631189     */
     1190    @Override
    11641191    public final boolean hasKeys() {
    11651192        return keys != null;
  • trunk/src/org/openstreetmap/josm/data/osm/PrimitiveData.java

    r3083 r4098  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.osm;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.util.ArrayList;
     
    1618 *
    1719 */
    18 public abstract class PrimitiveData implements Tagged, PrimitiveId {
     20public abstract class PrimitiveData implements IPrimitive {
    1921
    2022    // Useful?
     
    5153    private int changesetId;
    5254
     55    @Override
    5356    public boolean isModified() {
    5457        return modified;
    5558    }
     59    @Override
    5660    public void setModified(boolean modified) {
    5761        this.modified = modified;
    5862    }
     63    @Override
    5964    public boolean isVisible() {
    6065        return visible;
    6166    }
     67    @Override
    6268    public void setVisible(boolean visible) {
    6369        this.visible = visible;
    6470    }
     71    @Override
    6572    public boolean isDeleted() {
    6673        return deleted;
    6774    }
     75    @Override
    6876    public void setDeleted(boolean deleted) {
    6977        this.deleted = deleted;
    7078    }
     79    @Override
    7180    public long getId() {
    7281        return id > 0 ? id : 0;
     
    7584        this.id = id;
    7685    }
     86    @Override
    7787    public User getUser() {
    7888        return user;
    7989    }
     90    @Override
    8091    public void setUser(User user) {
    8192        this.user = user;
    8293    }
     94    @Override
    8395    public int getVersion() {
    8496        return version;
     
    8799        this.version = version;
    88100    }
     101    @Override
     102    public void setOsmId(long id, int version) {
     103        if (id <= 0)
     104            throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
     105        if (version <= 0)
     106            throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
     107        this.id = id;
     108        this.version = version;
     109        this.setIncomplete(false);
     110    }
     111    @Override
    89112    public Date getTimestamp() {
    90113        return timestamp;
    91114    }
     115    @Override
    92116    public void setTimestamp(Date timestamp) {
    93117        this.timestamp = timestamp;
    94118    }
    95 
     119    @Override
     120    public boolean isTimestampEmpty() {
     121        return timestamp == null || timestamp.getTime() == 0;
     122    }
     123
     124    @Override
    96125    public int getChangesetId() {
    97126        return changesetId;
    98127    }
    99128
     129    @Override
    100130    public void setChangesetId(int changesetId) {
    101131        this.changesetId = changesetId;
    102132    }
    103133
     134    @Override
    104135    public Map<String, String> getKeys() {
    105136        return keys;
    106137    }
     138    @Override
    107139    public boolean isIncomplete() {
    108140        return incomplete;
     
    141173    // Tagged implementation
    142174
     175    @Override
    143176    public String get(String key) {
    144177        return keys.get(key);
    145178    }
    146179
     180    @Override
    147181    public boolean hasKeys() {
    148182        return !keys.isEmpty();
    149183    }
    150184
     185    @Override
    151186    public Collection<String> keySet() {
    152187        return keys.keySet();
    153188    }
    154189
     190    @Override
    155191    public void put(String key, String value) {
    156192        keys.put(key, value);
    157193    }
    158194
     195    @Override
    159196    public void remove(String key) {
    160197        keys.remove(key);
    161198    }
    162199
     200    @Override
    163201    public void removeAll() {
    164202        keys.clear();
    165203    }
    166204
     205    @Override
    167206    public void setKeys(Map<String, String> keys) {
    168207        this.keys.clear();
     
    191230     * PrimitiveId implementation. Returns the same value as getId()
    192231     */
     232    @Override
    193233    public long getUniqueId() {
    194234        return id;
     
    204244    }
    205245
     246    @Override
    206247    public boolean isNew() {
    207248        return id <= 0;
    208249    }
    209250
     251    @Override
    210252    public abstract OsmPrimitiveType getType();
    211253}
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r3943 r4098  
    1818 * @author Frederik Ramm <frederik@remote.org>
    1919 */
    20 public final class Relation extends OsmPrimitive {
     20public final class Relation extends OsmPrimitive implements IRelation {
    2121
    2222    private RelationMember[] members = new RelationMember[0];
     
    6565     * @return number of members
    6666     */
     67    @Override
    6768    public int getMembersCount() {
    6869        return members.length;
     
    143144            writeUnlock(locked);
    144145        }
     146    }
     147
     148    @Override
     149    public long getMemberId(int idx) {
     150        return members[idx].getUniqueId();
     151    }
     152
     153    @Override
     154    public String getRole(int idx) {
     155        return members[idx].getRole();
     156    }
     157
     158    @Override
     159    public OsmPrimitiveType getMemberType(int idx) {
     160        return members[idx].getType();
    145161    }
    146162
     
    266282    }
    267283
     284    @Override
    268285    public int compareTo(OsmPrimitive o) {
    269286        return o instanceof Relation ? Long.valueOf(getUniqueId()).compareTo(o.getUniqueId()) : -1;
     
    373390    }
    374391
     392    @Override
    375393    public OsmPrimitiveType getType() {
    376394        return OsmPrimitiveType.RELATION;
    377395    }
    378396
     397    @Override
    379398    public OsmPrimitiveType getDisplayType() {
    380399        return isMultipolygon() ? OsmPrimitiveType.MULTIPOLYGON
  • trunk/src/org/openstreetmap/josm/data/osm/RelationData.java

    r3083 r4098  
    55import java.util.List;
    66
    7 public class RelationData extends PrimitiveData {
     7public class RelationData extends PrimitiveData implements IRelation {
    88
    99    private List<RelationMemberData> members = new ArrayList<RelationMemberData>();
     
    2727
    2828    @Override
     29    public int getMembersCount() {
     30        return members.size();
     31    }
     32
     33    @Override
     34    public long getMemberId(int idx) {
     35        return members.get(idx).getMemberId();
     36    }
     37
     38    @Override
     39    public String getRole(int idx) {
     40        return members.get(idx).getRole();
     41    }
     42
     43    @Override
     44    public OsmPrimitiveType getMemberType(int idx) {
     45        return members.get(idx).getMemberType();
     46    }
     47
     48    @Override
    2949    public RelationData makeCopy() {
    3050        return new RelationData(this);
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r4054 r4098  
    1919 * @author imi
    2020 */
    21 public final class Way extends OsmPrimitive {
     21public final class Way extends OsmPrimitive implements IWay {
    2222
    2323    /**
     
    9494     * @since 1862
    9595     */
     96    @Override
    9697    public int getNodesCount() {
    9798        return nodes.length;
     
    109110    public Node getNode(int index) {
    110111        return nodes[index];
     112    }
     113
     114    @Override
     115    public long getNodeId(int idx) {
     116        return nodes[idx].getUniqueId();
    111117    }
    112118
     
    275281    }
    276282
     283    @Override
    277284    public int compareTo(OsmPrimitive o) {
    278285        if (o instanceof Relation)
     
    444451    }
    445452
     453    @Override
    446454    public OsmPrimitiveType getType() {
    447455        return OsmPrimitiveType.WAY;
  • trunk/src/org/openstreetmap/josm/data/osm/WayData.java

    r3083 r4098  
    55import java.util.List;
    66
    7 public class WayData extends PrimitiveData {
     7public class WayData extends PrimitiveData implements IWay {
    88
    99    private List<Long> nodes = new ArrayList<Long>();
     
    2020    public List<Long> getNodes() {
    2121        return nodes;
     22    }
     23
     24    @Override
     25    public int getNodesCount() {
     26        return nodes.size();
     27    }
     28
     29    @Override
     30    public long getNodeId(int idx) {
     31        return nodes.get(idx);
    2232    }
    2333
Note: See TracChangeset for help on using the changeset viewer.