ImmutablePair.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.tuple;

  18. import java.util.Map;
  19. import java.util.Objects;

  20. /**
  21.  * An immutable pair consisting of two {@link Object} elements.
  22.  *
  23.  * <p>Although the implementation is immutable, there is no restriction on the objects
  24.  * that may be stored. If mutable objects are stored in the pair, then the pair
  25.  * itself effectively becomes mutable.</p>
  26.  *
  27.  * <p>#ThreadSafe# if both paired objects are thread-safe</p>
  28.  *
  29.  * @param <L> the left element type
  30.  * @param <R> the right element type
  31.  * @since 3.0
  32.  */
  33. public class ImmutablePair<L, R> extends Pair<L, R> {

  34.     /**
  35.      * An empty array.
  36.      * <p>
  37.      * Consider using {@link #emptyArray()} to avoid generics warnings.
  38.      * </p>
  39.      *
  40.      * @since 3.10
  41.      */
  42.     public static final ImmutablePair<?, ?>[] EMPTY_ARRAY = {};

  43.     /**
  44.      * An immutable pair of nulls.
  45.      */
  46.     // This is not defined with generics to avoid warnings in call sites.
  47.     @SuppressWarnings("rawtypes")
  48.     private static final ImmutablePair NULL = new ImmutablePair<>(null, null);

  49.     /** Serialization version */
  50.     private static final long serialVersionUID = 4954918890077093841L;

  51.     /**
  52.      * Returns the empty array singleton that can be assigned without compiler warning.
  53.      *
  54.      * @param <L> the left element type
  55.      * @param <R> the right element type
  56.      * @return the empty array singleton that can be assigned without compiler warning.
  57.      * @since 3.10
  58.      */
  59.     @SuppressWarnings("unchecked")
  60.     public static <L, R> ImmutablePair<L, R>[] emptyArray() {
  61.         return (ImmutablePair<L, R>[]) EMPTY_ARRAY;
  62.     }

  63.     /**
  64.      * Creates an immutable pair of two objects inferring the generic types.
  65.      *
  66.      * <p>This factory allows the pair to be created using inference to
  67.      * obtain the generic types.</p>
  68.      *
  69.      * @param <L> the left element type
  70.      * @param <R> the right element type
  71.      * @param left  the left element, may be null
  72.      * @return a pair formed from the two parameters, not null
  73.      * @since 3.11
  74.      */
  75.     public static <L, R> Pair<L, R> left(final L left) {
  76.         return of(left, null);
  77.     }

  78.     /**
  79.      * Returns an immutable pair of nulls.
  80.      *
  81.      * @param <L> the left element of this pair. Value is {@code null}.
  82.      * @param <R> the right element of this pair. Value is {@code null}.
  83.      * @return an immutable pair of nulls.
  84.      * @since 3.6
  85.      */
  86.     @SuppressWarnings("unchecked")
  87.     public static <L, R> ImmutablePair<L, R> nullPair() {
  88.         return NULL;
  89.     }

  90.     /**
  91.      * Creates an immutable pair of two objects inferring the generic types.
  92.      *
  93.      * <p>This factory allows the pair to be created using inference to
  94.      * obtain the generic types.</p>
  95.      *
  96.      * @param <L> the left element type
  97.      * @param <R> the right element type
  98.      * @param left  the left element, may be null
  99.      * @param right  the right element, may be null
  100.      * @return a pair formed from the two parameters, not null
  101.      */
  102.     public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
  103.         return left != null || right != null ? new ImmutablePair<>(left, right) : nullPair();
  104.     }

  105.     /**
  106.      * Creates an immutable pair from a map entry.
  107.      *
  108.      * <p>This factory allows the pair to be created using inference to
  109.      * obtain the generic types.</p>
  110.      *
  111.      * @param <L> the left element type
  112.      * @param <R> the right element type
  113.      * @param pair the existing map entry.
  114.      * @return a pair formed from the map entry
  115.      * @since 3.10
  116.      */
  117.     public static <L, R> ImmutablePair<L, R> of(final Map.Entry<L, R> pair) {
  118.         return pair != null ? new ImmutablePair<>(pair.getKey(), pair.getValue()) : nullPair();
  119.     }

  120.     /**
  121.      * Creates an immutable pair of two non-null objects inferring the generic types.
  122.      *
  123.      * <p>This factory allows the pair to be created using inference to
  124.      * obtain the generic types.</p>
  125.      *
  126.      * @param <L> the left element type
  127.      * @param <R> the right element type
  128.      * @param left  the left element, may not be null
  129.      * @param right  the right element, may not  be null
  130.      * @return a pair formed from the two parameters, not null
  131.      * @throws NullPointerException if any input is null
  132.      * @since 3.13.0
  133.      */
  134.     public static <L, R> ImmutablePair<L, R> ofNonNull(final L left, final R right) {
  135.         return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
  136.     }

  137.     /**
  138.      * Creates an immutable pair of two objects inferring the generic types.
  139.      *
  140.      * <p>This factory allows the pair to be created using inference to
  141.      * obtain the generic types.</p>
  142.      *
  143.      * @param <L> the left element type
  144.      * @param <R> the right element type
  145.      * @param right  the right element, may be null
  146.      * @return a pair formed from the two parameters, not null
  147.      * @since 3.11
  148.      */
  149.     public static <L, R> Pair<L, R> right(final R right) {
  150.         return of(null, right);
  151.     }

  152.     /** Left object */
  153.     public final L left;

  154.     /** Right object */
  155.     public final R right;

  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.      * {@inheritDoc}
  168.      */
  169.     @Override
  170.     public L getLeft() {
  171.         return left;
  172.     }

  173.     /**
  174.      * {@inheritDoc}
  175.      */
  176.     @Override
  177.     public R getRight() {
  178.         return right;
  179.     }

  180.     /**
  181.      * Throws {@link UnsupportedOperationException}.
  182.      *
  183.      * <p>This pair is immutable, so this operation is not supported.</p>
  184.      *
  185.      * @param value  the value to set
  186.      * @return never
  187.      * @throws UnsupportedOperationException as this operation is not supported
  188.      */
  189.     @Override
  190.     public R setValue(final R value) {
  191.         throw new UnsupportedOperationException();
  192.     }

  193. }