View Javadoc
1   /*
2    * Copyright 2016 Function1. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Simple node, representing an asset, that can be populated with asset data.
29   *  
30   * It is up to the concrete class extending this base class to decide what data to
31   * expose and how.
32   * 
33   * This base class just deals with the annoying stuff required by most (all?)
34   * AssetNode implementations.
35   *  
36   * @author Freddy Villalba
37   * @since 2017-03-02.
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 }