Skip to content

Commit aaba8df

Browse files
committed
introduced ConditionedExpressionDescription to avoid String.format()s with positional arguments
1 parent 0566486 commit aaba8df

File tree

6 files changed

+77
-18
lines changed

6 files changed

+77
-18
lines changed

src/main/java/net/itarray/automotion/internal/UIElement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.itarray.automotion.internal.geometry.Scalar;
99
import net.itarray.automotion.internal.geometry.Vector;
1010
import net.itarray.automotion.internal.properties.Context;
11+
import net.itarray.automotion.internal.properties.SuccessorConditionedExpressionDescription;
1112
import net.itarray.automotion.tools.general.SystemHelper;
1213
import net.itarray.automotion.tools.helpers.TextFinder;
1314
import net.itarray.automotion.validation.properties.Condition;
@@ -336,7 +337,7 @@ public void validateIsAbove(UIElement element, Condition<Scalar> condition, Cont
336337

337338
public void validateSuccessor(Direction direction, UIElement toBeValidatedSuccessor, Condition<Scalar> condition, Context context) {
338339
Expression<Scalar> signedDistance = Expression.signedDistance(end(direction), toBeValidatedSuccessor.begin(direction), direction);
339-
Expression<Boolean> assertion = condition.applyTo(signedDistance, direction.afterName() + " element aligned not properly. Expected margin should be %2$s. Actual margin is %4$s");
340+
Expression<Boolean> assertion = condition.applyTo(signedDistance, new SuccessorConditionedExpressionDescription<>(signedDistance, condition, direction));
340341
if (!assertion.evaluateIn(context, direction)) {
341342
context.add(assertion.getDescription(context, direction));
342343
context.draw(toBeValidatedSuccessor);
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.itarray.automotion.internal.properties;
22

3-
import net.itarray.automotion.internal.geometry.Direction;
43
import net.itarray.automotion.internal.geometry.ExtendGiving;
54
import net.itarray.automotion.internal.geometry.MetricSpace;
65
import net.itarray.automotion.validation.properties.Condition;
@@ -10,16 +9,16 @@ public class ConditionedExpression<T> implements Expression<Boolean> {
109

1110
private final Expression<T> toBeConditioned;
1211
private final Condition<T> toBeApplied;
13-
private final String messageFormat;
12+
private final ConditionedExpressionDescription description;
1413

1514
public ConditionedExpression(Expression<T> toBeConditioned, Condition<T> toBeApplied) {
16-
this(toBeConditioned, toBeApplied, "Expected %1$s to be %2$s. Actual %3$s is: %4$s");
15+
this(toBeConditioned, toBeApplied, new StandardConditionedExpressionDescription<>(toBeConditioned, toBeApplied));
1716
}
1817

19-
public ConditionedExpression(Expression<T> toBeConditioned, Condition<T> toBeApplied, String messageFormat) {
18+
public ConditionedExpression(Expression<T> toBeConditioned, Condition<T> toBeApplied, ConditionedExpressionDescription<T> description) {
2019
this.toBeConditioned = toBeConditioned;
2120
this.toBeApplied = toBeApplied;
22-
this.messageFormat = messageFormat;
21+
this.description = description;
2322
}
2423

2524
@Override
@@ -29,11 +28,7 @@ public <V extends MetricSpace<V>> Boolean evaluateIn(Context context, ExtendGivi
2928

3029
@Override
3130
public <V extends MetricSpace<V>> String getDescription(Context context, ExtendGiving<V> direction) {
32-
T t = toBeConditioned.evaluateIn(context, direction);
33-
return String.format(messageFormat,
34-
toBeConditioned.getDescription(context, direction),
35-
toBeApplied.getDescription(context, direction),
36-
toBeConditioned.getRepeatedDescription(context, direction),
37-
(t instanceof MetricSpace) ? ((MetricSpace) t).toStringWithUnits("px") : t);
31+
return description.describe(context, direction);
3832
}
33+
3934
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.itarray.automotion.internal.properties;
2+
3+
import net.itarray.automotion.internal.geometry.ExtendGiving;
4+
import net.itarray.automotion.internal.geometry.MetricSpace;
5+
import net.itarray.automotion.validation.properties.Condition;
6+
import net.itarray.automotion.validation.properties.Expression;
7+
8+
public abstract class ConditionedExpressionDescription<T> {
9+
protected final Expression<T> toBeConditioned;
10+
protected final Condition<T> toBeApplied;
11+
12+
public ConditionedExpressionDescription(Expression<T> toBeConditioned, Condition<T> toBeApplied) {
13+
this.toBeConditioned = toBeConditioned;
14+
this.toBeApplied = toBeApplied;
15+
}
16+
17+
public abstract <V extends MetricSpace<V>> String describe(Context context, ExtendGiving<V> direction);
18+
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.itarray.automotion.internal.properties;
2+
3+
import net.itarray.automotion.internal.geometry.ExtendGiving;
4+
import net.itarray.automotion.internal.geometry.MetricSpace;
5+
import net.itarray.automotion.validation.properties.Condition;
6+
import net.itarray.automotion.validation.properties.Expression;
7+
8+
public class StandardConditionedExpressionDescription<T> extends ConditionedExpressionDescription<T> {
9+
public StandardConditionedExpressionDescription(Expression<T> toBeConditioned, Condition<T> toBeApplied) {
10+
super(toBeConditioned, toBeApplied);
11+
}
12+
13+
public <V extends MetricSpace<V>> String describe(Context context, ExtendGiving<V> direction) {
14+
T t = toBeConditioned.evaluateIn(context, direction);
15+
return String.format("Expected %s to be %s. Actual %s is: %s",
16+
toBeConditioned.getDescription(context, direction),
17+
toBeApplied.getDescription(context, direction),
18+
toBeConditioned.getRepeatedDescription(context, direction),
19+
(t instanceof MetricSpace) ? ((MetricSpace) t).toStringWithUnits("px") : t);
20+
}
21+
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.itarray.automotion.internal.properties;
2+
3+
import net.itarray.automotion.internal.geometry.Direction;
4+
import net.itarray.automotion.internal.geometry.ExtendGiving;
5+
import net.itarray.automotion.internal.geometry.MetricSpace;
6+
import net.itarray.automotion.validation.properties.Condition;
7+
import net.itarray.automotion.validation.properties.Expression;
8+
9+
public class SuccessorConditionedExpressionDescription<T> extends ConditionedExpressionDescription<T> {
10+
private final Direction direction;
11+
12+
public SuccessorConditionedExpressionDescription(Expression<T> toBeConditioned, Condition<T> toBeApplied, Direction direction) {
13+
super(toBeConditioned, toBeApplied);
14+
this.direction = direction;
15+
}
16+
17+
18+
public <V extends MetricSpace<V>> String describe(Context context, ExtendGiving<V> extendGiving) {
19+
T t = toBeConditioned.evaluateIn(context, extendGiving);
20+
return String.format(direction.afterName() + " element aligned not properly. Expected margin should be %s. Actual margin is %s",
21+
toBeApplied.getDescription(context, extendGiving),
22+
(t instanceof MetricSpace) ? ((MetricSpace) t).toStringWithUnits("px") : t);
23+
}
24+
25+
}

src/main/java/net/itarray/automotion/validation/properties/Condition.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package net.itarray.automotion.validation.properties;
22

3-
import net.itarray.automotion.internal.geometry.Direction;
43
import net.itarray.automotion.internal.geometry.ExtendGiving;
54
import net.itarray.automotion.internal.geometry.MetricSpace;
65
import net.itarray.automotion.internal.geometry.Scalar;
76
import net.itarray.automotion.internal.properties.Between;
87
import net.itarray.automotion.internal.properties.BinaryScalarConditionWithFixedOperand;
8+
import net.itarray.automotion.internal.properties.ConditionedExpressionDescription;
99
import net.itarray.automotion.internal.properties.ConditionedExpression;
10-
import net.itarray.automotion.internal.properties.ConstantExpression;
1110
import net.itarray.automotion.internal.properties.Context;
1211
import net.itarray.automotion.internal.properties.ContextBiFunction;
1312
import net.itarray.automotion.internal.properties.PixelConstant;
1413

15-
import java.util.function.BiPredicate;
16-
1714
import static net.itarray.automotion.internal.geometry.Scalar.scalar;
1815

1916
public interface Condition<T> {
@@ -127,8 +124,8 @@ default Expression<Boolean> applyTo(Expression<T> toBeConditioned) {
127124
return new ConditionedExpression<>(toBeConditioned, this);
128125
}
129126

130-
default Expression<Boolean> applyTo(Expression<T> toBeConditioned, String messageFormat) {
131-
return new ConditionedExpression<>(toBeConditioned, this, messageFormat);
127+
default Expression<Boolean> applyTo(Expression<T> toBeConditioned, ConditionedExpressionDescription<T> description) {
128+
return new ConditionedExpression<>(toBeConditioned, this, description);
132129
}
133130

134131
<V extends MetricSpace<V>> String getDescription(Context context, ExtendGiving<V> direction);

0 commit comments

Comments
 (0)