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

Last change on this file since 7005 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

  • Property svn:eol-style set to native
File size: 3.7 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.Set;
7
8public class RelationToChildReference {
9
10 /**
11 * Replies a set of all {@link RelationToChildReference}s for a given child primitive.
12 *
13 * @param child the child primitive
14 * @return a set of all {@link RelationToChildReference}s for a given child primitive
15 */
16 public static Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
17 Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
18 Set<RelationToChildReference> references = new HashSet<>();
19 for (Relation parent: parents) {
20 for (int i=0; i < parent.getMembersCount(); i++) {
21 if (parent.getMember(i).refersTo(child)) {
22 references.add(new RelationToChildReference(parent, i, parent.getMember(i)));
23 }
24 }
25 }
26 return references;
27 }
28
29 /**
30 * Replies a set of all {@link RelationToChildReference}s for a collection of child primitives
31 *
32 * @param children the collection of child primitives
33 * @return a set of all {@link RelationToChildReference}s to the children in the collection of child
34 * primitives
35 */
36 public static Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
37 Set<RelationToChildReference> references = new HashSet<>();
38 for (OsmPrimitive child: children) {
39 references.addAll(getRelationToChildReferences(child));
40 }
41 return references;
42 }
43
44 private Relation parent;
45 private int position;
46 private String role;
47 private OsmPrimitive child;
48
49 public RelationToChildReference(Relation parent, int position, String role, OsmPrimitive child) {
50 this.parent = parent;
51 this.position = position;
52 this.role = role;
53 this.child = child;
54 }
55
56 public RelationToChildReference(Relation parent, int position, RelationMember member) {
57 this.parent = parent;
58 this.position = position;
59 this.role = member.getRole();
60 this.child = member.getMember();
61 }
62
63 public Relation getParent() {
64 return parent;
65 }
66
67 public int getPosition() {
68 return position;
69 }
70
71 public String getRole() {
72 return role;
73 }
74
75 public OsmPrimitive getChild() {
76 return child;
77 }
78
79 @Override
80 public int hashCode() {
81 final int prime = 31;
82 int result = 1;
83 result = prime * result + ((child == null) ? 0 : child.hashCode());
84 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
85 result = prime * result + position;
86 result = prime * result + ((role == null) ? 0 : role.hashCode());
87 return result;
88 }
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj)
92 return true;
93 if (obj == null)
94 return false;
95 if (getClass() != obj.getClass())
96 return false;
97 RelationToChildReference other = (RelationToChildReference) obj;
98 if (child == null) {
99 if (other.child != null)
100 return false;
101 } else if (!child.equals(other.child))
102 return false;
103 if (parent == null) {
104 if (other.parent != null)
105 return false;
106 } else if (!parent.equals(other.parent))
107 return false;
108 if (position != other.position)
109 return false;
110 if (role == null) {
111 if (other.role != null)
112 return false;
113 } else if (!role.equals(other.role))
114 return false;
115 return true;
116 }
117}
Note: See TracBrowser for help on using the repository browser.