Skip to content

Commit 1efbff6

Browse files
Un-generic-ify BinOpBiConstructor
1 parent 1dacab9 commit 1efbff6

4 files changed

Lines changed: 13 additions & 24 deletions

File tree

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
package net.marcellperger.mathexpr;
22

33

4-
// TODO: this is stupid - it should not be generic!!! It should return a MathSymbol always
5-
// (not a specific subclass but exactly a `impl MathSymbol` that MUST BE CAST)
6-
// i.e. we want this to be like `MathSymbol construct(MathSymbol left, MathSymbol right)`
7-
// similar to `List<T> of(T... values)` is NOT `(? extends List<T>) of(T... values)`
8-
// (or maybe could be generic on method? but probably bad idea)
9-
// This is NOT rust, you don't need to make it explicit `impl MathSymbol`
10-
// i.e. currently:
11-
// trait BinOpBinConstructor<R: MathSymbol> {
12-
// fn construct(MS left, MS right) -> R;
13-
// }
14-
// and we want:
15-
// trait BinOpBiConstructor {
16-
// fn construct(MS left, MS right) -> (dyn?) impl MS;
17-
// }
184
@FunctionalInterface
19-
public interface BinOpBiConstructor<R extends MathSymbol> {
20-
R construct(MathSymbol left, MathSymbol right);
5+
public interface BinOpBiConstructor {
6+
// NOTE: Not generic, but implementations will just return `MathSymbol`
7+
// (which is allowed to be any subclass of MathSymbol) similar to
8+
// `List<T> of(T... values)` can return any `List` implementation
9+
MathSymbol construct(MathSymbol left, MathSymbol right);
2110
}

src/main/java/net/marcellperger/mathexpr/SymbolInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public enum SymbolInfo {
4646
}
4747
SymbolInfo(Class<? extends BinaryOperationLeftRight> cls, int precedence,
4848
GroupingDirection groupingDirection, @Nullable String infix,
49-
BinOpBiConstructor<?> biConstructor) {
49+
BinOpBiConstructor biConstructor) {
5050
this.precedence = precedence;
5151
this.cls = cls; // TODO: private + getters?
5252
this.groupingDirection = groupingDirection;
@@ -69,8 +69,8 @@ public enum SymbolInfo {
6969
return Util.chainNulls(fromClass(cls), x -> x.infix);
7070
}
7171

72-
private BinOpBiConstructor<?> biConstructorCache = null;
73-
public @NotNull BinOpBiConstructor<?> getBiConstructor() {
72+
private BinOpBiConstructor biConstructorCache = null;
73+
public @NotNull BinOpBiConstructor getBiConstructor() {
7474
if(biConstructorCache != null) return biConstructorCache;
7575
assert BinaryOperationLeftRight.class.isAssignableFrom(cls);
7676

src/main/java/net/marcellperger/mathexpr/parser/Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ public MathSymbol parseInfixPrecedenceLevel(int level) throws ExprParseException
112112
return otherOps.reversed().stream().reduce((rightpair, leftpair) ->
113113
leftpair.asVars((preOp, argL) ->
114114
new Pair<>(preOp, rightpair.asVars((midOp, argR) -> midOp.getBiConstructor().construct(argL, argR))))
115-
).map(p -> p.<MathSymbol>asVars((midOp, argR) -> midOp.getBiConstructor().construct(javaIsAnIdiot_left, argR))).orElse(left);
115+
).map(p -> p.asVars((midOp, argR) -> midOp.getBiConstructor().construct(javaIsAnIdiot_left, argR))).orElse(left);
116116
}
117117
discardWhitespace();
118118
while((op = discardMatchesNextAny_optionsSorted(infixesToFind)) != null) {
119119
SymbolInfo opInfo = Objects.requireNonNull(infixToSymbolInfo.get(op));
120120
MathSymbol right = parseInfixPrecedenceLevel(level - 1);
121-
BinOpBiConstructor<?> ctor = opInfo.getBiConstructor();
121+
BinOpBiConstructor ctor = opInfo.getBiConstructor();
122122
left = ctor.construct(left, right);
123123
discardWhitespace();
124124
}

src/test/java/net/marcellperger/mathexpr/parser/ParserTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,15 @@ public void close() {
216216
// region implementation details (ctor, clearCache, restoreCache
217217
private WithSuppressingCache() {}
218218

219-
private Map<SymbolInfo, Optional<BinOpBiConstructor<?>>> origCache = null;
219+
private Map<SymbolInfo, Optional<BinOpBiConstructor>> origCache = null;
220220

221221
protected void clearCache() {
222222
Field cacheField = getBiConstructorCache();
223223
origCache = Arrays.stream(SymbolInfo.values()).map(sym -> {
224224
try {
225-
@Nullable BinOpBiConstructor<?> cachedValue = (BinOpBiConstructor<?>)cacheField.get(sym);
225+
@Nullable BinOpBiConstructor cachedValue = (BinOpBiConstructor)cacheField.get(sym);
226226
cacheField.set(sym, null);
227-
return Util.makeEntry(sym, Optional.<BinOpBiConstructor<?>>ofNullable(cachedValue));
227+
return Util.makeEntry(sym, Optional.ofNullable(cachedValue));
228228
} catch (IllegalAccessException e) {
229229
throw Util.excToError(e);
230230
}

0 commit comments

Comments
 (0)