1 package org.lsst.ccs.command;
2
3 import org.lsst.ccs.utilities.beanutils.Optional;
4 import org.lsst.ccs.utilities.structs.TreeBranch;
5
6 import javax.swing.tree.TreeNode;
7 import java.util.Iterator;
8 import java.util.LinkedHashMap;
9
10
11
12
13
14
15
16
17
18
19 public class DictionariesTree implements Iterable<DictionaryContext> {
20 private final TreeBranch<DictionaryContext> topNode;
21 private LinkedHashMap<String, TreeBranch<DictionaryContext>> map;
22
23 public DictionariesTree(TreeBranch<DictionaryContext> topNode) {
24 this.topNode = topNode;
25 Iterator<TreeBranch<DictionaryContext>> iterator = topNode.nodeIterator();
26 while (iterator.hasNext()) {
27 TreeBranch<DictionaryContext> node = iterator.next();
28 DictionaryContext dictionaryContext = node.getContent();
29 map.put(dictionaryContext.getName(), node);
30 }
31 }
32
33
34
35
36
37
38
39 public Optional<Dictionary> getDictionaryFor(String componentName) {
40 TreeBranch<DictionaryContext> node = map.get(componentName);
41 if (node != null) {
42 return Optional.of(node.getContent().getDictionary());
43 }
44 return Optional.empty();
45 }
46
47
48
49
50
51
52
53
54
55
56 public TreeBranch<DictionaryContext> getTopNode() {
57 return topNode;
58 }
59
60 public TreeNode getNodeFor(String componentName) {
61 return map.get(componentName);
62 }
63
64
65
66
67
68 @Override
69 public Iterator<DictionaryContext> iterator() {
70 return topNode.iterator();
71 }
72
73
74
75
76 public Iterator<String> namesIterator() {
77 return new Iterator<String>() {
78 Iterator<DictionaryContext> ctxIterator = iterator();
79
80 @Override
81 public boolean hasNext() {
82 return ctxIterator.hasNext();
83 }
84
85 @Override
86 public String next() {
87 DictionaryContext dictCtx = ctxIterator.next();
88 return dictCtx.getName();
89 }
90
91 @Override
92 public void remove() {
93 throw new UnsupportedOperationException("remove in nameIterator");
94 }
95 };
96 }
97
98
99 }