Group.java

1
/*
2
 * Copyright 2014-2026 Grzegorz Piwowarek, https://4comprehension.com/
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.pivovarit.collectors;
17
18
import java.util.ArrayList;
19
import java.util.Collections;
20
import java.util.List;
21
import java.util.Objects;
22
import java.util.function.BiFunction;
23
24
/**
25
 * Represents a grouping of values under a specific key.
26
 *
27
 * @param <K>    the type of the key
28
 * @param <V>    the type of the values
29
 * @param key    the key of this group, must not be null
30
 * @param values the list of values, must not be null
31
 */
32
public record Group<K, V>(K key, List<V> values) {
33
34
    /**
35
     * Constructs a new {@code Group} instance ensuring key and values are not null.
36
     *
37
     * @param key    the key, must not be null
38
     * @param values the list of values, must not be null
39
     */
40
    public Group {
41
        Objects.requireNonNull(key, "key cannot be null");
42
        Objects.requireNonNull(values, "values cannot be null");
43
        values = Collections.unmodifiableList(new ArrayList<>(values));
44
    }
45
46
    /**
47
     * Creates a new {@code Group} instance with the given key and values.
48
     *
49
     * @param key    the key, must not be null
50
     * @param values the list of values, must not be null
51
     * @param <K>    the type of the key
52
     * @param <V>    the type of the values
53
     *
54
     * @return a new {@code Group} instance
55
     */
56
    public static <K, V> Group<K, V> of(K key, List<V> values) {
57 1 1. of : replaced return value with null for com/pivovarit/collectors/Group::of → KILLED
        return new Group<>(key, values);
58
    }
59
60
    /**
61
     * Transforms the values in this group using the provided mapper function.
62
     * <p>
63
     * The mapper receives both the group's key and each value, allowing transformations
64
     * that depend on the grouping key.
65
     *
66
     * @param mapper the mapping function receiving (key, value), must not be null
67
     * @param <R>    the target type of the mapped values
68
     *
69
     * @return a new {@code Group} instance with the same key and the values produced by applying
70
     * {@code mapper} to the key and each value in this group
71
     */
72
    public <R> Group<K, R> map(BiFunction<? super K, ? super V, ? extends R> mapper) {
73
        Objects.requireNonNull(mapper, "mapper cannot be null");
74 1 1. map : replaced return value with null for com/pivovarit/collectors/Group::map → KILLED
        return new Group<>(key, values.stream()
75 1 1. lambda$map$0 : replaced return value with null for com/pivovarit/collectors/Group::lambda$map$0 → KILLED
          .map(v -> (R) mapper.apply(key, v))
76
          .toList());
77
    }
78
}

Mutations

57

1.1
Location : of
Killed by : com.pivovarit.collectors.GroupTest.[engine:junit-jupiter]/[class:com.pivovarit.collectors.GroupTest]/[nested-class:MapTest]/[method:mapShouldApplyMapperToAllValues()]
replaced return value with null for com/pivovarit/collectors/Group::of → KILLED

74

1.1
Location : map
Killed by : com.pivovarit.collectors.GroupTest.[engine:junit-jupiter]/[class:com.pivovarit.collectors.GroupTest]/[nested-class:MapTest]/[method:mapShouldWorkOnEmptyValuesList()]
replaced return value with null for com/pivovarit/collectors/Group::map → KILLED

75

1.1
Location : lambda$map$0
Killed by : com.pivovarit.collectors.GroupTest.[engine:junit-jupiter]/[class:com.pivovarit.collectors.GroupTest]/[nested-class:MapTest]/[method:mapShouldApplyMapperToAllValues()]
replaced return value with null for com/pivovarit/collectors/Group::lambda$map$0 → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support