MultiFunction.java

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

Mutations

75

1.1
Location : name
Killed by : com.github.valid8j.ut.currying.CurryingTest.givenCurriedFunction$whenToStringOnOngoing$thenExpectedResultReturned(com.github.valid8j.ut.currying.CurryingTest)
replaced return value with "" for com/github/valid8j/pcond/experimentals/currying/multi/MultiFunction$Impl::name → KILLED

80

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

85

1.1
Location : parameterType
Killed by : com.github.valid8j.ut.currying.CurryingTest.given_nullToCurriedFuncWithIntParam$whenIsValidArg$thenFalse(com.github.valid8j.ut.currying.CurryingTest)
replaced return value with null for com/github/valid8j/pcond/experimentals/currying/multi/MultiFunction$Impl::parameterType → KILLED

101

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

105

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

109

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

110

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

115

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

120

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

125

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

130

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

134

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

Active mutators

Tests examined


Report generated by PIT 1.7.3