Skip to content

Commit b0f81ef

Browse files
committed
DATACMNS-790 - Added Tuple value type.
Related pull request: spring-projects/spring-data-mongodb#327.
1 parent 64eafa4 commit b0f81ef

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
23+
/**
24+
* Unit tests for {@link Tuple}.
25+
*
26+
* @author Oliver Gierke
27+
*/
28+
public class TupleUnitTests {
29+
30+
/**
31+
* @see DATACMNS-790
32+
*/
33+
@Test
34+
public void setsUpSimpleInstance() {
35+
36+
Tuple<Integer, Integer> tuple = Tuple.of(1, 2);
37+
38+
assertThat(tuple.getFirst(), is(1));
39+
assertThat(tuple.getSecond(), is(2));
40+
}
41+
42+
/**
43+
* @see DATACMNS-790
44+
*/
45+
@Test(expected = IllegalArgumentException.class)
46+
public void rejectsNullFirstElement() {
47+
Tuple.of(null, 1);
48+
}
49+
50+
/**
51+
* @see DATACMNS-790
52+
*/
53+
@Test(expected = IllegalArgumentException.class)
54+
public void rejectsNullSecondElement() {
55+
Tuple.of(1, null);
56+
}
57+
58+
/**
59+
* @see DATACMNS-790
60+
*/
61+
@Test
62+
public void hasCorrectEquals() {
63+
64+
Tuple<Integer, Integer> first = Tuple.of(1, 2);
65+
Tuple<Integer, Integer> second = Tuple.of(1, 2);
66+
67+
assertThat(first, is(first));
68+
assertThat(first, is(second));
69+
assertThat(second, is(first));
70+
}
71+
72+
/**
73+
* @see DATACMNS-790
74+
*/
75+
@Test
76+
public void hasCorrectHashCode() {
77+
78+
Tuple<Integer, Integer> first = Tuple.of(1, 2);
79+
Tuple<Integer, Integer> second = Tuple.of(1, 2);
80+
Tuple<Integer, Integer> third = Tuple.of(2, 2);
81+
82+
assertThat(first.hashCode(), is(second.hashCode()));
83+
assertThat(first.hashCode(), is(not(third.hashCode())));
84+
}
85+
}

0 commit comments

Comments
 (0)