1
0
mirror of https://github.com/aNNiMON/HotaruFX.git synced 2024-09-19 14:14:21 +03:00

Small refactoring

This commit is contained in:
Victor 2017-09-16 11:49:36 +03:00
parent 7f33c77d22
commit 66ef5b01af
15 changed files with 45 additions and 87 deletions

View File

@ -38,7 +38,7 @@ public class CompositionBundle implements Bundle {
composition = new Composition(); composition = new Composition();
break; break;
case 1: case 1:
double frameRate = args[0].asNumber(); double frameRate = args[0].asDouble();
composition = new Composition(frameRate); composition = new Composition(frameRate);
break; break;
case 2: case 2:
@ -49,14 +49,14 @@ public class CompositionBundle implements Bundle {
case 3: case 3:
width = args[0].asInt(); width = args[0].asInt();
height = args[1].asInt(); height = args[1].asInt();
frameRate = args[2].asNumber(); frameRate = args[2].asDouble();
composition = new Composition(width, height, frameRate); composition = new Composition(width, height, frameRate);
break; break;
case 4: case 4:
default: default:
width = args[0].asInt(); width = args[0].asInt();
height = args[1].asInt(); height = args[1].asInt();
frameRate = args[2].asNumber(); frameRate = args[2].asDouble();
val background = PropertyType.PAINT.<Paint>getFromHFX().apply(args[3]); val background = PropertyType.PAINT.<Paint>getFromHFX().apply(args[3]);
composition = new Composition(width, height, frameRate, background); composition = new Composition(width, height, frameRate, background);
break; break;

View File

@ -41,7 +41,7 @@ public class NodeUtilsBundle implements Bundle {
if (args.length == 2) { if (args.length == 2) {
val array = validator.requireArrayAt(1); val array = validator.requireArrayAt(1);
val dashList = array.stream() val dashList = array.stream()
.map(Value::asNumber) .map(Value::asDouble)
.collect(Collectors.toList()); .collect(Collectors.toList());
shape.getStrokeDashArray().setAll(dashList); shape.getStrokeDashArray().setAll(dashList);
return NumberValue.ZERO; return NumberValue.ZERO;

View File

@ -52,7 +52,7 @@ public class NodesBundle implements Bundle {
val validator = Validator.with(args); val validator = Validator.with(args);
val map = validator.requireMapAt(1); val map = validator.requireMapAt(1);
val points = validator.requireArrayAt(0).stream() val points = validator.requireArrayAt(0).stream()
.map(Value::asNumber) .map(Value::asDouble)
.collect(Collectors.toList()); .collect(Collectors.toList());
val node = new NodeValue(ctor.apply(points)); val node = new NodeValue(ctor.apply(points));
node.fill(map); node.fill(map);

View File

@ -62,12 +62,7 @@ public class ArrayValue implements Value, Iterable<Value> {
} }
@Override @Override
public int asInt() { public Number asNumber() {
throw new TypeException("Cannot cast array to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast array to number"); throw new TypeException("Cannot cast array to number");
} }

View File

@ -26,12 +26,7 @@ public class FunctionValue implements Value {
} }
@Override @Override
public int asInt() { public Number asNumber() {
throw new TypeException("Cannot cast function to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast function to number"); throw new TypeException("Cannot cast function to number");
} }

View File

@ -24,12 +24,7 @@ public class InterpolatorValue implements Value {
} }
@Override @Override
public int asInt() { public Number asNumber() {
throw new TypeException("Cannot cast interpolator to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast interpolator to number"); throw new TypeException("Cannot cast interpolator to number");
} }

View File

@ -46,12 +46,7 @@ public class MapValue implements Value, Iterable<Map.Entry<String, Value>> {
} }
@Override @Override
public int asInt() { public Number asNumber() {
throw new TypeException("Cannot cast map to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast map to number"); throw new TypeException("Cannot cast map to number");
} }

View File

@ -116,12 +116,7 @@ public class NodeValue implements Value {
} }
@Override @Override
public int asInt() { public Number asNumber() {
throw new TypeException("Cannot cast node to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast node to number"); throw new TypeException("Cannot cast node to number");
} }

View File

@ -55,34 +55,9 @@ public class NumberValue implements Value {
return value.intValue() != 0; return value.intValue() != 0;
} }
public byte asByte() {
return value.byteValue();
}
public short asShort() {
return value.shortValue();
}
@Override @Override
public int asInt() { public Number asNumber() {
return value.intValue(); return value;
}
public long asLong() {
return value.longValue();
}
public float asFloat() {
return value.floatValue();
}
public double asDouble() {
return value.doubleValue();
}
@Override
public double asNumber() {
return value.doubleValue();
} }
@Override @Override

View File

@ -108,12 +108,7 @@ public class PropertyValue implements Value {
} }
@Override @Override
public int asInt() { public Number asNumber() {
throw new TypeException("Cannot cast property to integer");
}
@Override
public double asNumber() {
throw new TypeException("Cannot cast property to number"); throw new TypeException("Cannot cast property to number");
} }

View File

