|
| 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 immutable triple of three elements, referred to as 'left', 'middle' and 'right'. |
| 24 | + * This class provides a way to group three values of different types. |
| 25 | + * <p> |
| 26 | + * The generic types {@code L}, {@code M} and {@code R} denote the types of the left, middle and right |
| 27 | + * elements respectively. |
| 28 | + * |
| 29 | + * @param <L> the type of the left element |
| 30 | + * @param <M> the type of the middle element |
| 31 | + * @param <R> the type of the right element |
| 32 | + * @author siujamo |
| 33 | + * @author zihluwang |
| 34 | + */ |
| 35 | +public final class TriTuple<L, M, R> { |
| 36 | + |
| 37 | + /** |
| 38 | + * The left element of the triple. |
| 39 | + */ |
| 40 | + private L left; |
| 41 | + |
| 42 | + /** |
| 43 | + * The middle element of the triple. |
| 44 | + */ |
| 45 | + private M middle; |
| 46 | + |
| 47 | + /** |
| 48 | + * The right element of the triple. |
| 49 | + */ |
| 50 | + private R right; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructs a new {@code TriTuple} with the given left, middle and right elements. |
| 54 | + * |
| 55 | + * @param left the left element |
| 56 | + * @param middle the middle element |
| 57 | + * @param right the right element |
| 58 | + */ |
| 59 | + public TriTuple(L left, M middle, R right) { |
| 60 | + this.left = left; |
| 61 | + this.middle = middle; |
| 62 | + this.right = right; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieves the left element of the triple. |
| 67 | + * |
| 68 | + * @return the left element |
| 69 | + */ |
| 70 | + public L getLeft() { |
| 71 | + return left; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sets the left element of the triple. |
| 76 | + * |
| 77 | + * @param left the new left element |
| 78 | + */ |
| 79 | + public void setLeft(L left) { |
| 80 | + this.left = left; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Retrieves the middle element of the triple. |
| 85 | + * |
| 86 | + * @return the middle element |
| 87 | + */ |
| 88 | + public M getMiddle() { |
| 89 | + return middle; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets the middle element of the triple. |
| 94 | + * |
| 95 | + * @param middle the new middle element |
| 96 | + */ |
| 97 | + public void setMiddle(M middle) { |
| 98 | + this.middle = middle; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Retrieves the right element of the triple. |
| 103 | + * |
| 104 | + * @return the right element |
| 105 | + */ |
| 106 | + public R getRight() { |
| 107 | + return right; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the right element of the triple. |
| 112 | + * |
| 113 | + * @param right the new right element |
| 114 | + */ |
| 115 | + public void setRight(R right) { |
| 116 | + this.right = right; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Checks if this {@code TriTuple} is equal to the specified object. |
| 121 | + * Two {@code TriTuple}s are considered equal if their left, middle and right elements are equal. |
| 122 | + * |
| 123 | + * @param object the object to compare with |
| 124 | + * @return {@code true} if the objects are equal, {@code false} otherwise |
| 125 | + */ |
| 126 | + @Override |
| 127 | + public boolean equals(Object object) { |
| 128 | + if (!(object instanceof TriTuple<?, ?, ?> triTuple)) return false; |
| 129 | + return Objects.equals(left, triTuple.left) && |
| 130 | + Objects.equals(middle, triTuple.middle) && |
| 131 | + Objects.equals(right, triTuple.right); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Calculates the hash code for this {@code TriTuple} based on its left, middle and right elements. |
| 136 | + * |
| 137 | + * @return the hash code value for this object |
| 138 | + */ |
| 139 | + @Override |
| 140 | + public int hashCode() { |
| 141 | + return Objects.hash(left, middle, right); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns a string representation of this {@code TriTuple}, including its left, middle and right elements. |
| 146 | + * |
| 147 | + * @return a string representation of the object |
| 148 | + */ |
| 149 | + @Override |
| 150 | + public String toString() { |
| 151 | + return "TriTuple{" + |
| 152 | + "left=" + left + |
| 153 | + ", middle=" + middle + |
| 154 | + ", right=" + right + |
| 155 | + '}'; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Factory method to create a new {@code TriTuple} instance with the given left, middle and right elements. |
| 160 | + * |
| 161 | + * @param left the left element |
| 162 | + * @param middle the middle element |
| 163 | + * @param right the right element |
| 164 | + * @param <L> the type of the left element |
| 165 | + * @param <M> the type of the middle element |
| 166 | + * @param <R> the type of the right element |
| 167 | + * @return a new {@code TriTuple} instance |
| 168 | + */ |
| 169 | + public static <L, M, R> TriTuple<L, M, R> of(L left, M middle, R right) { |
| 170 | + return new TriTuple<>(left, middle, right); |
| 171 | + } |
| 172 | +} |
0 commit comments