1 /*
2  * Copyright 2015-2020 the original author or authors.
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 org.springframework.data.util;
17
18 import lombok.AccessLevel;
19 import lombok.EqualsAndHashCode;
20 import lombok.NonNull;
21 import lombok.RequiredArgsConstructor;
22 import lombok.ToString;
23
24 import java.util.Map;
25 import java.util.stream.Collector;
26 import java.util.stream.Collectors;
27 import java.util.stream.Stream;
28
29 /**
30  * A tuple of things.
31  *
32  * @author Tobias Trelle
33  * @author Oliver Gierke
34  * @author Christoph Strobl
35  * @param <S> Type of the first thing.
36  * @param <T> Type of the second thing.
37  * @since 1.12
38  */

39 @ToString
40 @EqualsAndHashCode
41 @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
42 public final class Pair<S, T> {
43
44     private final @NonNull S first;
45     private final @NonNull T second;
46
47     /**
48      * Creates a new {@link Pair} for the given elements.
49      *
50      * @param first must not be {@literal null}.
51      * @param second must not be {@literal null}.
52      * @return
53      */

54     public static <S, T> Pair<S, T> of(S first, T second) {
55         return new Pair<>(first, second);
56     }
57
58     /**
59      * Returns the first element of the {@link Pair}.
60      *
61      * @return
62      */

63     public S getFirst() {
64         return first;
65     }
66
67     /**
68      * Returns the second element of the {@link Pair}.
69      *
70      * @return
71      */

72     public T getSecond() {
73         return second;
74     }
75
76     /**
77      * A collector to create a {@link Map} from a {@link Stream} of {@link Pair}s.
78      *
79      * @return
80      */

81     public static <S, T> Collector<Pair<S, T>, ?, Map<S, T>> toMap() {
82         return Collectors.toMap(Pair::getFirst, Pair::getSecond);
83     }
84 }
85