1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package tools.gsf.navigation.siteplan;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.stream.Collectors;
21
22 import com.fatwire.assetapi.data.AssetId;
23
24 import tools.gsf.navigation.AssetNode;
25 import tools.gsf.navigation.ConfigurableNode;
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public abstract class AbstractAssetNode<N extends AssetNode<N> & ConfigurableNode<N>> implements AssetNode<N>, ConfigurableNode<N> {
40
41 private static final long serialVersionUID = -7637446633778028560L;
42
43 protected AssetId id;
44 protected N parent = null;
45 protected ArrayList<N> children = new ArrayList<>();
46
47 public AbstractAssetNode(AssetId assetId) {
48 this.id = assetId;
49 }
50
51 public AssetId getId() {
52 return this.id;
53 }
54
55 @Override
56 public void addChild(N node) {
57 children.add(node);
58 }
59
60 @Override
61 public void setParent(N parent) {
62 this.parent = parent;
63 }
64
65 @Override
66 public void removeChildren() {
67 for (N child : this.children) {
68 child.setParent(null);
69 }
70 this.children.clear();
71 }
72
73 public boolean removeChild(N child) {
74 return this.children.remove(child);
75 }
76
77 @Override
78 public N getParent() {
79 return parent;
80 }
81
82 @Override
83 public List<N> getSiblings() {
84 return parent.getChildren();
85 }
86
87 @Override
88 public List<N> getChildren() {
89 return children.stream().filter(n -> n != null).collect(Collectors.toList());
90 }
91
92 @Override
93 public boolean equals(Object o) {
94 if (this == o) {
95 return true;
96 }
97 if (o == null || getClass() != o.getClass()) {
98 return false;
99 }
100
101 AssetNode<?> that = (AssetNode<?>) o;
102
103 if (! this.id.equals(that.getId())) {
104 return false;
105 }
106 if (this.parent != null ? !this.parent.equals(that.getParent()) : that.getParent() != null) {
107 return false;
108 }
109 return this.children.equals(that.getChildren());
110
111 }
112
113 @Override
114 public int hashCode() {
115 int result = this.id.hashCode();
116 result = 31 * result + (this.parent != null ? this.parent.getId().hashCode() : 0);
117 result = 31 * result + this.children.hashCode();
118 return result;
119 }
120
121 }