From 23faaf9a16deae794a48f16629bb628ed0d00a09 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 17 Jul 2025 09:45:17 +0800 Subject: [PATCH] feat: add tri tuple --- .../java/com/onixbyte/tuple/TriTuple.java | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 tuple/src/main/java/com/onixbyte/tuple/TriTuple.java diff --git a/tuple/src/main/java/com/onixbyte/tuple/TriTuple.java b/tuple/src/main/java/com/onixbyte/tuple/TriTuple.java new file mode 100644 index 0000000..6e12c7f --- /dev/null +++ b/tuple/src/main/java/com/onixbyte/tuple/TriTuple.java @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2024-2025 OnixByte. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.onixbyte.tuple; + +import java.util.Objects; + +/** + * Represents an immutable triple of three elements, referred to as 'left', 'middle' and 'right'. + * This class provides a way to group three values of different types. + *

+ * The generic types {@code L}, {@code M} and {@code R} denote the types of the left, middle and right + * elements respectively. + * + * @param the type of the left element + * @param the type of the middle element + * @param the type of the right element + * @author siujamo + * @author zihluwang + */ +public final class TriTuple { + + /** + * The left element of the triple. + */ + private L left; + + /** + * The middle element of the triple. + */ + private M middle; + + /** + * The right element of the triple. + */ + private R right; + + /** + * Constructs a new {@code TriTuple} with the given left, middle and right elements. + * + * @param left the left element + * @param middle the middle element + * @param right the right element + */ + public TriTuple(L left, M middle, R right) { + this.left = left; + this.middle = middle; + this.right = right; + } + + /** + * Retrieves the left element of the triple. + * + * @return the left element + */ + public L getLeft() { + return left; + } + + /** + * Sets the left element of the triple. + * + * @param left the new left element + */ + public void setLeft(L left) { + this.left = left; + } + + /** + * Retrieves the middle element of the triple. + * + * @return the middle element + */ + public M getMiddle() { + return middle; + } + + /** + * Sets the middle element of the triple. + * + * @param middle the new middle element + */ + public void setMiddle(M middle) { + this.middle = middle; + } + + /** + * Retrieves the right element of the triple. + * + * @return the right element + */ + public R getRight() { + return right; + } + + /** + * Sets the right element of the triple. + * + * @param right the new right element + */ + public void setRight(R right) { + this.right = right; + } + + /** + * Checks if this {@code TriTuple} is equal to the specified object. + * Two {@code TriTuple}s are considered equal if their left, middle and right elements are equal. + * + * @param object the object to compare with + * @return {@code true} if the objects are equal, {@code false} otherwise + */ + @Override + public boolean equals(Object object) { + if (!(object instanceof TriTuple triTuple)) return false; + return Objects.equals(left, triTuple.left) && + Objects.equals(middle, triTuple.middle) && + Objects.equals(right, triTuple.right); + } + + /** + * Calculates the hash code for this {@code TriTuple} based on its left, middle and right elements. + * + * @return the hash code value for this object + */ + @Override + public int hashCode() { + return Objects.hash(left, middle, right); + } + + /** + * Returns a string representation of this {@code TriTuple}, including its left, middle and right elements. + * + * @return a string representation of the object + */ + @Override + public String toString() { + return "TriTuple{" + + "left=" + left + + ", middle=" + middle + + ", right=" + right + + '}'; + } + + /** + * Factory method to create a new {@code TriTuple} instance with the given left, middle and right elements. + * + * @param left the left element + * @param middle the middle element + * @param right the right element + * @param the type of the left element + * @param the type of the middle element + * @param the type of the right element + * @return a new {@code TriTuple} instance + */ + public static TriTuple of(L left, M middle, R right) { + return new TriTuple<>(left, middle, right); + } +}