|
| 1 | +/* |
| 2 | + * Copyright 2015 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 | + * http://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 org.springframework.util.Assert; |
| 19 | + |
| 20 | +/** |
| 21 | + * A tuple of things. |
| 22 | + * |
| 23 | + * @author Tobias Trelle |
| 24 | + * @author Oliver Gierke |
| 25 | + * @param <T> Type of the first thing. |
| 26 | + * @param <S> Type of the second thing. |
| 27 | + * @since 1.12 |
| 28 | + */ |
| 29 | +public final class Tuple<S, T> { |
| 30 | + |
| 31 | + private final T second; |
| 32 | + private final S first; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a new {@link Tuple} for the given elements. |
| 36 | + * |
| 37 | + * @param first must not be {@literal null}. |
| 38 | + * @param second must not be {@literal null}. |
| 39 | + * @return |
| 40 | + */ |
| 41 | + Tuple(S first, T second) { |
| 42 | + |
| 43 | + Assert.notNull(first, "First tuple element must not be null!"); |
| 44 | + Assert.notNull(second, "Second tuple element must not be null!"); |
| 45 | + |
| 46 | + this.second = second; |
| 47 | + this.first = first; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new {@link Tuple} for the given elements. |
| 52 | + * |
| 53 | + * @param first must not be {@literal null}. |
| 54 | + * @param second must not be {@literal null}. |
| 55 | + * @return |
| 56 | + */ |
| 57 | + public static <S, T> Tuple<S, T> of(S first, T second) { |
| 58 | + return new Tuple<S, T>(first, second); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the first element of the {@link com.querydsl.core.Tuple}. |
| 63 | + * |
| 64 | + * @return |
| 65 | + */ |
| 66 | + public S getFirst() { |
| 67 | + return first; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the second element of the {@link com.querydsl.core.Tuple}. |
| 72 | + * |
| 73 | + * @return |
| 74 | + */ |
| 75 | + public T getSecond() { |
| 76 | + return second; |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + * (non-Javadoc) |
| 81 | + * @see java.lang.Object#equals(java.lang.Object) |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public boolean equals(Object obj) { |
| 85 | + |
| 86 | + if (this == obj) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + if (!(obj instanceof Tuple)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + Tuple<?, ?> that = (Tuple<?, ?>) obj; |
| 95 | + |
| 96 | + return this.first.equals(that.first) && this.second.equals(that.second); |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + * (non-Javadoc) |
| 101 | + * @see java.lang.Object#hashCode() |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public int hashCode() { |
| 105 | + |
| 106 | + int result = 31; |
| 107 | + |
| 108 | + result += 17 * first.hashCode(); |
| 109 | + result += 17 * second.hashCode(); |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | +} |
0 commit comments