Skip to content

Commit 6788ac5

Browse files
committed
feat: add bi tuple
1 parent 05e6982 commit 6788ac5

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ include(
2424
"crypto-toolbox",
2525
"math-toolbox",
2626
)
27+
28+
include("tuple")
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
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+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.tuple;
19+
20+
import java.util.Objects;
21+
22+
/**
23+
* Represents an ordered pair of elements, where the first element is of type {@code L}
24+
* and the second is of type {@code R}. This class provides a simple way to group two
25+
* related values together.
26+
* <p>
27+
* The class is mutable, allowing the values of the left and right elements to be changed
28+
* after creation. It also overrides the {@code equals}, {@code hashCode}, and {@code toString}
29+
* methods to provide meaningful comparisons and string representations.
30+
*
31+
* @param <L> the type of the left element
32+
* @param <R> the type of the right element
33+
* @author siujamo
34+
* @author zihluwang
35+
*/
36+
public final class BiTuple<L, R> {
37+
38+
/**
39+
* The left element of the tuple.
40+
*/
41+
private L left;
42+
43+
/**
44+
* The right element of the tuple.
45+
*/
46+
private R right;
47+
48+
/**
49+
* Constructs a new {@code BiTuple} with the specified left and right elements.
50+
*
51+
* @param left the left element to be stored in the tuple
52+
* @param right the right element to be stored in the tuple
53+
*/
54+
public BiTuple(L left, R right) {
55+
this.left = left;
56+
this.right = right;
57+
}
58+
59+
/**
60+
* Retrieves the left element of the tuple.
61+
*
62+
* @return the left element of the tuple
63+
*/
64+
public L getLeft() {
65+
return left;
66+
}
67+
68+
/**
69+
* Sets the left element of the tuple to the specified value.
70+
*
71+
* @param left the new value for the left element of the tuple
72+
*/
73+
public void setLeft(L left) {
74+
this.left = left;
75+
}
76+
77+
/**
78+
* Retrieves the right element of the tuple.
79+
*
80+
* @return the right element of the tuple
81+
*/
82+
public R getRight() {
83+
return right;
84+
}
85+
86+
/**
87+
* Sets the right element of the tuple to the specified value.
88+
*
89+
* @param right the new value for the right element of the tuple
90+
*/
91+
public void setRight(R right) {
92+
this.right = right;
93+
}
94+
95+
/**
96+
* Compares this {@code BiTuple} with the specified object for equality.
97+
* <p>
98+
* Two {@code BiTuple}s are considered equal if they are of the same type and their left and
99+
* right elements are equal according to their respective {@code equals} methods.
100+
*
101+
* @param object the object to compare with this {@code BiTuple}
102+
* @return {@code true} if the specified object is equal to this {@code BiTuple},
103+
* {@code false} otherwise
104+
*/
105+
@Override
106+
public boolean equals(Object object) {
107+
if (!(object instanceof BiTuple<?, ?> biTuple)) return false;
108+
return Objects.equals(left, biTuple.left) && Objects.equals(right, biTuple.right);
109+
}
110+
111+
/**
112+
* Returns a hash code value for the {@code BiTuple}.
113+
* <p>
114+
* The hash code is calculated based on the hash codes of the left and right elements.
115+
*
116+
* @return a hash code value for this {@code BiTuple}
117+
*/
118+
@Override
119+
public int hashCode() {
120+
return Objects.hash(left, right);
121+
}
122+
123+
/**
124+
* Returns a string representation of the {@code BiTuple}.
125+
* <p>
126+
* The string representation consists of the class name, followed by the values of
127+
* the left and right elements in the format {@code "BiTuple{left=value1, right=value2}"}.
128+
*
129+
* @return a string representation of the {@code BiTuple}
130+
*/
131+
@Override
132+
public String toString() {
133+
return "BiTuple{" +
134+
"left=" + left +
135+
", right=" + right +
136+
'}';
137+
}
138+
139+
/**
140+
* Creates a new {@code BiTuple} with the specified left and right elements.
141+
*
142+
* @param <L> the type of the left element
143+
* @param <R> the type of the right element
144+
* @param left the left element
145+
* @param right the right element
146+
* @return a new {@code BiTuple} containing the specified elements
147+
*/
148+
public static <L, R> BiTuple<L, R> of(L left, R right) {
149+
return new BiTuple<>(left, right);
150+
}
151+
}

0 commit comments

Comments
 (0)