CurryingUtils.java

1
package com.github.dakusui.pcond.experimentals.currying;
2
3
import com.github.dakusui.pcond.experimentals.currying.context.CurriedContext;
4
import com.github.dakusui.pcond.experimentals.currying.multi.MultiFunction;
5
import com.github.dakusui.pcond.core.printable.PrintableFunctionFactory;
6
7
import java.util.List;
8
import java.util.function.Function;
9
import java.util.function.Supplier;
10
import java.util.stream.IntStream;
11
import java.util.stream.Stream;
12
13
import static com.github.dakusui.pcond.internals.InternalUtils.formatObject;
14
import static java.util.Arrays.asList;
15
import static java.util.Collections.emptyList;
16
import static java.util.stream.Collectors.joining;
17
18
/**
19
 * Intended for internal use only.
20
 */
21
public enum CurryingUtils {
22
  ;
23
  private static final ThreadLocal<Function<List<Object>, CurriedFunction<Object, Object>>> CURRIED_FUNCTION_FACTORY_POOL = new ThreadLocal<>();
24
25
  public static CurriedFunction<Object, Object> curry(MultiFunction<Object> function) {
26 1 1. curry : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::curry → KILLED
    return curry(function, emptyList());
27
  }
28
29
  public static Function<List<Object>, CurriedFunction<Object, Object>> currier() {
30 1 1. currier : negated conditional → KILLED
    if (CURRIED_FUNCTION_FACTORY_POOL.get() == null)
31 1 1. currier : removed call to java/lang/ThreadLocal::set → SURVIVED
      CURRIED_FUNCTION_FACTORY_POOL.set((List<Object> args) ->
32 1 1. lambda$currier$3 : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$currier$3 → KILLED
          PrintableFunctionFactory.create(
33 3 1. lambda$null$0 : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$0 → KILLED
2. lambda$null$1 : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$1 → KILLED
3. lambda$null$2 : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$2 → KILLED
              (args_) -> () -> functionNameFormatter(functionName(args_), ongoingContext(args_)).apply(function(args_)), (args_) -> new CurriedFunction.Impl(function(args_), ongoingContext(args_)), args, CurryingUtils.class
34
          ));
35 1 1. currier : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::currier → KILLED
    return CURRIED_FUNCTION_FACTORY_POOL.get();
36
  }
37
38
  public static <R> Function<CurriedContext, R> applyCurriedFunction(CurriedFunction<Object, Object> curriedFunction, int... orderArgs) {
39 1 1. applyCurriedFunction : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::applyCurriedFunction → KILLED
    return context -> {
40
      CurriedFunction<?, ?> cur = curriedFunction;
41
      int[] normalizedOrderArgs = normalizeOrderArgs(context, orderArgs);
42 4 1. lambda$applyCurriedFunction$4 : changed conditional boundary → KILLED
2. lambda$applyCurriedFunction$4 : Changed increment from 1 to -1 → KILLED
3. lambda$applyCurriedFunction$4 : Replaced integer subtraction with addition → KILLED
4. lambda$applyCurriedFunction$4 : negated conditional → KILLED
      for (int i = 0; i < normalizedOrderArgs.length - 1; i++)
43
        cur = cur.applyNext(context.valueAt(normalizedOrderArgs[i]));
44 2 1. lambda$applyCurriedFunction$4 : Replaced integer subtraction with addition → KILLED
2. lambda$applyCurriedFunction$4 : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$applyCurriedFunction$4 → KILLED
      return cur.applyLast(context.valueAt(normalizedOrderArgs[context.size() - 1]));
45
    };
46
  }
47
48
  public static int[] normalizeOrderArgs(CurriedContext curriedContext, int[] orderArgs) {
49
    int[] order;
50 1 1. normalizeOrderArgs : negated conditional → KILLED
    if (orderArgs.length == 0)
51
      order = IntStream.range(0, curriedContext.size()).toArray();
52
    else
53
      order = orderArgs;
54 1 1. normalizeOrderArgs : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::normalizeOrderArgs → KILLED
    return order;
55
  }
56
57
  static CurriedFunction<Object, Object> curry(MultiFunction<Object> function, List<? super Object> ongoingContext) {
58 1 1. curry : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::curry → KILLED
    return currier().apply(asList(function.name(), function, ongoingContext));
59
  }
60
61
  private static String functionName(List<Object> args) {
62 1 1. functionName : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::functionName → KILLED
    return (String) args.get(0);
63
  }
64
65
  @SuppressWarnings("unchecked")
66
  private static MultiFunction<Object> function(List<Object> args) {
67 1 1. function : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::function → KILLED
    return (MultiFunction<Object>) args.get(1);
68
  }
69
70
  @SuppressWarnings("unchecked")
71
  private static List<? super Object> ongoingContext(List<Object> args) {
72 1 1. ongoingContext : replaced return value with Collections.emptyList for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::ongoingContext → KILLED
    return (List<? super Object>) args.get((2));
73
  }
74
75
  private static Function<MultiFunction<Object>, String> functionNameFormatter(String functionName, List<? super Object> ongoingContext) {
76 2 1. functionNameFormatter : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::functionNameFormatter → KILLED
2. lambda$functionNameFormatter$7 : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$functionNameFormatter$7 → KILLED
    return (MultiFunction<Object> function) -> functionName +
77 1 1. lambda$functionNameFormatter$7 : negated conditional → KILLED
        (!ongoingContext.isEmpty() ? IntStream.range(0, ongoingContext.size())
78 1 1. lambda$null$5 : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$5 → KILLED
            .mapToObj(i -> function.parameterType(i).getSimpleName() + ":" + ongoingContext.get(i))
79
            .collect(joining(",", "(", ")")) : "") +
80
        IntStream.range(ongoingContext.size(), function.arity())
81 1 1. lambda$null$6 : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$6 → KILLED
            .mapToObj(i -> "(" + function.parameterType(i).getSimpleName() + ")")
82
            .collect(joining());
83
  }
84
85
  public static String formatParameterOrder(List<Integer> paramOrder) {
86
    String formatted = formatParamOrder(paramOrder.stream());
87
    String uncustomized = formatParamOrder(IntStream.range(0, paramOrder.size()).boxed());
88 2 1. formatParameterOrder : negated conditional → KILLED
2. formatParameterOrder : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::formatParameterOrder → KILLED
    return formatted.equals(uncustomized) ?
89
        "" :
90
        formatted;
91
  }
92
93
  private static String formatParamOrder(Stream<Integer> paramOrderStream) {
94 1 1. formatParamOrder : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::formatParamOrder → KILLED
    return paramOrderStream.map(Object::toString).collect(joining(",", "(", ")"));
95
  }
96
97
  static Supplier<String> messageInvalidTypeArgument(Object value, Class<?> aClass) {
98 3 1. lambda$messageInvalidTypeArgument$8 : negated conditional → KILLED
2. lambda$messageInvalidTypeArgument$8 : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$messageInvalidTypeArgument$8 → KILLED
3. messageInvalidTypeArgument : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::messageInvalidTypeArgument → KILLED
    return () -> "Given argument:" + formatObject(value) +
99
        (value == null ?
100
            "" :
101
            "(" + value.getClass() + ")") +
102
        " cannot be assigned to parameter:" + aClass.getCanonicalName();
103
  }
104
}

