1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.tools.Utils;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * IRelation captures the common functions of {@link Relation} and {@link RelationData}.
|
---|
13 | * @param <M> Type of OSM relation member
|
---|
14 | * @since 4098
|
---|
15 | */
|
---|
16 | public interface IRelation<M extends IRelationMember<?>> extends IPrimitive {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Returns the number of members.
|
---|
20 | * @return number of members
|
---|
21 | */
|
---|
22 | int getMembersCount();
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Returns the relation member at the specified index.
|
---|
26 | * @param index the index of the relation member
|
---|
27 | * @return relation member at the specified index
|
---|
28 | * @since 13766 (IRelation)
|
---|
29 | */
|
---|
30 | M getMember(int index);
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Returns members of the relation.
|
---|
34 | * @return Members of the relation. Changes made in returned list are not mapped
|
---|
35 | * back to the primitive, use {@link #setMembers} to modify the members
|
---|
36 | * @since 1925
|
---|
37 | * @since 13766 (IRelation)
|
---|
38 | */
|
---|
39 | List<M> getMembers();
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Sets members of the relation.
|
---|
43 | * @param members Can be null, in that case all members are removed
|
---|
44 | */
|
---|
45 | void setMembers(List<M> members);
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Returns id of the member at given index.
|
---|
49 | * @param idx member index
|
---|
50 | * @return id of the member at given index
|
---|
51 | */
|
---|
52 | long getMemberId(int idx);
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns role of the member at given index.
|
---|
56 | * @param idx member index
|
---|
57 | * @return role of the member at given index
|
---|
58 | */
|
---|
59 | String getRole(int idx);
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Returns type of the member at given index.
|
---|
63 | * @param idx member index
|
---|
64 | * @return type of the member at given index
|
---|
65 | */
|
---|
66 | OsmPrimitiveType getMemberType(int idx);
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Determines if at least one child primitive is incomplete.
|
---|
70 | *
|
---|
71 | * @return true if at least one child primitive is incomplete
|
---|
72 | * @since 13564
|
---|
73 | */
|
---|
74 | default boolean hasIncompleteMembers() {
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | default int compareTo(IPrimitive o) {
|
---|
80 | return o instanceof IRelation ? Long.compare(getUniqueId(), o.getUniqueId()) : -1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | default String getDisplayName(NameFormatter formatter) {
|
---|
85 | return formatter.format(this);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Determines if this relation is a boundary.
|
---|
90 | * @return {@code true} if a boundary relation
|
---|
91 | */
|
---|
92 | default boolean isBoundary() {
|
---|
93 | return "boundary".equals(get("type"));
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | default boolean isMultipolygon() {
|
---|
98 | return "multipolygon".equals(get("type")) || isBoundary();
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Returns an unmodifiable list of the {@link OsmPrimitive}s referred to by at least one member of this relation.
|
---|
103 | * @return an unmodifiable list of the primitives
|
---|
104 | * @since 13957
|
---|
105 | */
|
---|
106 | default List<? extends IPrimitive> getMemberPrimitivesList() {
|
---|
107 | return Utils.transform(getMembers(), IRelationMember::getMember);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Replies a collection with the incomplete children this relation refers to.
|
---|
112 | *
|
---|
113 | * @return the incomplete children. Empty collection if no children are incomplete.
|
---|
114 | * @since 13957
|
---|
115 | */
|
---|
116 | default Collection<? extends IPrimitive> getIncompleteMembers() {
|
---|
117 | Set<IPrimitive> ret = new HashSet<>();
|
---|
118 | for (M rm : getMembers()) {
|
---|
119 | if (!rm.getMember().isIncomplete()) {
|
---|
120 | continue;
|
---|
121 | }
|
---|
122 | ret.add(rm.getMember());
|
---|
123 | }
|
---|
124 | return ret;
|
---|
125 | }
|
---|
126 | }
|
---|