您的位置:首页 > 编程语言 > Java开发

(二十七)Java组件类Triple、MutableTriple、ImmutableTriple存储三个对象类详解

2018-02-10 18:20 369 查看
组件类是在包org.apache.commons.lang3.tuple下;

1.Triple抽象类,实现这个抽象类的类能够存储三个对象

package org.apache.commons.lang3.tuple;
import java.io.Serializable;
import java.util.Objects;

import org.apache.commons.lang3.builder.CompareToBuilder;

public abstract class Triple<L, M, R> implements Comparable<Triple<L, M, R>>, Serializable {

/** Serialization version */
private static final long serialVersionUID = 1L;

/**
* 静态方法,返回不可变的存储三个元素的组件对象
*/
public static <L, M, R> Triple<L, M, R> of(final L left, final M middle, final R right) {
return new ImmutableTriple<>(left, middle, right);
}

/**
* 获取 左元素
*/
public abstract L getLeft();

/**
* 获取中元素
*/
public abstract M getMiddle();

/**
* 获取右元素
*/
public abstract R getRight();

/**
* 比较组件中三个元素的值大小
*/
@Override
public int compareTo(final Triple<L, M, R> other) {
return new CompareToBuilder().append(getLeft(), other.getLeft())
.append(getMiddle(), other.getMiddle())
.append(getRight(), other.getRight()).toComparison();
}

/**
* 比较三个组件中三个对象值是否相等
*/
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Triple<?, ?, ?>) {
final Triple<?, ?, ?> other = (Triple<?, ?, ?>) obj;
return Objects.equals(getLeft(), other.getLeft())
&& Objects.equals(getMiddle(), other.getMiddle())
&& Objects.equals(getRight(), other.getRight());
}
return false;
}

/**
* 返回组件对象的哈希吗
*/
@Override
public int hashCode() {
return (getLeft() == null ? 0 : getLeft().hashCode()) ^
(getMiddle() == null ? 0 : getMiddle().hashCode()) ^
(getRight() == null ? 0 : getRight().hashCode());
}

/**
* 打印组件的三个对象
*/
@Override
public String toString() {
return "(" + getLeft() + "," + getMiddle() + "," + getRight() + ")";
}

/**
* 格式化三个组件的对象
*/
public String toString(final String format) {
return String.format(format, getLeft(), getMiddle(), getRight());
}

}


2.ImmutableTriple不可变组件对象

package org.apache.commons.lang3.tuple;

public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {

/**
* 返回一个三个对象都是null的不可变组件
*/
@SuppressWarnings("rawtypes")
private static final ImmutableTriple NULL = ImmutableTriple.of(null, null, null);

/** Serialization version */
private static final long serialVersionUID = 1L;

/**
* 静态方法,返回三个元素都为null的不可变的组件对象
*/
@SuppressWarnings("unchecked")
public static <L, M, R> ImmutableTriple<L, M, R> nullTriple() {
return NULL;
}

/** Left object */
public final L left;
/** Middle object */
public final M middle;
/** Right object */
public final R right;

/**
* 静态方法,返回一个三个指定对象的组件对象
*/
public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
return new ImmutableTriple<>(left, middle, right);
}

/**
* 传递三个对象的构造函数
*/
public ImmutableTriple(final L left, final M middle, final R right) {
super();
this.left = left;
this.middle = middle;
this.right = right;
}

/**
* 返回做元素
*/
@Override
public L getLeft() {
return left;
}

/**
* 返回中间元素
*/
@Override
public M getMiddle() {
return middle;
}

/**
* 返回右元素
*/
@Override
public R getRight() {
return right;
}
}


3.MutableTriple可改变值的三个元素组件对象

package org.apache.commons.lang3.tuple;
public class MutableTriple<L, M, R> extends Triple<L, M, R> {

/** Serialization version */
private static final long serialVersionUID = 1L;

/** Left object */
public L left;
/** Middle object */
public M middle;
/** Right object */
public R right;

/**
* 静态方法,返回三个元素对象的组件对象
*/
public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) {
return new MutableTriple<>(left, middle, right);
}

/**
* 无参数构造函数
*/
public MutableTriple() {
super();
}

/**
* 三个参数的构造函数
*/
public MutableTriple(final L left, final M middle, final R right) {
super();
this.left = left;
this.middle = middle;
this.right = right;
}

/**
* 返回左元素
*/
@Override
public L getLeft() {
return left;
}

/**
* 返回右元素
*/
public void setLeft(final L left) {
this.left = left;
}

/**
* 返回中间元素
*/
@Override
public M getMiddle() {
return middle;
}

/**
* 修改中间对象值
*/
public void setMiddle(final M middle) {
this.middle = middle;
}

/**
* 返回右元素 值
*/
@Override
public R getRight() {
return right;
}

/**
* 修改右元素对象值
*/
public void setRight(final R right) {
this.right = right;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