create changelog entry
[debian/openrocket] / android-libraries / TreeViewList / src / pl / polidea / treeview / InMemoryTreeNode.java
1 package pl.polidea.treeview;
2
3 import java.io.Serializable;
4 import java.util.LinkedList;
5 import java.util.List;
6
7 /**
8  * Node. It is package protected so that it cannot be used outside.
9  * 
10  * @param <T>
11  *            type of the identifier used by the tree
12  */
13 class InMemoryTreeNode<T> implements Serializable {
14     private static final long serialVersionUID = 1L;
15     private final T id;
16     private final T parent;
17     private final int level;
18     private boolean visible = true;
19     private final List<InMemoryTreeNode<T>> children = new LinkedList<InMemoryTreeNode<T>>();
20     private List<T> childIdListCache = null;
21
22     public InMemoryTreeNode(final T id, final T parent, final int level,
23             final boolean visible) {
24         super();
25         this.id = id;
26         this.parent = parent;
27         this.level = level;
28         this.visible = visible;
29     }
30
31     public int indexOf(final T id) {
32         return getChildIdList().indexOf(id);
33     }
34
35     /**
36      * Cache is built lasily only if needed. The cache is cleaned on any
37      * structure change for that node!).
38      * 
39      * @return list of ids of children
40      */
41     public synchronized List<T> getChildIdList() {
42         if (childIdListCache == null) {
43             childIdListCache = new LinkedList<T>();
44             for (final InMemoryTreeNode<T> n : children) {
45                 childIdListCache.add(n.getId());
46             }
47         }
48         return childIdListCache;
49     }
50
51     public boolean isVisible() {
52         return visible;
53     }
54
55     public void setVisible(final boolean visible) {
56         this.visible = visible;
57     }
58
59     public int getChildrenListSize() {
60         return children.size();
61     }
62
63     public synchronized InMemoryTreeNode<T> add(final int index, final T child,
64             final boolean visible) {
65         childIdListCache = null;
66         // Note! top levell children are always visible (!)
67         final InMemoryTreeNode<T> newNode = new InMemoryTreeNode<T>(child,
68                 getId(), getLevel() + 1, getId() == null ? true : visible);
69         children.add(index, newNode);
70         return newNode;
71     }
72
73     /**
74      * Note. This method should technically return unmodifiable collection, but
75      * for performance reason on small devices we do not do it.
76      * 
77      * @return children list
78      */
79     public List<InMemoryTreeNode<T>> getChildren() {
80         return children;
81     }
82
83     public synchronized void clearChildren() {
84         children.clear();
85         childIdListCache = null;
86     }
87
88     public synchronized void removeChild(final T child) {
89         final int childIndex = indexOf(child);
90         if (childIndex != -1) {
91             children.remove(childIndex);
92             childIdListCache = null;
93         }
94     }
95
96     @Override
97     public String toString() {
98         return "InMemoryTreeNode [id=" + getId() + ", parent=" + getParent()
99                 + ", level=" + getLevel() + ", visible=" + visible
100                 + ", children=" + children + ", childIdListCache="
101                 + childIdListCache + "]";
102     }
103
104     T getId() {
105         return id;
106     }
107
108     T getParent() {
109         return parent;
110     }
111
112     int getLevel() {
113         return level;
114     }
115
116 }