Source code
Description: source: package: org.apache.commons.lang3.tuple, class: ImmutablePair

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.tuple;
    018
    019import java.util.Map;
    020import java.util.Objects;
    021
    022/**
    023 * An immutable pair consisting of two {@link Object} elements.
    024 *
    025 * <p>Although the implementation is immutable, there is no restriction on the objects
    026 * that may be stored. If mutable objects are stored in the pair, then the pair
    027 * itself effectively becomes mutable.</p>
    028 *
    029 * <p>#ThreadSafe# if both paired objects are thread-safe</p>
    030 *
    031 * @param <L> the left element type
    032 * @param <R> the right element type
    033 * @since 3.0
    034 */
    035public class ImmutablePair<L, R> extends Pair<L, R> {
    036
    037    /**
    038     * An empty array.
    039     * <p>
    040     * Consider using {@link #emptyArray()} to avoid generics warnings.
    041     * </p>
    042     *
    043     * @since 3.10
    044     */
    045    public static final ImmutablePair<?, ?>[] EMPTY_ARRAY = {};
    046
    047    /**
    048     * An immutable pair of nulls.
    049     */
    050    // This is not defined with generics to avoid warnings in call sites.
    051    @SuppressWarnings("rawtypes")
    052    private static final ImmutablePair NULL = new ImmutablePair<>(null, null);
    053
    054    /** Serialization version */
    055    private static final long serialVersionUID = 4954918890077093841L;
    056
    057    /**
    058     * Returns the empty array singleton that can be assigned without compiler warning.
    059     *
    060     * @param <L> the left element type
    061     * @param <R> the right element type
    062     * @return the empty array singleton that can be assigned without compiler warning.
    063     * @since 3.10
    064     */
    065    @SuppressWarnings("unchecked")
    066    public static <L, R> ImmutablePair<L, R>[] emptyArray() {
    067        return (ImmutablePair<L, R>[]) EMPTY_ARRAY;
    068    }
    069
    070    /**
    071     * Creates an immutable pair of two objects inferring the generic types.
    072     *
    073     * @param <L> the left element type.
    074     * @param <R> the right element type.
    075     * @param left  the left element, may be null.
    076     * @return an immutable formed from the two parameters, not null.
    077     * @since 3.11
    078     */
    079    public static <L, R> Pair<L, R> left(final L left) {
    080        return of(left, null);
    081    }
    082
    083    /**
    084     * Returns an immutable pair of nulls.
    085     *
    086     * @param <L> the left element of this pair. Value is {@code null}.
    087     * @param <R> the right element of this pair. Value is {@code null}.
    088     * @return an immutable pair of nulls.
    089     * @since 3.6
    090     */
    091    @SuppressWarnings("unchecked")
    092    public static <L, R> ImmutablePair<L, R> nullPair() {
    093        return NULL;
    094    }
    095
    096    /**
    097     * Creates an immutable pair of two objects inferring the generic types.
    098     *
    099     * @param <L> the left element type.
    100     * @param <R> the right element type.
    101     * @param left  the left element, may be null.
    102     * @param right  the right element, may be null.
    103     * @return an immutable formed from the two parameters, not null.
    104     */
    105    public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
    106        return left != null || right != null ? new ImmutablePair<>(left, right) : nullPair();
    107    }
    108
    109    /**
    110     * Creates an immutable pair from a map entry.
    111     *
    112     * @param <L> the left element type.
    113     * @param <R> the right element type.
    114     * @param pair the existing map entry.
    115     * @return an immutable formed from the map entry.
    116     * @since 3.10
    117     */
    118    public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
    119        return pair != null ? new ImmutablePair<>(pair.getKey(), pair.getValue()) : nullPair();
    120    }
    121
    122    /**
    123     * Creates an immutable pair of two non-null objects inferring the generic types.
    124     *
    125     * @param <L> the left element type.
    126     * @param <R> the right element type.
    127     * @param left  the left element, may not be null.
    128     * @param right  the right element, may not  be null.
    129     * @return an immutable formed from the two parameters, not null.
    130     * @throws NullPointerException if any input is null.
    131     * @since 3.13.0
    132     */
    133    public static <L, R> ImmutablePair<L, R> ofNonNull(final L left, final R right) {
    134        return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
    135    }
    136
    137    /**
    138     * Creates an immutable pair of two objects inferring the generic types.
    139     *
    140     * @param <L> the left element type.
    141     * @param <R> the right element type.
    142     * @param right  the right element, may be null.
    143     * @return an immutable formed from the two parameters, not null.
    144     * @since 3.11
    145     */
    146    public static <L, R> Pair<L, R> right(final R right) {
    147        return of(null, right);
    148    }
    149
    150    /** Left object */
    151    public final L left;
    152
    153    /** Right object */
    154    public final R right;
    155
    156    /**
    157     * Create a new pair instance.
    158     *
    159     * @param left  the left value, may be null
    160     * @param right  the right value, may be null
    161     */
    162    public ImmutablePair(final L left, final R right) {
    163        this.left = left;
    164        this.right = right;
    165    }
    166
    167    /**
    168     * {@inheritDoc}
    169     */
    170    @Override
    171    public L getLeft() {
    172        return left;
    173    }
    174
    175    /**
    176     * {@inheritDoc}
    177     */
    178    @Override
    179    public R getRight() {
    180        return right;
    181    }
    182
    183    /**
    184     * Throws {@link UnsupportedOperationException}.
    185     *
    186     * <p>This pair is immutable, so this operation is not supported.</p>
    187     *
    188     * @param value  the value to set
    189     * @return never
    190     * @throws UnsupportedOperationException as this operation is not supported
    191     */
    192    @Override
    193    public R setValue(final R value) {
    194        throw new UnsupportedOperationException();
    195    }
    196
    197}