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/UnaryNode.java

69 lines
1.7 KiB
Java
Raw Normal View History

2017-07-28 11:14:45 -07:00
package org.nwapw.abacus.tree;
2017-08-02 10:41:52 -07:00
public class UnaryNode extends TreeNode {
2017-07-28 11:14:45 -07:00
/**
* 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-08-02 10:41:52 -07:00
public UnaryNode(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-08-02 10:41:52 -07:00
public UnaryNode(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) {
2017-08-04 14:29:24 -07:00
if (Thread.currentThread().isInterrupted())
2017-08-03 14:04:09 -07:00
return null;
2017-07-28 11:14:45 -07:00
Object reducedChild = applyTo.reduce(reducer);
2017-07-30 21:11:32 -07:00
if (reducedChild == null) return null;
2017-08-03 14:04:09 -07:00
T a = reducer.reduceNode(this, reducedChild);
2017-08-04 14:29:24 -07:00
if (Thread.currentThread().isInterrupted())
2017-08-03 14:04:09 -07:00
return null;
return a;
2017-07-28 11:14:45 -07:00
}
/**
* 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
}