| 10 | import buildcraft.lib.expression.node.cast.NodeCasting; |
| 11 | |
| 12 | public class Arguments { |
| 13 | public static final Arguments NO_ARGS = ArgumentCounts.NO_ARGS.createArgs(); |
| 14 | |
| 15 | private final ArgumentCounts counts; |
| 16 | public final INodeLong[] longs; |
| 17 | public final INodeDouble[] doubles; |
| 18 | public final INodeBoolean[] bools; |
| 19 | public final INodeString[] strings; |
| 20 | private int li, di, bi, si; |
| 21 | |
| 22 | public Arguments(ArgumentCounts counts) { |
| 23 | this.counts = counts; |
| 24 | this.longs = new INodeLong[counts.longs]; |
| 25 | this.doubles = new INodeDouble[counts.doubles]; |
| 26 | this.bools = new INodeBoolean[counts.booleans]; |
| 27 | this.strings = new INodeString[counts.strings]; |
| 28 | } |
| 29 | |
| 30 | public void appendNode(IExpressionNode node) { |
| 31 | if (node instanceof INodeLong) longs[li++] = (INodeLong) node; |
| 32 | if (node instanceof INodeDouble) doubles[di++] = (INodeDouble) node; |
| 33 | if (node instanceof INodeBoolean) bools[bi++] = (INodeBoolean) node; |
| 34 | if (node instanceof INodeString) strings[si++] = (INodeString) node; |
| 35 | } |
| 36 | |
| 37 | public Arguments castTo(ArgumentCounts counts) throws InvalidExpressionException { |
| 38 | li = di = bi = si = 0; |
| 39 | Arguments to = counts.createArgs(); |
| 40 | for (int i = 0; i < counts.order.size(); i++) { |
| 41 | ArgType oldType = this.counts.order.get(i); |
| 42 | ArgType newType = counts.order.get(i); |
| 43 | IExpressionNode node; |
| 44 | if (oldType == ArgType.LONG) node = longs[li++]; |
| 45 | else if (oldType == ArgType.DOUBLE) node = doubles[di++]; |
| 46 | else if (oldType == ArgType.BOOL) node = bools[bi++]; |
| 47 | else/* if (old == ArgType.STRING) */ node = strings[si++]; |
| 48 | if (newType == ArgType.STRING) { |
| 49 | node = NodeCasting.castToString(node); |
| 50 | } else if (newType == ArgType.DOUBLE) { |
| 51 | node = NodeCasting.castToDouble(node); |
| 52 | } else if (newType != oldType) { |
| 53 | throw new InvalidExpressionException("Cannot cast from " + oldType + " to " + newType); |
| 54 | } |
| 55 | to.appendNode(node); |
| 56 | } |
| 57 | return to; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String toString() { |
| 62 | return Arrays.toString(longs) + ", " + Arrays.toString(doubles) + ", " + Arrays.toString(bools) + ", " + Arrays.toString(strings); |
| 63 | } |
| 64 | } |
nothing calls this directly
no test coverage detected