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 *      http://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 *
    034 * @since 3.0
    035 */
    036public class ImmutablePair<L, R> extends Pair<L, R> {
    037
    038    /**
    039     * An empty array.
    040     * <p>
    041     * Consider using {@link #emptyArray()} to avoid generics warnings.
    042     * </p>
    043     *
    044     * @since 3.10.
    045     */
    046    public static final ImmutablePair<?, ?>[] EMPTY_ARRAY = {};
    047
    048    /**
    049     * An immutable pair of nulls.
    050     */
    051    // This is not defined with generics to avoid warnings in call sites.
    052    @SuppressWarnings("rawtypes")
    053    private static final ImmutablePair NULL = new ImmutablePair<>(null, null);
    054
    055    /** Serialization version */
    056    private static final long serialVersionUID = 4954918890077093841L;
    057
    058    /**
    059     * Returns the empty array singleton that can be assigned without compiler warning.
    060     *
    061     * @param <L> the left element type
    062     * @param <R> the right element type
    063     * @return the empty array singleton that can be assigned without compiler warning.
    064     *
    065     * @since 3.10.
    066     */
    067    @SuppressWarnings("unchecked")
    068    public static <L, R> ImmutablePair<L, R>[] emptyArray() {
    069        return (ImmutablePair<L, R>[]) EMPTY_ARRAY;
    070    }
    071
    072    /**
    073     * Creates an immutable pair of two objects inferring the generic types.
    074     *
    075     * <p>This factory allows the pair to be created using inference to
    076     * obtain the generic types.</p>
    077     *
    078     * @param <L> the left element type
    079     * @param <R> the right element type
    080     * @param left  the left element, may be null
    081     * @return a pair formed from the two parameters, not null
    082     * @since 3.11
    083     */
    084    public static <L, R> Pair<L, R> left(final L left) {
    085        return of(left, null);
    086    }
    087
    088    /**
    089     * Returns an immutable pair of nulls.
    090     *
    091     * @param <L> the left element of this pair. Value is {@code null}.
    092     * @param <R> the right element of this pair. Value is {@code null}.
    093     * @return an immutable pair of nulls.
    094     * @since 3.6
    095     */
    096    @SuppressWarnings("unchecked")
    097    public static <L, R> ImmutablePair<L, R> nullPair() {
    098        return NULL;
    099    }
    100
    101    /**
    102     * Creates an immutable pair of two objects inferring the generic types.
    103     *
    104     * <p>This factory allows the pair to be created using inference to
    105     * obtain the generic types.</p>
    106     *
    107     * @param <L> the left element type
    108     * @param <R> the right element type
    109     * @param left  the left element, may be null
    110     * @param right  the right element, may be null
    111     * @return a pair formed from the two parameters, not null
    112     */
    113    public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
    114        return left != null || right != null ? new ImmutablePair<>(left, right) : nullPair();
    115    }
    116
    117    /**
    118     * Creates an immutable pair from a map entry.
    119     *
    120     * <p>This factory allows the pair to be created using inference to
    121     * obtain the generic types.</p>
    122     *
    123     * @param <L> the left element type
    124     * @param <R> the right element type
    125     * @param pair the existing map entry.
    126     * @return a pair formed from the map entry
    127     * @since 3.10
    128     */
    129    public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
    130        return pair != null ? new ImmutablePair<>(pair.getKey(), pair.getValue()) : nullPair();
    131    }
    132
    133    /**
    134     * Creates an immutable pair of two non-null objects inferring the generic types.
    135     *
    136     * <p>This factory allows the pair to be created using inference to
    137     * obtain the generic types.</p>
    138     *
    139     * @param <L> the left element type
    140     * @param <R> the right element type
    141     * @param left  the left element, may not be null
    142     * @param right  the right element, may not  be null
    143     * @return a pair formed from the two parameters, not null
    144     * @throws NullPointerException if any input is null
    145     * @since 3.13.0
    146     */
    147    public static <L, R> ImmutablePair<L, R> ofNonNull(final L left, final R right) {
    148        return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
    149    }
    150
    151    /**
    152     * Creates an immutable pair of two objects inferring the generic types.
    153     *
    154     * <p>This factory allows the pair to be created using inference to
    155     * obtain the generic types.</p>
    156     *
    157     * @param <L> the left element type
    158     * @param <R> the right element type
    159     * @param right  the right element, may be null
    160     * @return a pair formed from the two parameters, not null
    161     * @since 3.11
    162     */
    163    public static <L, R> Pair<L, R> right(final R right) {
    164        return of(null, right);
    165    }
    166
    167    /** Left object */
    168    public final L left;
    169
    170    /** Right object */
    171    public final R right;
    172
    173    /**
    174     * Create a new pair instance.
    175     *
    176     * @param left  the left value, may be null
    177     * @param right  the right value, may be null
    178     */
    179    public ImmutablePair(final L left, final R right) {
    180        this.left = left;
    181        this.right = right;
    182    }
    183
    184    /**
    185     * {@inheritDoc}
    186     */
    187    @Override
    188    public L getLeft() {
    189        return left;
    190    }
    191
    192    /**
    193     * {@inheritDoc}
    194     */
    195    @Override
    196    public R getRight() {
    197        return right;
    198    }
    199
    200    /**
    201     * Throws {@link UnsupportedOperationException}.
    202     *
    203     * <p>This pair is immutable, so this operation is not supported.</p>
    204     *
    205     * @param value  the value to set
    206     * @return never
    207     * @throws UnsupportedOperationException as this operation is not supported
    208     */
    209    @Override
    210    public R setValue(final R value) {
    211        throw new UnsupportedOperationException();
    212    }
    213
    214}