    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
    019/**
    020 * <p>An immutable pair consisting of two {@code Object} elements.</p>
    021 *
    022 * <p>Although the implementation is immutable, there is no restriction on the objects
    023 * that may be stored. If mutable objects are stored in the pair, then the pair
    024 * itself effectively becomes mutable. The class is also {@code final}, so a subclass
    025 * can not add undesirable behaviour.</p>
    026 *
    027 * <p>#ThreadSafe# if both paired objects are thread-safe</p>
    028 *
    029 * @param <L> the left element type
    030 * @param <R> the right element type
    031 *
    032 * @since 3.0
    033 */
    034public final class ImmutablePair<L, R> extends Pair<L, R> {
    035
    036    /**
    037     * An immutable pair of nulls.
    038     */
    039    // This is not defined with generics to avoid warnings in call sites.
    040    @SuppressWarnings("rawtypes")
    041    private static final ImmutablePair NULL = of(null, null);
    042
    043    /** Serialization version */
    044    private static final long serialVersionUID = 4954918890077093841L;
    045
    046    /**
    047     * Returns an immutable pair of nulls.
    048     *
    049     * @param <L> the left element of this pair. Value is {@code null}.
    050     * @param <R> the right element of this pair. Value is {@code null}.
    051     * @return an immutable pair of nulls.
    052     * @since 3.6
    053     */
    054    @SuppressWarnings("unchecked")
    055    public static <L, R> ImmutablePair<L, R> nullPair() {
    056        return NULL;
    057    }
    058
    059    /** Left object */
    060    public final L left;
    061    /** Right object */
    062    public final R right;
    063
    064    /**
    065     * <p>Obtains an immutable pair of two objects inferring the generic types.</p>
    066     *
    067     * <p>This factory allows the pair to be created using inference to
    068     * obtain the generic types.</p>
    069     *
    070     * @param <L> the left element type
    071     * @param <R> the right element type
    072     * @param left  the left element, may be null
    073     * @param right  the right element, may be null
    074     * @return a pair formed from the two parameters, not null
    075     */
    076    public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
    077        return new ImmutablePair<>(left, right);
    078    }
    079
    080    /**
    081     * Create a new pair instance.
    082     *
    083     * @param left  the left value, may be null
    084     * @param right  the right value, may be null
    085     */
    086    public ImmutablePair(final L left, final R right) {
    087        super();
    088        this.left = left;
    089        this.right = right;
    090    }
    091
    092    //-----------------------------------------------------------------------
    093    /**
    094     * {@inheritDoc}
    095     */
    096    @Override
    097    public L getLeft() {
    098        return left;
    099    }
    100
    101    /**
    102     * {@inheritDoc}
    103     */
    104    @Override
    105    public R getRight() {
    106        return right;
    107    }
    108
    109    /**
    110     * <p>Throws {@code UnsupportedOperationException}.</p>
    111     *
    112     * <p>This pair is immutable, so this operation is not supported.</p>
    113     *
    114     * @param value  the value to set
    115     * @return never
    116     * @throws UnsupportedOperationException as this operation is not supported
    117     */
    118    @Override
    119    public R setValue(final R value) {
    120        throw new UnsupportedOperationException();
    121    }
    122
    123}