@ -4,8 +4,6 @@ import java.util.Objects;
public class StringValue implements Value { public class StringValue implements Value {
public static final StringValue EMPTY = new StringValue("");
private final String value; private final String value;
public StringValue(String value) { public StringValue(String value) {
@ -36,7 +34,7 @@ public class StringValue implements Value {
} }
@Override @Override
public double asNumber() { public double asDouble() {
try { try {
return Double.parseDouble(value); return Double.parseDouble(value);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -44,6 +42,11 @@ public class StringValue implements Value {
} }
} }
@Override
public Number asNumber() {
return asDouble();
}
@Override @Override
public String asString() { public String asString() {
return value; return value;

View File

@ -4,9 +4,19 @@ public interface Value extends Comparable<Value> {
Object raw(); Object raw();
int asInt(); default boolean asBoolean() {
return asInt() != 0;
}
double asNumber(); default int asInt() {
return asNumber().intValue();
}
default double asDouble() {
return asNumber().doubleValue();
}
Number asNumber();
String asString(); String asString();

View File

@ -117,12 +117,12 @@ public class InterpreterVisitor implements ResultVisitor<Value, Context> {
final double frame; final double frame;
switch (node.operator) { switch (node.operator) {
case SECONDS: case SECONDS:
frame = value.asNumber() * frameRate; frame = value.asDouble() * frameRate;
break; break;
case MILLISECONDS: case MILLISECONDS:
default: default:
frame = (value.asNumber() * frameRate) / 1000d; frame = (value.asDouble() * frameRate) / 1000d;
break; break;
} }
return NumberValue.of(frame); return NumberValue.of(frame);

View File

@ -23,7 +23,7 @@ import lombok.val;
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public enum PropertyType { public enum PropertyType {
BOOLEAN(v -> v.asInt() != 0, o -> NumberValue.fromBoolean(Boolean.TRUE.equals(o))), BOOLEAN(Value::asBoolean, o -> NumberValue.fromBoolean(Boolean.TRUE.equals(o))),
NUMBER(toNumber(), o -> NumberValue.of((Number) o)), NUMBER(toNumber(), o -> NumberValue.of((Number) o)),
STRING(Value::asString, o -> new StringValue(String.valueOf(o))), STRING(Value::asString, o -> new StringValue(String.valueOf(o))),
NODE(toNode(), fromNode()), NODE(toNode(), fromNode()),
@ -50,7 +50,7 @@ public enum PropertyType {
if (value.type() == Types.NUMBER) { if (value.type() == Types.NUMBER) {
return ((NumberValue) value).raw(); return ((NumberValue) value).raw();
} }
return value.asNumber(); return value.asDouble();
}; };
} }
@ -82,12 +82,12 @@ public enum PropertyType {
val map = ((MapValue) value).getMap(); val map = ((MapValue) value).getMap();
val family = map.getOrDefault("family", new StringValue(Font.getDefault().getFamily())).asString(); val family = map.getOrDefault("family", new StringValue(Font.getDefault().getFamily())).asString();
val weight = map.getOrDefault("weight", NumberValue.of(FontWeight.NORMAL.getWeight())).asInt(); val weight = map.getOrDefault("weight", NumberValue.of(FontWeight.NORMAL.getWeight())).asInt();
val isItalic = map.getOrDefault("italic", NumberValue.ZERO).asInt() != 0; val isItalic = map.getOrDefault("italic", NumberValue.ZERO).asBoolean();
val posture = isItalic ? FontPosture.ITALIC : FontPosture.REGULAR; val posture = isItalic ? FontPosture.ITALIC : FontPosture.REGULAR;
val size = map.getOrDefault("size", NumberValue.MINUS_ONE).asNumber(); val size = map.getOrDefault("size", NumberValue.MINUS_ONE).asDouble();
return Font.font(family, FontWeight.findByWeight(weight), posture, size); return Font.font(family, FontWeight.findByWeight(weight), posture, size);
} }
return Font.font(value.asNumber()); return Font.font(value.asDouble());
}; };
} }

View File

@ -126,25 +126,25 @@ class InterpreterVisitorTest {
Value value; Value value;
value = eval("rate(30) t = 1 sec", context); value = eval("rate(30) t = 1 sec", context);
assertThat(value.asNumber(), closeTo(30, 0.0001)); assertThat(value.asDouble(), closeTo(30, 0.0001));
value = eval("rate(30) t = 5 sec", context); value = eval("rate(30) t = 5 sec", context);
assertThat(value.asNumber(), closeTo(150, 0.0001)); assertThat(value.asDouble(), closeTo(150, 0.0001));
value = eval("rate(30) t = 0.5 sec", context); value = eval("rate(30) t = 0.5 sec", context);
assertThat(value.asNumber(), closeTo(15, 0.0001)); assertThat(value.asDouble(), closeTo(15, 0.0001));
value = eval("rate(30) t = 1000 ms", context); value = eval("rate(30) t = 1000 ms", context);
assertThat(value.asNumber(), closeTo(30, 0.0001)); assertThat(value.asDouble(), closeTo(30, 0.0001));
value = eval("rate(30) t = 333 ms", context); value = eval("rate(30) t = 333 ms", context);
assertThat(value.asNumber(), closeTo(10, 0.01)); assertThat(value.asDouble(), closeTo(10, 0.01));
value = eval("rate(25) t = 1 sec", context); value = eval("rate(25) t = 1 sec", context);
assertThat(value.asNumber(), closeTo(25, 0.0001)); assertThat(value.asDouble(), closeTo(25, 0.0001));
value = eval("rate(25) t = 1000 ms", context); value = eval("rate(25) t = 1000 ms", context);
assertThat(value.asNumber(), closeTo(25, 0.0001)); assertThat(value.asDouble(), closeTo(25, 0.0001));
} }
@Test @Test