1
0
mirror of https://github.com/DanilaFe/abacus synced 2025-04-22 08:27:12 -07:00
Abacus/src/main/java/org/nwapw/abacus/tree/UnaryPrefixNode.java

64 lines
1.6 KiB
Java
Raw Normal View History

2017-07-28 11:14:45 -07:00
package org.nwapw.abacus.tree;
public class UnaryPrefixNode extends TreeNode {
/**
* The operation this node will apply.
*/
private String operation;
/**
* The tree node to apply the operation to.
*/
private TreeNode applyTo;
/**
* Creates a new node with the given operation and no child.
2017-07-30 21:11:32 -07:00
*
2017-07-28 11:14:45 -07:00
* @param operation the operation for this node.
*/
2017-07-30 21:11:32 -07:00
public UnaryPrefixNode(String operation) {
2017-07-28 11:14:45 -07:00
this(operation, null);
}
/**
* Creates a new node with the given operation and child.
2017-07-30 21:11:32 -07:00
*
2017-07-28 11:14:45 -07:00
* @param operation the operation for this node.
2017-07-30 21:11:32 -07:00
* @param applyTo the node to apply the function to.
2017-07-28 11:14:45 -07:00
*/
2017-07-30 21:11:32 -07:00
public UnaryPrefixNode(String operation, TreeNode applyTo) {
2017-07-28 11:14:45 -07:00
this.operation = operation;
this.applyTo = applyTo;
}
@Override
public <T> T reduce(Reducer<T> reducer) {
Object reducedChild = applyTo.reduce(reducer);
2017-07-30 21:11:32 -07:00
if (reducedChild == null) return null;
2017-07-28 11:14:45 -07:00
return reducer.reduceNode(this, reducedChild);
}
/**
* Gets the operation of this node.
2017-07-30 21:11:32 -07:00
*
2017-07-28 11:14:45 -07:00
* @return the operation this node performs.
*/
public String getOperation() {
return operation;
}
/**
* Gets the node to which this node's operation applies.
2017-07-30 21:11:32 -07:00
*
2017-07-28 11:14:45 -07:00
* @return the tree node to which the operation will be applied.
*/
public TreeNode getApplyTo() {
return applyTo;
}
2017-07-28 14:57:11 -07:00
@Override
public String toString() {
return "(" + (applyTo == null ? "null" : applyTo.toString()) + ")" + operation;
}
2017-07-28 11:14:45 -07:00
}