From f6eb4bdad2b7c1edc2614cd3147af23518a75da8 Mon Sep 17 00:00:00 2001 From: siujamo Date: Thu, 17 Jul 2025 09:45:26 +0800 Subject: [PATCH] feat: add immutable bi tuple --- .../com/onixbyte/tuple/ImmutableBiTuple.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tuple/src/main/java/com/onixbyte/tuple/ImmutableBiTuple.java diff --git a/tuple/src/main/java/com/onixbyte/tuple/ImmutableBiTuple.java b/tuple/src/main/java/com/onixbyte/tuple/ImmutableBiTuple.java new file mode 100644 index 0000000..48b38b3 --- /dev/null +++ b/tuple/src/main/java/com/onixbyte/tuple/ImmutableBiTuple.java @@ -0,0 +1,52 @@ +/* + * 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; + +/** + * Represents an immutable pair of two elements, referred to as 'left' and 'right'. This class + * provides a simple way to group two values without the need to create a custom class for each + * specific pair. + *

+ * The generic types {@code L} and {@code R} denote the types of the left and right elements, + * respectively. Instances of this class are immutable once created. + * + * @param the type of the left element + * @param the type of the right element + * @param left the left element of this tuple + * @param right the right element of this tuple + * @author siujamo + * @author zihluwang + */ +public record ImmutableBiTuple( + L left, + R right +) { + + /** + * Creates a new {@code ImmutableBiTuple} with the specified left and right elements. + * + * @param the type of the left element + * @param the type of the right element + * @param left the left element + * @param right the right element + * @return a new {@code ImmutableBiTuple} containing the specified elements + */ + public static ImmutableBiTuple of(L left, R right) { + return new ImmutableBiTuple<>(left, right); + } +}