mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add Pair
This commit is contained in:
parent
1527d6e27c
commit
87a1b7e1a2
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||||
|
* Hutool is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.core.lang.mutable;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.tuple.Pair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可变二元组对象
|
||||||
|
*
|
||||||
|
* @param <L> 左值类型
|
||||||
|
* @param <R> 右值类型
|
||||||
|
* @author looly
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public class MutablePair<L, R> extends Pair<L, R> implements Mutable<MutablePair<L, R>>{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建MutablePair对象
|
||||||
|
*
|
||||||
|
* @param <L> 左值类型
|
||||||
|
* @param <R> 右值类型
|
||||||
|
* @param left 左值
|
||||||
|
* @param right 右值
|
||||||
|
* @return MutablePair
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public static <L, R> MutablePair<L, R> of(final L left, final R right) {
|
||||||
|
return new MutablePair<>(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param left 左值
|
||||||
|
* @param right 右值
|
||||||
|
*/
|
||||||
|
public MutablePair(final L left, final R right) {
|
||||||
|
super(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MutablePair<L, R> get() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(final MutablePair<L, R> value) {
|
||||||
|
this.left = value.left;
|
||||||
|
this.right = value.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置左值
|
||||||
|
*
|
||||||
|
* @param left 左值
|
||||||
|
*/
|
||||||
|
public void setLeft(final L left) {
|
||||||
|
this.left = left;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置右值
|
||||||
|
*
|
||||||
|
* @param right 右值
|
||||||
|
*/
|
||||||
|
public void setRight(final R right) {
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||||
|
* Hutool is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.core.lang.tuple;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.exception.CloneException;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 不可变二元组对象
|
||||||
|
*
|
||||||
|
* @param <L> 左值类型
|
||||||
|
* @param <R> 右值类型
|
||||||
|
* @author looly
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public class Pair<L, R> implements Serializable, Cloneable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建Pair对象
|
||||||
|
*
|
||||||
|
* @param <L> 左值类型
|
||||||
|
* @param <R> 右值类型
|
||||||
|
* @param left 左值
|
||||||
|
* @param right 右值
|
||||||
|
* @return Pair
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public static <L, R> Pair<L, R> of(final L left, final R right) {
|
||||||
|
return new Pair<>(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected L left;
|
||||||
|
protected R right;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param left 左值
|
||||||
|
* @param right 右值
|
||||||
|
*/
|
||||||
|
public Pair(final L left, final R right) {
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
// region ----- getXXX
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取左值
|
||||||
|
*
|
||||||
|
* @return 左值
|
||||||
|
*/
|
||||||
|
public L getLeft() {
|
||||||
|
return this.left;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取右值
|
||||||
|
*
|
||||||
|
* @return 右值
|
||||||
|
*/
|
||||||
|
public R getRight() {
|
||||||
|
return this.right;
|
||||||
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object o) {
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final Pair<?, ?> pair = (Pair<?, ?>) o;
|
||||||
|
return Objects.equals(left, pair.left) && Objects.equals(right, pair.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Pair{" +
|
||||||
|
"left=" + left +
|
||||||
|
", right=" + right +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public Pair<L, R> clone() {
|
||||||
|
try {
|
||||||
|
return (Pair<L, R>) super.clone();
|
||||||
|
} catch (final CloneNotSupportedException e) {
|
||||||
|
throw new CloneException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,9 +12,6 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.lang.tuple;
|
package org.dromara.hutool.core.lang.tuple;
|
||||||
|
|
||||||
import org.dromara.hutool.core.exception.CloneException;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,15 +23,13 @@ import java.util.Objects;
|
|||||||
* @author kirno7
|
* @author kirno7
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public class Triple<L, M, R> implements Serializable, Cloneable {
|
public class Triple<L, M, R> extends Pair<L, R> {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
protected L left;
|
|
||||||
protected M middle;
|
protected M middle;
|
||||||
protected R right;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建ImmutableTriple对象
|
* 构建Triple对象
|
||||||
*
|
*
|
||||||
* @param <L> 左值类型
|
* @param <L> 左值类型
|
||||||
* @param <M> 中值类型
|
* @param <M> 中值类型
|
||||||
@ -42,7 +37,7 @@ public class Triple<L, M, R> implements Serializable, Cloneable {
|
|||||||
* @param left 左值
|
* @param left 左值
|
||||||
* @param middle 中值
|
* @param middle 中值
|
||||||
* @param right 右值
|
* @param right 右值
|
||||||
* @return ImmutableTriple
|
* @return Triple
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public static <L, M, R> Triple<L, M, R> of(final L left, final M middle, final R right) {
|
public static <L, M, R> Triple<L, M, R> of(final L left, final M middle, final R right) {
|
||||||
@ -57,21 +52,11 @@ public class Triple<L, M, R> implements Serializable, Cloneable {
|
|||||||
* @param right 右值
|
* @param right 右值
|
||||||
*/
|
*/
|
||||||
public Triple(final L left, final M middle, final R right) {
|
public Triple(final L left, final M middle, final R right) {
|
||||||
this.left = left;
|
super(left, right);
|
||||||
this.middle = middle;
|
this.middle = middle;
|
||||||
this.right = right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// region ----- getXXX
|
// region ----- getXXX
|
||||||
/**
|
|
||||||
* 获取左值
|
|
||||||
*
|
|
||||||
* @return 左值
|
|
||||||
*/
|
|
||||||
public L getLeft() {
|
|
||||||
return this.left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取中值
|
* 获取中值
|
||||||
*
|
*
|
||||||
@ -80,15 +65,6 @@ public class Triple<L, M, R> implements Serializable, Cloneable {
|
|||||||
public M getMiddle() {
|
public M getMiddle() {
|
||||||
return this.middle;
|
return this.middle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取右值
|
|
||||||
*
|
|
||||||
* @return 右值
|
|
||||||
*/
|
|
||||||
public R getRight() {
|
|
||||||
return this.right;
|
|
||||||
}
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -107,23 +83,16 @@ public class Triple<L, M, R> implements Serializable, Cloneable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hashCode(getLeft())
|
return Objects.hash(this.left, this.middle, this.right);
|
||||||
^ Objects.hashCode(getMiddle())
|
|
||||||
^ Objects.hashCode(getRight());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Triple {" + "left=" + getLeft() + ", middle=" + getMiddle() + ", right=" + getRight() + '}';
|
return "Triple{" + "left=" + getLeft() + ", middle=" + getMiddle() + ", right=" + getRight() + '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public Triple<L, M, R> clone() {
|
public Triple<L, M, R> clone() {
|
||||||
try {
|
|
||||||
return (Triple<L, M, R>) super.clone();
|
return (Triple<L, M, R>) super.clone();
|
||||||
} catch (final CloneNotSupportedException e) {
|
|
||||||
throw new CloneException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||||
|
* Hutool is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.core.lang.tuple;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.mutable.MutablePair;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Pair} 二元组单元测试
|
||||||
|
* {@link MutablePair} 二元组单元测试
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
*/
|
||||||
|
public class PairTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mutablePairTest() {
|
||||||
|
final MutablePair<String, String> pair = MutablePair
|
||||||
|
.of("1", "1");
|
||||||
|
Assertions.assertEquals("Pair{left=1, right=1}", pair.toString());
|
||||||
|
|
||||||
|
pair.setLeft("2");
|
||||||
|
pair.setRight("2");
|
||||||
|
Assertions.assertEquals("Pair{left=2, right=2}", pair.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pairTest() {
|
||||||
|
final Pair<String, String> triple = Pair
|
||||||
|
.of("3", "3");
|
||||||
|
Assertions.assertEquals("Pair{left=3, right=3}", triple.toString());
|
||||||
|
}
|
||||||
|
}
|
@ -28,18 +28,18 @@ public class TripleTest {
|
|||||||
public void mutableTripleTest() {
|
public void mutableTripleTest() {
|
||||||
final MutableTriple<String, String, String> mutableTriple = MutableTriple
|
final MutableTriple<String, String, String> mutableTriple = MutableTriple
|
||||||
.of("1", "1", "1");
|
.of("1", "1", "1");
|
||||||
Assertions.assertEquals("Triple {left=1, middle=1, right=1}", mutableTriple.toString());
|
Assertions.assertEquals("Triple{left=1, middle=1, right=1}", mutableTriple.toString());
|
||||||
|
|
||||||
mutableTriple.setLeft("2");
|
mutableTriple.setLeft("2");
|
||||||
mutableTriple.setMiddle("2");
|
mutableTriple.setMiddle("2");
|
||||||
mutableTriple.setRight("2");
|
mutableTriple.setRight("2");
|
||||||
Assertions.assertEquals("Triple {left=2, middle=2, right=2}", mutableTriple.toString());
|
Assertions.assertEquals("Triple{left=2, middle=2, right=2}", mutableTriple.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tripleTest() {
|
public void tripleTest() {
|
||||||
final Triple<String, String, String> triple = Triple
|
final Triple<String, String, String> triple = Triple
|
||||||
.of("3", "3", "3");
|
.of("3", "3", "3");
|
||||||
Assertions.assertEquals("Triple {left=3, middle=3, right=3}", triple.toString());
|
Assertions.assertEquals("Triple{left=3, middle=3, right=3}", triple.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user