Mutations

26

1.1
Location : curry
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::curry → KILLED

30

1.1
Location : currier
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
negated conditional → KILLED

31

1.1
Location : currier
Killed by : none
removed call to java/lang/ThreadLocal::set → SURVIVED

32

1.1
Location : lambda$currier$3
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$currier$3 → KILLED

33

1.1
Location : lambda$null$0
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$0 → KILLED

2.2
Location : lambda$null$1
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$1 → KILLED

3.3
Location : lambda$null$2
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$2 → KILLED

35

1.1
Location : currier
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::currier → KILLED

39

1.1
Location : applyCurriedFunction
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::applyCurriedFunction → KILLED

42

1.1
Location : lambda$applyCurriedFunction$4
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
changed conditional boundary → KILLED

2.2
Location : lambda$applyCurriedFunction$4
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
Changed increment from 1 to -1 → KILLED

3.3
Location : lambda$applyCurriedFunction$4
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
Replaced integer subtraction with addition → KILLED

4.4
Location : lambda$applyCurriedFunction$4
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
negated conditional → KILLED

44

1.1
Location : lambda$applyCurriedFunction$4
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
Replaced integer subtraction with addition → KILLED

2.2
Location : lambda$applyCurriedFunction$4
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$applyCurriedFunction$4 → KILLED

50

1.1
Location : normalizeOrderArgs
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
negated conditional → KILLED

54

1.1
Location : normalizeOrderArgs
Killed by : com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest.hello_b(com.github.dakusui.pcond.experimentals.DbCCurriedFunctionsTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::normalizeOrderArgs → KILLED

58

1.1
Location : curry
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::curry → KILLED

62

1.1
Location : functionName
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::functionName → KILLED

67

1.1
Location : function
Killed by : com.github.dakusui.pcond.ut.CurryingTest.given_nullToCurriedFuncWithStringParam$whenIsValidArg$thenTrue(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::function → KILLED

72

1.1
Location : ongoingContext
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenApplyNextMoreThanExpected$thenNoSuchElementIsThrown(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with Collections.emptyList for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::ongoingContext → KILLED

76

1.1
Location : functionNameFormatter
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::functionNameFormatter → KILLED

2.2
Location : lambda$functionNameFormatter$7
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$functionNameFormatter$7 → KILLED

77

1.1
Location : lambda$functionNameFormatter$7
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
negated conditional → KILLED

78

1.1
Location : lambda$null$5
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$5 → KILLED

81

1.1
Location : lambda$null$6
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$null$6 → KILLED

88

1.1
Location : formatParameterOrder
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.toStringMultiParameterFunction$thenExpectedValueReturned(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
negated conditional → KILLED

2.2
Location : formatParameterOrder
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.toStringMultiParameterFunction$withReversedOrder$thenExpectedValueReturned(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::formatParameterOrder → KILLED

94

1.1
Location : formatParamOrder
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.toStringMultiParameterFunction$withReversedOrder$thenExpectedValueReturned(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::formatParamOrder → KILLED

98

1.1
Location : lambda$messageInvalidTypeArgument$8
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenApplyWithInvalidArg$thenThrown(com.github.dakusui.pcond.ut.CurryingTest)
negated conditional → KILLED

2.2
Location : lambda$messageInvalidTypeArgument$8
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenApplyWithInvalidArg$thenThrown(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::lambda$messageInvalidTypeArgument$8 → KILLED

3.3
Location : messageInvalidTypeArgument
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenApplyWithInvalidArg$thenThrown(com.github.dakusui.pcond.ut.CurryingTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/CurryingUtils::messageInvalidTypeArgument → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3