Skip to content

Commit

Permalink
cleaned up some of the static constructors with bounded wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
horeilly1101 committed Jun 16, 2019
1 parent b6b0c5b commit 5be6176
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/deriv/expression/Add.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private Add(List<Expression> terms) {
* @param terms list of expressions
* @return Add
*/
public static Expression add(List<Expression> terms) {
public static Expression add(List<? extends Expression> terms) {
if (terms.isEmpty())
throw new RuntimeException("Don't instantiate an add with an empty list!");

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/deriv/expression/ExpressionUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.deriv.expression;

import com.deriv.util.ThreadManager;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand All @@ -26,11 +24,11 @@ private ExpressionUtils() {
* @param func linear function
* @return aggregated result
*/
static <T> Optional<List<T>> linearityHelper(List<T> elements, Function<T, Optional<T>> func) {
static <T> Optional<List<T>> linearityHelper(List<? extends T> elements, Function<? super T, Optional<T>> func) {
List<T> newList = new ArrayList<>();

for (T element : elements) {
Optional<T> oMapped = func.apply(element);
Optional<? extends T> oMapped = func.apply(element);
if (!oMapped.isPresent())
return Optional.empty();
newList.add(oMapped.get());
Expand All @@ -47,7 +45,7 @@ static <T> Optional<List<T>> linearityHelper(List<T> elements, Function<T, Optio
* @param <T> type of object in input list
* @return shallow copy of input list
*/
static <T> List<T> shallowCopy(List<T> lst) {
static <T> List<T> shallowCopy(List<? extends T> lst) {
return new ArrayList<>(lst);
}

Expand Down
11 changes: 2 additions & 9 deletions src/main/java/com/deriv/expression/Mult.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private Mult(List<Expression> factors) {
* @param factors list of expressions
* @return Expression
*/
public static Expression mult(List<Expression> factors) {
public static Expression mult(List<? extends Expression> factors) {
if (factors.isEmpty()) // can't allow this
throw new RuntimeException("Don't instantiate a term with an empty list!");

Expand Down Expand Up @@ -186,14 +186,7 @@ public String toLaTex() {
@Override
public Optional<Expression> evaluate(Variable var, Expression input) {
// multiplies terms together
return Optional.of(_factors.parallelStream()
.map(x -> x.evaluate(var, input))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList()))
.flatMap(lst -> lst.size() == _factors.size()
? Optional.of(mult(lst))
: Optional.empty());
return ExpressionUtils.linearityHelper(_factors, x -> x.evaluate(var, input)).map(Mult::mult);
}

@Override
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/deriv/expression/Sigma.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.deriv.expression;

import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import static com.deriv.expression.Constant.constant;

// Sigma.of(3, 4, i -> Sigma.of(1, 5, j -> addID()))

public class Sigma implements Expression {
private final int _beginRange;
private final int _endRange;
private final Function<Variable, ? extends Expression> _varMap;

private Sigma(int beginRange, int endRange, Function<Variable, ? extends Expression> varMap) {
this._beginRange = beginRange;
this._endRange = endRange;
this._varMap = varMap;
}

public static Expression of(int beginRange, int endRange, Function<Variable, ? extends Expression> varMap) {
return new Sigma(beginRange, endRange, varMap);
}

@Override
public String toString() {
return "";
}

@Override
public Optional<Expression> evaluate(Variable var, Expression input) {
// return of(_beginRange, _endRange, _varMap.andThen(x -> x.evaluate(var, input)));
return Optional.empty();
}

@Override
public Optional<Expression> differentiate(Variable var) {
return Optional.empty();
}

@Override
public Set<Variable> getVariables() {
return null;
}

@Override
public String toLaTex() {
return null;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/deriv/util/IndexGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//package com.deriv.util;
//
//@FunctionalInterface
//public interface IndexGenerator {
// apply();
//}

// Sigma.of(i -> Sigma.of(j -> addID(), 1, 5), 3, 4)
// Sigma.of(3, 4, i -> Sigma.of(1, 5, j -> addID()))
2 changes: 1 addition & 1 deletion src/main/java/com/deriv/util/ThreadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ThreadManager {
/**
* Static, final singleton thread pool to be used throughout the application.
*/
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(8);
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();

/**
* Static method to submit a task to the internal thread pool.
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/deriv/util/ThreadManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void submitStringTest() {

@Test
void submitExpressionTest() {
Future<Optional<Expression>> futureOExpression = ThreadManager.submitTask(() -> x().differentiate(x().asVariable()));
Future<Optional<Expression>> futureOExpression
= ThreadManager.submitTask(() -> x().differentiate(x().asVariable()));

Expression retrievedExpression;
try {
Expand Down

0 comments on commit 5be6176

Please sign in to comment.