com.landawn.abacus.util.Pair Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2015, Haiyang Li.
*
* 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.landawn.abacus.util;
import java.util.AbstractMap;
import java.util.Map;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.stream.Stream;
/**
*
* @author Haiyang Li
* @param
* @param
* @since 0.8
*/
public final class Pair implements Mutable {
// implements Map.Entry {
public L left;
public R right;
public Pair() {
}
Pair(final L l, final R r) {
this.left = l;
this.right = r;
}
/**
*
* @param
* @param
* @param l
* @param r
* @return
*/
public static Pair of(final L l, final R r) {
return new Pair<>(l, r);
}
/**
*
* @param the key type
* @param the value type
* @param entry
* @return
*/
public static Pair from(final Map.Entry entry) {
return new Pair<>(entry.getKey(), entry.getValue());
}
private static final Pair, ?>[] EMPTY_ARRAY = new Pair[0];
@SuppressWarnings("unchecked")
public static Pair[] emptyArray() {
return (Pair[]) EMPTY_ARRAY;
}
/**
* Gets the left.
*
* @return
*/
public L getLeft() {
return left;
}
/**
* Sets the left.
*
* @param left the new left
*/
public void setLeft(final L left) {
this.left = left;
}
/**
* Gets the right.
*
* @return
*/
public R getRight() {
return right;
}
/**
* Sets the right.
*
* @param right the new right
*/
public void setRight(final R right) {
this.right = right;
}
/**
*
* @param left
* @param right
*/
public void set(final L left, final R right) {
this.left = left;
this.right = right;
}
/**
* Gets the and set left.
*
* @param newLeft
* @return
*/
public L getAndSetLeft(L newLeft) {
final L res = left;
left = newLeft;
return res;
}
/**
* Sets the and get left.
*
* @param newLeft
* @return
*/
public L setAndGetLeft(L newLeft) {
left = newLeft;
return left;
}
/**
* Gets the and set right.
*
* @param newRight
* @return
*/
public R getAndSetRight(R newRight) {
final R res = newRight;
right = newRight;
return res;
}
/**
* Sets the and get right.
*
* @param newRight
* @return
*/
public R setAndGetRight(R newRight) {
right = newRight;
return right;
}
/**
* Set to the specified newLeft
and returns true
* if predicate
returns true. Otherwise returns
* false
without setting the value to new value.
*
* @param
* @param newLeft
* @param predicate - the first parameter is current pair, the second
* parameter is the newLeft
* @return
* @throws E the e
*/
public boolean setLeftIf(final L newLeft, Throwables.BiPredicate super Pair, ? super L, E> predicate) throws E {
if (predicate.test(this, newLeft)) {
this.left = newLeft;
return true;
}
return false;
}
/**
* Set to the specified newRight
and returns true
* if predicate
returns true. Otherwise returns
* false
without setting the value to new value.
*
* @param
* @param newRight
* @param predicate - the first parameter is current pair, the second
* parameter is the newRight
* @return
* @throws E the e
*/
public boolean setRightIf(final R newRight, Throwables.BiPredicate super Pair, ? super R, E> predicate) throws E {
if (predicate.test(this, newRight)) {
this.right = newRight;
return true;
}
return false;
}
/**
* Set to the specified newLeft
and newRight
and returns true
* if predicate
returns true. Otherwise returns
* false
without setting the left/right to new values.
*
* @param
* @param newLeft
* @param newRight
* @param predicate - the first parameter is current pair, the second
* parameter is the newLeft
, the third parameter is the newRight
.
* @return
* @throws E the e
*/
public boolean setIf(final L newLeft, final R newRight,
Throwables.TriPredicate super Pair, ? super L, ? super R, E> predicate) throws E {
if (predicate.test(this, newLeft, newRight)) {
this.left = newLeft;
this.right = newRight;
return true;
}
return false;
}
//
// /**
// *
// * @deprecated don't access this method by {@code Pair} interface.
// */
// @Deprecated
// @Override
// public L getKey() {
// return left;
// }
//
// /**
// *
// * @deprecated don't access this method by {@code Pair} interface.
// */
// @Deprecated
// @Override
// public R getValue() {
// return right;
// }
//
// /**
// *
// * @deprecated don't access this method by {@code Pair} interface.
// */
// @Deprecated
// @Override
// public R setValue(R value) {
// R oldValue = this.right;
// this.right = value;
//
// return oldValue;
// }
// public R getAndSetValue(R newRight) {
// return getAndSetRight(newRight);
// }
//
// public R setAndGetValue(R newRight) {
// return setAndGetRight(newRight);
// }
//
// /**
// *
// * @param newRight
// * @param predicate
// * @return
// * @see #setRightIf(Object, BiPredicate)
// */
// public boolean setValueIf(final R newRight, BiPredicate super Pair, ? super R> predicate) {
// return setRightIf(newRight, predicate);
// }
/**
* Returns a new instance of Pair<R, L>.
*
* @return a new instance of Pair<R, L>.
*/
@Beta
public Pair reverse() {
return new Pair<>(this.right, this.left);
}
public Pair copy() {
return new Pair<>(this.left, this.right);
}
public Object[] toArray() {
return new Object[] { left, right };
}
/**
*
* @param
* @param a
* @return
*/
public A[] toArray(A[] a) {
if (a.length < 2) {
a = N.copyOf(a, 2);
}
a[0] = (A) left;
a[1] = (A) right;
return a;
}
/**
*
* @param
* @param comsumer
* @throws E the e
*/
public void forEach(Throwables.Consumer, E> comsumer) throws E {
final Throwables.Consumer