    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;
    020
    021/**
    022 * <p>An immutable pair consisting of two {@code Object} elements.</p>
    023 *
    024 * <p>Although the implementation is immutable, there is no restriction on the objects
    025 * that may be stored. If mutable objects are stored in the pair, then the pair
    026 * itself effectively becomes mutable. The class is also {@code final}, so a subclass
    027 * can not add undesirable behavior.</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 final 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 = new ImmutablePair[0];
    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 = of(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     * <p>Creates an immutable pair of two objects inferring the generic types.</p>
    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 ImmutablePair.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    public static <L, R> ImmutablePair<L, R> nullPair() {
    097        return NULL;
    098    }
    099
    100    /**
    101     * <p>Creates an immutable pair of two objects inferring the generic types.</p>
    102     *
    103     * <p>This factory allows the pair to be created using inference to
    104     * obtain the generic types.</p>
    105     *
    106     * @param <L> the left element type
    107     * @param <R> the right element type
    108     * @param left  the left element, may be null
    109     * @param right  the right element, may be null
    110     * @return a pair formed from the two parameters, not null
    111     */
    112    public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
    113        return new ImmutablePair<>(left, right);
    114    }
    115
    116    /**
    117     * <p>Creates an immutable pair from an existing pair.</p>
    118     *
    119     * <p>This factory allows the pair to be created using inference to
    120     * obtain the generic types.</p>
    121     *
    122     * @param <L> the left element type
    123     * @param <R> the right element type
    124     * @param pair the existing pair.
    125     * @return a pair formed from the two parameters, not null
    126     * @since 3.10
    127     */
    128    public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
    129        final L left;
    130        final R right;
    131        if (pair != null) {
    132            left = pair.getKey();
    133            right = pair.getValue();
    134        } else {
    135            left = null;
    136            right = null;
    137        }
    138        return new ImmutablePair<>(left, right);
    139    }
    140
    141    /**
    142     * <p>Creates an immutable pair of two objects inferring the generic types.</p>
    143     *
    144     * <p>This factory allows the pair to be created using inference to
    145     * obtain the generic types.</p>
    146     *
    147     * @param <L> the left element type
    148     * @param <R> the right element type
    149     * @param right  the right element, may be null
    150     * @return a pair formed from the two parameters, not null
    151     * @since 3.11
    152     */
    153    public static <L, R> Pair<L, R> right(final R right) {
    154        return ImmutablePair.of(null, right);
    155    }
    156
    157    /** Left object */
    158    public final L left;
    159
    160    /** Right object */
    161    public final R right;
    162
    163    /**
    164     * Create a new pair instance.
    165     *
    166     * @param left  the left value, may be null
    167     * @param right  the right value, may be null
    168     */
    169    public ImmutablePair(final L left, final R right) {
    170        super();
    171        this.left = left;
    172        this.right = right;
    173    }
    174
    175    /**
    176     * {@inheritDoc}
    177     */
    178    @Override
    179    public L getLeft() {
    180        return left;
    181    }
    182
    183    /**
    184     * {@inheritDoc}
    185     */
    186    @Override
    187    public R getRight() {
    188        return right;
    189    }
    190
    191    /**
    192     * <p>Throws {@code UnsupportedOperationException}.</p>
    193     *
    194     * <p>This pair is immutable, so this operation is not supported.</p>
    195     *
    196     * @param value  the value to set
    197     * @return never
    198     * @throws UnsupportedOperationException as this operation is not supported
    199     */
    200    @Override
    201    public R setValue(final R value) {
    202        throw new UnsupportedOperationException();
    203    }
    204
    205}