1
20 package org.crosswire.jsword.passage;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
35 public class TreeKey extends AbstractKeyList {
36
42 public TreeKey(String name, Key parent) {
43 super(name);
44 this.parent = parent;
45 this.children = new ArrayList<Key>();
46 }
47
48
53 public TreeKey(String text) {
54 this(text, null);
55 }
56
57
60 public boolean canHaveChildren() {
61 return true;
62 }
63
64
67 public int getChildCount() {
68 return children.size();
69 }
70
71
74 public int getCardinality() {
75 int cardinality = 1; for (Key child : children) {
77 cardinality += child.getCardinality();
78 }
79
80 return cardinality;
81 }
82
83 @Override
84 public boolean isEmpty() {
85 return children.isEmpty();
86 }
87
88 @Override
89 public boolean contains(Key key) {
90 if (children.contains(key)) {
91 return true;
92 }
93
94 for (Key child : children) {
95 if (child.contains(key)) {
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103
106 public Iterator<Key> iterator() {
107 return new KeyIterator(this);
108 }
109
110
113 public void addAll(Key key) {
114 children.add(key);
115 }
116
117
120 public void removeAll(Key key) {
121 children.remove(key);
122 }
123
124
127 public void clear() {
128 children.clear();
129 }
130
131
134 public Key get(int index) {
135 return children.get(index);
136 }
137
138
141 public int indexOf(Key that) {
142 return children.indexOf(that);
143 }
144
145
148 public Key getParent() {
149 return parent;
150 }
151
152
154 @Override
155 public boolean equals(Object obj) {
156 if (obj == null) {
158 return false;
159 }
160
161 if (!obj.getClass().equals(this.getClass())) {
164 return false;
165 }
166
167 TreeKey otherTreeKey = (TreeKey) obj;
168 if (!getName().equals(otherTreeKey.getName())) {
169 return false;
170 }
171
172 if (getParent() == null) {
174 return otherTreeKey.getParent() == null;
175 }
176
177 return getParent().equals(otherTreeKey.getParent());
179 }
180
181
184 public void blur(int by, RestrictionType restrict) {
185 log.warn("attempt to blur a non-blur-able list");
186 }
187
188 @Override
189 public TreeKey clone() {
190 return (TreeKey) super.clone();
191 }
192
193 @Override
194 public String getRootName() {
195 String rootName = getName();
196 for (Key parentKey = this; parentKey != null && parentKey.getName().length() > 0; parentKey = parentKey.getParent()) {
197 rootName = parentKey.getName();
198 }
199 return rootName;
200 }
201
202 @Override
203 public String getOsisRef() {
204 return getOsisID();
205 }
206
207 @Override
208 public String getOsisID() {
209 StringBuilder b = new StringBuilder(100);
210 b.append(getName());
211 for (Key parentKey = this.getParent(); parentKey != null && parentKey.getName().length() > 0; parentKey = parentKey.getParent()) {
212 b.insert(0, "/");
213 b.insert(0, parentKey.getName());
214 }
215 return b.toString();
217 }
218
219
222 private Key parent;
223
224
227 private List<Key> children;
228
229
232 private static final Logger log = LoggerFactory.getLogger(TreeKey.class);
233
234
237 private static final long serialVersionUID = -6560408145705717977L;
238 }
239