MultiFunction.java

1
package com.github.dakusui.pcond.experimentals.currying.multi;
2
3
import com.github.dakusui.pcond.core.printable.PrintableFunction;
4
5
import java.util.ArrayList;
6
import java.util.LinkedList;
7
import java.util.List;
8
import java.util.Objects;
9
import java.util.function.Function;
10
import java.util.function.Supplier;
11
12
import static com.github.dakusui.pcond.internals.InternalChecks.requireArgumentListSize;
13
import static java.util.Objects.requireNonNull;
14
import static java.util.stream.Collectors.joining;
15
16
/**
17
 * An interface that represents a function that can have more than one parameters.
18
 * This interface is often used in combination with {@link com.github.dakusui.pcond.forms.Functions#curry(MultiFunction)} method.
19
 *
20
 * @param <R> The type of the returned value.
21
 */
22
public interface MultiFunction<R> extends Function<List<? super Object>, R> {
23
  /**
24
   * Returns a name of this function.
25
   *
26
   * @return The name of this function.
27
   */
28
  String name();
29
30
  /**
31
   * Returns the number of parameters that this function can take.
32
   *
33
   * @return the number of parameters
34
   */
35
  int arity();
36
37
  /**
38
   * The expected type of the {@code i}th parameter.
39
   *
40
   * @param i The parameter index.
41
   * @return The type of {@code i}th parameter.
42
   */
43
  Class<?> parameterType(int i);
44
45
  /**
46
   * The type of the value returned by this function.
47
   *
48
   * @return The type of the returned value.
49
   */
50
  Class<? extends R> returnType();
51
52
  class Impl<R> extends PrintableFunction<List<? super Object>, R> implements MultiFunction<R> {
53
    private final String         name;
54
    private final List<Class<?>> parameterTypes;
55
56
    protected Impl(
57
        Object creator,
58
        List<Object> args,
59
        Supplier<String> formatter,
60
        String name,
61
        Function<? super List<? super Object>, ? extends R> function,
62
        List<Class<?>> parameterTypes) {
63
      super(
64
          creator,
65
          args,
66
          formatter,
67
          function);
68
      this.name = name;
69
      this.parameterTypes = new ArrayList<>(parameterTypes);
70
    }
71
72
    @Override
73
    public String name() {
74 1 1. name : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Impl::name → KILLED
      return name;
75
    }
76
77
    @Override
78
    public int arity() {
79 1 1. arity : replaced int return with 0 for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Impl::arity → KILLED
      return parameterTypes.size();
80
    }
81
82
    @Override
83
    public Class<?> parameterType(int i) {
84 1 1. parameterType : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Impl::parameterType → KILLED
      return parameterTypes.get(i);
85
    }
86
87
  }
88
89
  /**
90
   * A builder for a {@link MultiFunction} instance.
91
   *
92
   * @param <R> The type of value returned by the multi-function built by this object.
93
   */
94
  class Builder<R> {
95
    private final Object                                              creator        = Builder.class;
96
    private       List<Object>                                        identityArgs;
97
    private       String                                              name           = "(anonymous)";
98
    private final Function<? super List<? super Object>, ? extends R> body;
99
    private final List<Class<?>>                                      parameterTypes = new LinkedList<>();
100 1 1. lambda$new$0 : replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::lambda$new$0 → NO_COVERAGE
    private       Supplier<String>                                    formatter      = () -> name + "(" + parameterTypes.stream().map(Class::getSimpleName).collect(joining(",")) + ")";
101
102
    public Builder(Function<List<Object>, R> body) {
103
      requireNonNull(body);
104 1 1. lambda$new$1 : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::lambda$new$1 → KILLED
      this.body = args -> body.apply(requireArgumentListSize(requireNonNull(args), parameterTypes.size()));
105
    }
106
107
    public Builder<R> addParameters(List<Class<?>> parameterTypes) {
108 1 1. addParameters : removed call to java/util/stream/Stream::forEach → KILLED
      requireNonNull(parameterTypes).stream().map(this::addParameter).forEach(Objects::requireNonNull);
109 1 1. addParameters : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::addParameters → KILLED
      return this;
110
    }
111
112
    public Builder<R> identityArgs(List<Object> identity) {
113
      this.identityArgs = requireNonNull(identity);
114 1 1. identityArgs : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::identityArgs → KILLED
      return this;
115
    }
116
117
    public Builder<R> name(String name) {
118
      this.name = name;
119 1 1. name : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::name → KILLED
      return this;
120
    }
121
122
    public Builder<R> addParameter(Class<?> parameterType) {
123
      this.parameterTypes.add(requireNonNull(parameterType));
124 1 1. addParameter : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::addParameter → KILLED
      return this;
125
    }
126
127
    public Builder<R> formatter(Supplier<String> formatter) {
128
      this.formatter = requireNonNull(formatter);
129 1 1. formatter : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::formatter → KILLED
      return this;
130
    }
131
132
    public MultiFunction<R> $() {
133 1 1. $ : replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::$ → KILLED
      return new Impl<R>(
134
          this.creator,
135
          this.identityArgs,
136
          this.formatter,
137
          this.name,
138
          this.body,
139
          this.parameterTypes
140
      );
141
    }
142
  }
143
}

Mutations

74

1.1
Location : name
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/multi/MultiFunction$Impl::name → KILLED

79

1.1
Location : arity
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenApplyNextMoreThanExpected$thenNoSuchElementIsThrown(com.github.dakusui.pcond.ut.CurryingTest)
replaced int return with 0 for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Impl::arity → KILLED

84

1.1
Location : parameterType
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/multi/MultiFunction$Impl::parameterType → KILLED

100

1.1
Location : lambda$new$0
Killed by : none
replaced return value with "" for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::lambda$new$0 → NO_COVERAGE

104

1.1
Location : lambda$new$1
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.runMultiParameterFunction$thenExpectedValueReturned(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::lambda$new$1 → KILLED

108

1.1
Location : addParameters
Killed by : com.github.dakusui.pcond.ut.CurryingTest.givenCurriedFunction$whenApplyExpectedTimes$thenExpectedResultReturned(com.github.dakusui.pcond.ut.CurryingTest)
removed call to java/util/stream/Stream::forEach → KILLED

109

1.1
Location : addParameters
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.testEqualsWithDifferentObjects(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::addParameters → KILLED

114

1.1
Location : identityArgs
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.testEqualsWithDifferentObjects(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::identityArgs → KILLED

119

1.1
Location : name
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.testEqualsWithDifferentObjects(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::name → KILLED

124

1.1
Location : addParameter
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.testEqualsWithDifferentObjects(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::addParameter → KILLED

129

1.1
Location : formatter
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.testEqualsWithDifferentObjects(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::formatter → KILLED

133

1.1
Location : $
Killed by : com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest.testEqualsWithDifferentObjects(com.github.dakusui.pcond.ut.FunctionsTest$MultiFunctionTest)
replaced return value with null for com/github/dakusui/pcond/experimentals/currying/multi/MultiFunction$Builder::$ → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3