summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java
blob: 624c9d6335d6097f323dbcf19b0891d9536c2be3 (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
package bjc.utils.funcdata;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import bjc.utils.funcdata.bst.TreeLinearizationMethod;

/**
 * A node in a homogenous tree with a unlimited amount of children
 * 
 * @author ben
 * @param <ContainedType>
 *            The type of data contained in the tree nodes
 *
 */
public interface ITree<ContainedType> {
	/**
	 * Add a child to this node
	 * 
	 * @param child
	 *            The child to add to this node
	 */
	public void addChild(ITree<ContainedType> child);

	/**
	 * Transform the value that is the head of this node
	 * 
	 * @param <TransformedType>
	 *            The type of the transformed value
	 * @param transformer
	 *            The function to use to transform the value
	 * @return The transformed value
	 */
	public <TransformedType> TransformedType transformHead(
			Function<ContainedType, TransformedType> transformer);

	/**
	 * Get a count of the number of direct children this node has
	 * 
	 * @return The number of direct children this node has
	 */
	public int getChildrenCount();

	/**
	 * Transform one of this nodes children
	 * 
	 * @param <TransformedType>
	 *            The type of the transformed value
	 * @param childNo
	 *            The number of the child to transform
	 * @param transformer
	 *            The function to use to transform the value
	 * @return The transformed value
	 * 
	 * @throws IllegalArgumentException
	 *             if the childNo is out of bounds (0 <= childNo <=
	 *             childCount())
	 */
	public <TransformedType> TransformedType transformChild(int childNo,
			Function<ITree<ContainedType>, TransformedType> transformer);

	/**
	 * Collapse a tree into a single version
	 * 
	 * @param <NewType>
	 *            The intermediate type being folded
	 * @param <ReturnedType>
	 *            The type that is the end result
	 * @param leafTransform
	 *            The function to use to convert leaf values
	 * @param nodeCollapser
	 *            The function to use to convert internal nodes and their
	 *            children
	 * @param resultTransformer
	 *            The function to use to convert a state to the returned
	 *            version
	 * @return The final transformed state
	 */
	public <NewType, ReturnedType> ReturnedType collapse(
			Function<ContainedType, NewType> leafTransform,
			Function<ContainedType, Function<IFunctionalList<NewType>, NewType>> nodeCollapser,
			Function<NewType, ReturnedType> resultTransformer);

	/**
	 * Expand the nodes of a tree into trees, and then merge the contents
	 * of those trees into a single tree
	 * 
	 * @param mapper
	 *            The function to use to map values into trees
	 * @return A tree, with some nodes expanded into trees
	 */
	public ITree<ContainedType> flatMapTree(
			Function<ContainedType, ITree<ContainedType>> mapper);

	/**
	 * Transform some of the nodes in this tree
	 * 
	 * @param nodePicker
	 *            The predicate to use to pick nodes to transform
	 * @param transformer
	 *            The function to use to transform picked nodes
	 */
	public void selectiveTransform(Predicate<ContainedType> nodePicker,
			UnaryOperator<ContainedType> transformer);

	/**
	 * Transform the tree into a tree with a different type of token
	 * 
	 * @param <MappedType>
	 *            The type of the new tree
	 * @param transformer
	 *            The function to use to transform tokens
	 * @return A tree with the token types transformed
	 */
	public <MappedType> ITree<MappedType> transformTree(
			Function<ContainedType, MappedType> transformer);

	/**
	 * Perform an action on each part of the tree
	 * 
	 * @param linearizationMethod
	 *            The way to traverse the tree
	 * @param action
	 *            The action to perform on each tree node
	 */
	public void traverse(TreeLinearizationMethod linearizationMethod,
			Consumer<ContainedType> action);

	/**
	 * Rebuild the tree with the same structure, but different nodes
	 * 
	 * @param <MappedType>
	 *            The type of the new tree
	 * @param leafTransformer
	 *            The function to use to transform leaf tokens
	 * @param operatorTransformer
	 *            The function to use to transform internal tokens
	 * @return The tree, with the nodes changed
	 */
	public <MappedType> ITree<MappedType> rebuildTree(
			Function<ContainedType, MappedType> leafTransformer,
			Function<ContainedType, MappedType> operatorTransformer);
}