source: josm/trunk/src/org/openstreetmap/josm/data/osm/RelationToChildReference.java@ 13795

Last change on this file since 13795 was 12190, checked in by michael2402, 7 years ago

See #14794: More javadoc for data.osm package

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.Collection;
5import java.util.HashSet;
6import java.util.Objects;
7import java.util.Set;
8
9/**
10 * This is an extension of {@link RelationMember} that stores the parent relation and the index in it in addition to the role/child.
11 */
12public class RelationToChildReference {
13
14 /**
15 * Replies a set of all {@link RelationToChildReference}s for a given child primitive.
16 *
17 * @param child the child primitive
18 * @return a set of all {@link RelationToChildReference}s for a given child primitive
19 */
20 public static Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
21 Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
22 Set<RelationToChildReference> references = new HashSet<>();
23 for (Relation parent: parents) {
24 for (int i = 0; i < parent.getMembersCount(); i++) {
25 if (parent.getMember(i).refersTo(child)) {
26 references.add(new RelationToChildReference(parent, i, parent.getMember(i)));
27 }
28 }
29 }
30 return references;
31 }
32
33 /**
34 * Replies a set of all {@link RelationToChildReference}s for a collection of child primitives
35 *
36 * @param children the collection of child primitives
37 * @return a set of all {@link RelationToChildReference}s to the children in the collection of child
38 * primitives
39 */
40 public static Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
41 Set<RelationToChildReference> references = new HashSet<>();
42 for (OsmPrimitive child: children) {
43 references.addAll(getRelationToChildReferences(child));
44 }
45 return references;
46 }
47
48 private final Relation parent;
49 private final int position;
50 private final String role;
51 private final OsmPrimitive child;
52
53 /**
54 * Create a new {@link RelationToChildReference}
55 * @param parent The parent relation
56 * @param position The position of the child in the parent
57 * @param role The role of the child
58 * @param child The actual child (member of parent)
59 */
60 public RelationToChildReference(Relation parent, int position, String role, OsmPrimitive child) {
61 this.parent = parent;
62 this.position = position;
63 this.role = role;
64 this.child = child;
65 }
66
67 /**
68 * Create a new {@link RelationToChildReference}
69 * @param parent The parent relation
70 * @param position The position of the child in the parent
71 * @param member The role and relation for the child
72 */
73 public RelationToChildReference(Relation parent, int position, RelationMember member) {
74 this(parent, position, member.getRole(), member.getMember());
75 }
76
77 /**
78 * Get the parent relation
79 * @return The parent
80 */
81 public Relation getParent() {
82 return parent;
83 }
84
85 /**
86 * Get the position of the child in the parent
87 * @return The position of the child
88 */
89 public int getPosition() {
90 return position;
91 }
92
93 /**
94 * Get the role of the child
95 * @return The role
96 */
97 public String getRole() {
98 return role;
99 }
100
101 /**
102 * Get the actual child
103 * @return The child
104 */
105 public OsmPrimitive getChild() {
106 return child;
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) return true;
112 if (obj == null || getClass() != obj.getClass()) return false;
113 RelationToChildReference that = (RelationToChildReference) obj;
114 return position == that.position &&
115 Objects.equals(parent, that.parent) &&
116 Objects.equals(role, that.role) &&
117 Objects.equals(child, that.child);
118 }
119
120 @Override
121 public int hashCode() {
122 return Objects.hash(parent, position, role, child);
123 }
124}
Note: See TracBrowser for help on using the repository browser.