summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata/AbbrevTree.java
blob: 35c44f092b5052846bef04ff0138eaeec9791541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package bjc.esodata;

import java.util.*;

import bjc.data.Pair;
import bjc.data.TransformIterator;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.ListEx;

/**
 * A labeled tree, where you can reference sub-nodes by their label as long as
 * the reference is unambiguous.
 *
 * Inspired by the way that you can reference COBOL members by their name, as
 * long as it is unambiguous. If it is ambiguous, you can instead use parent
 * nodes to disambiguate.
 *
 * Additional note: The base iterator will give you all of the child nodes, but
 * in no defined order.
 * 
 * @param <Label>     The label on each node
 * @param <Contained> The type of data contained in the nodes.
 */
public class AbbrevTree<Label, Contained> implements Iterable<Pair<Label, Contained>> {
	private Multimap<Label, AbbrevTree<Label, Contained>> labelledNodes;

	private Map<Label, AbbrevTree<Label, Contained>> children;
	private AbbrevTree<Label, Contained> parent;

	private Contained data;
	private Label label;

	/**
	 * Create a new empty root AbbrevTree.
	 */
	public AbbrevTree() {
		labelledNodes = new Multimap<>();
		children = new HashMap<>();
	}

	/**
	 * Create a new occupied root AbbrevTree
	 * 
	 * @param label The label for this tree
	 * @param data  The data for this tree
	 */
	public AbbrevTree(Label label, Contained data) {
		this();

		this.label = label;
		this.data = data;
	}

	/**
	 * Create a new empty child AbbrevTree.
	 * 
	 * @param parent The parent of this node
	 */
	public AbbrevTree(AbbrevTree<Label, Contained> parent) {
		labelledNodes = new Multimap<>();
		children = new HashMap<>();

		this.parent = parent;
	}

	/**
	 * Create a new occupied child AbbrevTree
	 * 
	 * @param parent The parent of this node
	 * @param label  The label for this tree
	 * @param data   The data for this tree
	 */
	public AbbrevTree(AbbrevTree<Label, Contained> parent, Label label, Contained data) {
		this();

		this.parent = parent;
		this.label = label;
		this.data = data;

		addFromChild(label, this);
	}

	private void addFromChild(Label lbl, AbbrevTree<Label, Contained> node) {
		labelledNodes.add(lbl, node);

		if (parent != null)
			parent.addFromChild(lbl, node);
	}

	/**
	 * Get the data contained in this node.
	 * 
	 * @return The contained data.
	 */
	public Contained getData() {
		return data;
	}

	/**
	 * Set the data contained in this node.
	 * 
	 * @param data The new data.
	 */
	public void setData(Contained data) {
		this.data = data;
	}

	/**
	 * Get the label for this node.
	 * 
	 * @return The label for this node.
	 */
	public Label getLabel() {
		return label;
	}

	/*
	 * Unsupported for now. This requires some additional scaffolding.
	 * 
	 * Set the label for this node.
	 * 
	 * @param label The new label for this node.
	 */
	// public void setLabel(Label label) {
	// this.label = label;
	// }

	/**
	 * Add a child to this node
	 * 
	 * @param key The label for the new node
	 * @param dat The data for the new node.
	 * 
	 * @return The new node
	 */
	public AbbrevTree<Label, Contained> add(Label key, Contained dat) {
		AbbrevTree<Label, Contained> node = new AbbrevTree<>(this, key, dat);

		children.put(key, node);

		return node;
	}

	/**
	 * Remove a direct child from this node.
	 * 
	 * @param key The label for this child.
	 * 
	 * @return The removed child.
	 */
	public Optional<AbbrevTree<Label, Contained>> removeChild(Label key) {
		Optional<AbbrevTree<Label, Contained>> res = Optional.ofNullable(children.remove(key));

		res.ifPresent((node) -> {
			node.parent = null;
			node.labelledNodes.iterator().forEachRemaining((par) -> {
				labelledNodes.remove(par.getLeft(), par.getRight());

				parent.labelledNodes.remove(par.getLeft(), par.getRight());
			});
		});

		return res;
	}

	/**
	 * Retrieve a number of subnodes from this tree which correspond to the given
	 * keys.
	 * 
	 * Note that the keys are passed in reverse order. Essentially, the first
	 * argument is the actual key, the remainder are just disambiguators
	 * 
	 * @param keys The keys to look up.
	 * 
	 * @return All of the nodes which match the given key pattern.
	 */
	public Set<AbbrevTree<Label, Contained>> nodes(@SuppressWarnings("unchecked") Label... keys) {
		// Need this; Java can't deduce the proper type for reduceAux otherwise
		Set<AbbrevTree<Label, Contained>> nodes = new HashSet<>();

		ListEx<Label> keyList = new FunctionalList<>(keys);

		// COBOL keylists are in reverse order
		keyList.reverse();

		Label last = keyList.popLast();

		List<AbbrevTree<Label, Contained>> focusList = List.of(this);
		for (Label key : keyList) {
			List<AbbrevTree<Label, Contained>> nextFocus = new ArrayList<>();

			for (AbbrevTree<Label, Contained> focus : focusList) {
				Set<AbbrevTree<Label, Contained>> focusSet = focus.labelledNodes.get(key);
				nextFocus.addAll(focusSet);
			}

			focusList = nextFocus;
		}

		focusList.forEach((focus) -> {
			nodes.addAll(focus.labelledNodes.get(last));
		});

		if (label.equals(last))
			nodes.add(this);

		return nodes;
	}

	/**
	 * Retrieve all of the values which correspond to a given key.
	 * 
	 * 
	 * Note that the keys are passed in reverse order. Essentially, the first
	 * argument is the actual key, the remainder are just disambiguators
	 * 
	 * @param keys The keys to look up
	 * 
	 * @return All of the values which correspond to the key
	 */
	public Set<Contained> values(@SuppressWarnings("unchecked") Label... keys) {
		Set<Contained> res = new HashSet<>();

		nodes(keys).forEach((node) -> res.add(node.data));

		return res;
	}

	/**
	 * Returns the singular value identified by the given keypath.
	 * 
	 * Note that unlike {@link AbbrevTree#nodes(Object...)} and
	 * {@link AbbrevTree#values(Object...)}, the keys to this method are passed in
	 * the proper order, not reverse.
	 * 
	 * @param keys The keypath to look up.
	 * 
	 * @return An optional containing the identified element if there is one;
	 *         otherwise, empty.
	 */
	public Optional<AbbrevTree<Label, Contained>> path(@SuppressWarnings("unchecked") Label... keys) {
		Optional<AbbrevTree<Label, Contained>> focus = Optional.of(this);

		for (Label key : keys) {
			focus.map((node) -> {
				return node.children.get(key);
			});
		}

		return focus;
	}

	@Override
	public Iterator<Pair<Label, Contained>> iterator() {
		return new TransformIterator<>(labelledNodes.iterator(),
				(node) -> node.mapRight(AbbrevTree<Label, Contained>::getData));
	}

	@Override
	public int hashCode() {
		return Objects.hash(children, data, label, parent);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbbrevTree<?, ?> other = (AbbrevTree<?, ?>) obj;
		return Objects.equals(children, other.children) && Objects.equals(data, other.data)
				&& Objects.equals(label, other.label);
	}
}