001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.osm.history; 003 004import java.text.MessageFormat; 005import java.util.ArrayList; 006import java.util.Collections; 007import java.util.Date; 008import java.util.List; 009 010import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 011import org.openstreetmap.josm.data.osm.Relation; 012import org.openstreetmap.josm.data.osm.RelationMemberData; 013import org.openstreetmap.josm.data.osm.User; 014import org.openstreetmap.josm.tools.CheckParameterUtil; 015 016/** 017 * Represents an immutable OSM relation in the context of a historical view on 018 * OSM data. 019 * 020 */ 021public class HistoryRelation extends HistoryOsmPrimitive{ 022 023 private List<RelationMemberData> members = new ArrayList<RelationMemberData>(); 024 025 /** 026 * constructor 027 * 028 * @param id the id (>0 required) 029 * @param version the version (> 0 required) 030 * @param visible whether the primitive is still visible 031 * @param user the user (! null required) 032 * @param changesetId the changeset id (> 0 required) 033 * @param timestamp the timestamp (! null required) 034 * 035 * @throws IllegalArgumentException if preconditions are violated 036 */ 037 public HistoryRelation(long id, long version, boolean visible, User user, long changesetId, Date timestamp) throws IllegalArgumentException { 038 super(id, version, visible, user, changesetId, timestamp); 039 } 040 041 /** 042 * constructor 043 * 044 * @param id the id (>0 required) 045 * @param version the version (> 0 required) 046 * @param visible whether the primitive is still visible 047 * @param user the user (! null required) 048 * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true) 049 * @param timestamp the timestamp (! null required if {@code checkHistoricParams} is true) 050 * @param checkHistoricParams If true, checks values of {@code changesetId} and {@code timestamp} 051 * 052 * @throws IllegalArgumentException if preconditions are violated 053 * @since 5440 054 */ 055 public HistoryRelation(long id, long version, boolean visible, User user, long changesetId, Date timestamp, boolean checkHistoricParams) throws IllegalArgumentException { 056 super(id, version, visible, user, changesetId, timestamp, checkHistoricParams); 057 } 058 059 /** 060 * constructor 061 * 062 * @param id the id (>0 required) 063 * @param version the version (> 0 required) 064 * @param visible whether the primitive is still visible 065 * @param user the user (! null required) 066 * @param changesetId the changeset id (> 0 required) 067 * @param timestamp the timestamp (! null required) 068 * @param members list of members for this relation 069 * 070 * @throws IllegalArgumentException thrown if preconditions are violated 071 */ 072 public HistoryRelation(long id, long version, boolean visible, User user, long changesetId, Date timestamp, List<RelationMemberData> members) { 073 this(id, version, visible, user, changesetId, timestamp); 074 if (members != null) { 075 this.members.addAll(members); 076 } 077 } 078 079 /** 080 * Constructs a new {@code HistoryRelation} from an existing {@link Relation}. 081 * @param r the relation 082 */ 083 public HistoryRelation(Relation r) { 084 super(r); 085 } 086 087 /** 088 * replies an immutable list of members of this relation 089 * 090 * @return an immutable list of members of this relation 091 */ 092 public List<RelationMemberData> getMembers() { 093 return Collections.unmodifiableList(members); 094 } 095 096 /** 097 * replies the number of members 098 * 099 * @return the number of members 100 * 101 */ 102 public int getNumMembers() { 103 return members.size(); 104 } 105 106 /** 107 * replies the idx-th member 108 * @param idx the index 109 * @return the idx-th member 110 * @throws IndexOutOfBoundsException thrown, if idx is out of bounds 111 */ 112 public RelationMemberData getRelationMember(int idx) throws IndexOutOfBoundsException { 113 if (idx < 0 || idx >= members.size()) 114 throw new IndexOutOfBoundsException(MessageFormat.format("Parameter {0} not in range 0..{1}. Got ''{2}''.", "idx", members.size(),idx)); 115 return members.get(idx); 116 } 117 118 /** 119 * replies the type, i.e. {@link OsmPrimitiveType#RELATION} 120 * 121 */ 122 @Override 123 public OsmPrimitiveType getType() { 124 return OsmPrimitiveType.RELATION; 125 } 126 127 /** 128 * adds a member to the list of members 129 * 130 * @param member the member (must not be null) 131 * @exception IllegalArgumentException thrown, if member is null 132 */ 133 public void addMember(RelationMemberData member) throws IllegalArgumentException { 134 CheckParameterUtil.ensureParameterNotNull(member, "member"); 135 members.add(member); 136 } 137 138 @Override 139 public String getDisplayName(HistoryNameFormatter formatter) { 140 return formatter.format(this); 141 } 142}