refactor: split tuple module to repo onixbyte/onixbyte-tuple
This commit is contained in:
@@ -24,7 +24,6 @@ rootProject.name = "onixbyte-toolbox"
|
|||||||
|
|
||||||
include("version-catalogue")
|
include("version-catalogue")
|
||||||
include("common-toolbox")
|
include("common-toolbox")
|
||||||
include("tuple")
|
|
||||||
include("identity-generator")
|
include("identity-generator")
|
||||||
include("crypto-toolbox")
|
include("crypto-toolbox")
|
||||||
include("math-toolbox")
|
include("math-toolbox")
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
# Tuple
|
|
||||||
|
|
||||||
## Introduction
|
|
||||||
|
|
||||||
The `tuple` module provides simple and efficient implementations of mutable and immutable bi-tuples and tri-tuples. These tuples allow you to group two or three values together without creating custom classes, supporting convenient usage in various programming scenarios.
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- Immutable and mutable versions of bi-tuples (pairs) and tri-tuples (triplets);
|
|
||||||
- Factory method of() for easy instantiation of all tuple types;
|
|
||||||
- Simple, lightweight implementation compatible with standard Java usage;
|
|
||||||
- Clear distinction between mutable and immutable tuples for flexibility.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
### Maven
|
|
||||||
|
|
||||||
Add the following dependency to your `pom.xml`:
|
|
||||||
|
|
||||||
```xml
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.onixbyte</groupId>
|
|
||||||
<artifactId>tuple</artifactId>
|
|
||||||
<version>$artefactVersion</version>
|
|
||||||
</dependency>
|
|
||||||
```
|
|
||||||
|
|
||||||
### Gradle
|
|
||||||
|
|
||||||
#### Version Catalogue
|
|
||||||
|
|
||||||
Add the following codes to you `gradle/libs.versions.toml`:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[version]
|
|
||||||
onixbyteToolbox = "$artefactVersion"
|
|
||||||
|
|
||||||
[libraries]
|
|
||||||
onixbyteToolbox-tuple = { group = "com.onixbyte", name = "tuple", version.ref = "onixbyteToolbox" }
|
|
||||||
```
|
|
||||||
|
|
||||||
Then add the following codes to your `build.gradle.kts` or `build.gradle` dependencies block:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
implementation(libs.onixbyteToolbox.tuple)
|
|
||||||
```
|
|
||||||
|
|
||||||
```groovy
|
|
||||||
implementation libs.onixbyteToolbox.tuple
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Kotlin DSL
|
|
||||||
|
|
||||||
Add the following line to your `build.gradle.kts` dependencies block:
|
|
||||||
|
|
||||||
```kotlin
|
|
||||||
implementation("com.onixbyte:tuple:$artefactVersion")
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Groovy DSL
|
|
||||||
|
|
||||||
Add the following line to your `build.gradle` dependencies block:
|
|
||||||
|
|
||||||
```groovy
|
|
||||||
implementation 'com.onixbyte:tuple:${artefactVersion}'
|
|
||||||
```
|
|
||||||
|
|
||||||
## Dependencies
|
|
||||||
|
|
||||||
This module has no external dependencies other than the standard Java SDK.
|
|
||||||
|
|
||||||
## Acknowledgement
|
|
||||||
|
|
||||||
Thanks to all contributors who helped develop this module, improving its design, performance, and documentation over time.
|
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2024-2026 OnixByte
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.net.URI
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
java
|
|
||||||
id("java-library")
|
|
||||||
id("maven-publish")
|
|
||||||
id("signing")
|
|
||||||
}
|
|
||||||
|
|
||||||
val tupleVersion: String by project
|
|
||||||
|
|
||||||
version = tupleVersion
|
|
||||||
val projectUrl: String by project
|
|
||||||
val projectGithubUrl: String by project
|
|
||||||
val licenseName: String by project
|
|
||||||
val licenseUrl: String by project
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
java {
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
|
||||||
withSourcesJar()
|
|
||||||
withJavadocJar()
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<JavaCompile> {
|
|
||||||
options.encoding = "UTF-8"
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<Jar> {
|
|
||||||
exclude("logback.xml")
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
compileOnly(libs.slf4j)
|
|
||||||
implementation(libs.logback)
|
|
||||||
testImplementation(platform(libs.junit.bom))
|
|
||||||
testImplementation(libs.junit.jupiter)
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.test {
|
|
||||||
useJUnitPlatform()
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
|
||||||
publications {
|
|
||||||
create<MavenPublication>("tuple") {
|
|
||||||
groupId = group.toString()
|
|
||||||
artifactId = "tuple"
|
|
||||||
version = tupleVersion
|
|
||||||
|
|
||||||
pom {
|
|
||||||
name = "OnixByte Tuple"
|
|
||||||
description =
|
|
||||||
"The tuple module is designed to offer a convenient and efficient way to handle grouped data."
|
|
||||||
url = projectUrl
|
|
||||||
|
|
||||||
licenses {
|
|
||||||
license {
|
|
||||||
name = licenseName
|
|
||||||
url = licenseUrl
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
scm {
|
|
||||||
connection = "scm:git:git://github.com:onixbyte/onixbyte-toolbox.git"
|
|
||||||
developerConnection = "scm:git:git://github.com:onixbyte/onixbyte-toolbox.git"
|
|
||||||
url = projectGithubUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
developers {
|
|
||||||
developer {
|
|
||||||
id = "zihluwang"
|
|
||||||
name = "Zihlu Wang"
|
|
||||||
email = "really@zihlu.wang"
|
|
||||||
timezone = "Asia/Hong_Kong"
|
|
||||||
}
|
|
||||||
|
|
||||||
developer {
|
|
||||||
id = "siujamo"
|
|
||||||
name = "Siu Jam'o"
|
|
||||||
email = "jamo.siu@outlook.com"
|
|
||||||
timezone = "Asia/Shanghai"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
from(components["java"])
|
|
||||||
|
|
||||||
signing {
|
|
||||||
setRequired(project.hasProperty("signing.keyId"))
|
|
||||||
sign(publishing.publications["tuple"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
name = "sonatypeNexus"
|
|
||||||
url = URI(providers.gradleProperty("repo.maven-central.host").get())
|
|
||||||
credentials {
|
|
||||||
username = providers.gradleProperty("repo.maven-central.username").get()
|
|
||||||
password = providers.gradleProperty("repo.maven-central.password").get()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2024-2026 OnixByte
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.onixbyte.tuple;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an immutable triple of three elements, referred to as 'left', 'middle', and 'right'.
|
|
||||||
* This class provides a generic way to group three values without the need to create a custom class
|
|
||||||
* for each specific combination.
|
|
||||||
* <p>
|
|
||||||
* The generic types {@code L}, {@code M}, and {@code R} denote the types of the left, middle, and
|
|
||||||
* right elements, respectively. Instances of this class are immutable once created.
|
|
||||||
*
|
|
||||||
* @param <L> the type of the left element
|
|
||||||
* @param <M> the type of the middle element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @param left the left element of this triple
|
|
||||||
* @param middle the middle element of this triple
|
|
||||||
* @param right the right element of this triple
|
|
||||||
* @author siujamo
|
|
||||||
* @author zihluwang
|
|
||||||
*/
|
|
||||||
public record ImmutableTriple<L, M, R>(
|
|
||||||
L left,
|
|
||||||
M middle,
|
|
||||||
R right
|
|
||||||
) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new {@code ImmutableTriple} with the specified left, middle, and right elements.
|
|
||||||
*
|
|
||||||
* @param <L> the type of the left element
|
|
||||||
* @param <M> the type of the middle element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @param left the left element
|
|
||||||
* @param middle the middle element
|
|
||||||
* @param right the right element
|
|
||||||
* @return a new {@code ImmutableTriple} containing the specified elements
|
|
||||||
*/
|
|
||||||
public static <L, M, R> ImmutableTriple<L, M, R> of(L left, M middle, R right) {
|
|
||||||
return new ImmutableTriple<>(left, middle, right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2024-2026 OnixByte
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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.
|
|
||||||
* <p>
|
|
||||||
* 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 <L> the type of the left element
|
|
||||||
* @param <R> 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 ImmutableTuple<L, R>(
|
|
||||||
L left,
|
|
||||||
R right
|
|
||||||
) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new {@code ImmutableTuple} with the specified left and right elements.
|
|
||||||
*
|
|
||||||
* @param <L> the type of the left element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @param left the left element
|
|
||||||
* @param right the right element
|
|
||||||
* @return a new {@code ImmutableTuple} containing the specified elements
|
|
||||||
*/
|
|
||||||
public static <L, R> ImmutableTuple<L, R> of(L left, R right) {
|
|
||||||
return new ImmutableTuple<>(left, right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2024-2026 OnixByte
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.onixbyte.tuple;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a mutable triple of three elements, referred to as 'left', 'middle' and 'right'.
|
|
||||||
* This class provides a way to group three values of different types.
|
|
||||||
* <p>
|
|
||||||
* The generic types {@code L}, {@code M} and {@code R} denote the types of the left, middle and
|
|
||||||
* right elements respectively.
|
|
||||||
*
|
|
||||||
* @param <L> the type of the left element
|
|
||||||
* @param <M> the type of the middle element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @author siujamo
|
|
||||||
* @author zihluwang
|
|
||||||
*/
|
|
||||||
public final class Triple<L, M, R> {
|
|
||||||
|
|
||||||
private L left;
|
|
||||||
private M middle;
|
|
||||||
private R right;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new {@code Triple} with the given left, middle and right elements.
|
|
||||||
*
|
|
||||||
* @param left the left element
|
|
||||||
* @param middle the middle element
|
|
||||||
* @param right the right element
|
|
||||||
*/
|
|
||||||
public Triple(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 Triple} is equal to the specified object. Two {@code Triple}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 Triple<?, ?, ?> triple)) return false;
|
|
||||||
return Objects.equals(left, triple.left) &&
|
|
||||||
Objects.equals(middle, triple.middle) &&
|
|
||||||
Objects.equals(right, triple.right);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the hash code for this {@code Triple} 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 Triple}, including its left, middle and
|
|
||||||
* right elements.
|
|
||||||
*
|
|
||||||
* @return a string representation of the object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Triple{" +
|
|
||||||
"left=" + left +
|
|
||||||
", middle=" + middle +
|
|
||||||
", right=" + right +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method to create a new {@code Triple} 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 <L> the type of the left element
|
|
||||||
* @param <M> the type of the middle element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @return a new {@code Triple} instance
|
|
||||||
*/
|
|
||||||
public static <L, M, R> Triple<L, M, R> of(L left, M middle, R right) {
|
|
||||||
return new Triple<>(left, middle, right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2024-2026 OnixByte
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.onixbyte.tuple;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an ordered pair of elements, where the first element is of type {@code L} and the
|
|
||||||
* second is of type {@code R}. This class provides a simple way to group two related
|
|
||||||
* values together.
|
|
||||||
* <p>
|
|
||||||
* The class is mutable, allowing the values of the left and right elements to be changed
|
|
||||||
* after creation. It also overrides the {@code equals}, {@code hashCode}, and {@code toString}
|
|
||||||
* methods to provide meaningful comparisons and string representations.
|
|
||||||
*
|
|
||||||
* @param <L> the type of the left element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @author siujamo
|
|
||||||
* @author zihluwang
|
|
||||||
*/
|
|
||||||
public final class Tuple<L, R> {
|
|
||||||
|
|
||||||
private L left;
|
|
||||||
private R right;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new {@code Tuple} with the specified left and right elements.
|
|
||||||
*
|
|
||||||
* @param left the left element to be stored in the tuple
|
|
||||||
* @param right the right element to be stored in the tuple
|
|
||||||
*/
|
|
||||||
public Tuple(L left, R right) {
|
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the left element of the tuple.
|
|
||||||
*
|
|
||||||
* @return the left element of the tuple
|
|
||||||
*/
|
|
||||||
public L getLeft() {
|
|
||||||
return left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the left element of the tuple to the specified value.
|
|
||||||
*
|
|
||||||
* @param left the new value for the left element of the tuple
|
|
||||||
*/
|
|
||||||
public void setLeft(L left) {
|
|
||||||
this.left = left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves the right element of the tuple.
|
|
||||||
*
|
|
||||||
* @return the right element of the tuple
|
|
||||||
*/
|
|
||||||
public R getRight() {
|
|
||||||
return right;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the right element of the tuple to the specified value.
|
|
||||||
*
|
|
||||||
* @param right the new value for the right element of the tuple
|
|
||||||
*/
|
|
||||||
public void setRight(R right) {
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compares this {@code Tuple} with the specified object for equality.
|
|
||||||
* <p>
|
|
||||||
* Two {@code Tuple}s are considered equal if they are of the same type and their left and
|
|
||||||
* right elements are equal according to their respective {@code equals} methods.
|
|
||||||
*
|
|
||||||
* @param object the object to compare with this {@code Tuple}
|
|
||||||
* @return {@code true} if the specified object is equal to this {@code Tuple},
|
|
||||||
* {@code false} otherwise
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object object) {
|
|
||||||
if (!(object instanceof Tuple<?, ?> tuple)) return false;
|
|
||||||
return Objects.equals(left, tuple.left) && Objects.equals(right, tuple.right);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a hash code value for the {@code Tuple}.
|
|
||||||
* <p>
|
|
||||||
* The hash code is calculated based on the hash codes of the left and right elements.
|
|
||||||
*
|
|
||||||
* @return a hash code value for this {@code Tuple}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(left, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a string representation of the {@code Tuple}.
|
|
||||||
* <p>
|
|
||||||
* The string representation consists of the class name, followed by the values of
|
|
||||||
* the left and right elements in the format {@code "Tuple{left=value1, right=value2}"}.
|
|
||||||
*
|
|
||||||
* @return a string representation of the {@code Tuple}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Tuple{" +
|
|
||||||
"left=" + left +
|
|
||||||
", right=" + right +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new {@code Tuple} with the specified left and right elements.
|
|
||||||
*
|
|
||||||
* @param <L> the type of the left element
|
|
||||||
* @param <R> the type of the right element
|
|
||||||
* @param left the left element
|
|
||||||
* @param right the right element
|
|
||||||
* @return a new {@code Tuple} containing the specified elements
|
|
||||||
*/
|
|
||||||
public static <L, R> Tuple<L, R> of(L left, R right) {
|
|
||||||
return new Tuple<>(left, right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user