ImmutableTriple.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.Objects;

  19. /**
  20.  * An immutable triple consisting of three {@link Object} elements.
  21.  *
  22.  * <p>Although the implementation is immutable, there is no restriction on the objects
  23.  * that may be stored. If mutable objects are stored in the triple, then the triple
  24.  * itself effectively becomes mutable.</p>
  25.  *
  26.  * <p>#ThreadSafe# if all three objects are thread-safe.</p>
  27.  *
  28.  * @param <L> the left element type.
  29.  * @param <M> the middle element type.
  30.  * @param <R> the right element type.
  31.  * @since 3.2
  32.  */
  33. public class ImmutableTriple<L, M, R> extends Triple<L, M, 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 ImmutableTriple<?, ?, ?>[] EMPTY_ARRAY = {};

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

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

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

  64.     /**
  65.      * Gets the immutable triple of nulls singleton.
  66.      *
  67.      * @param <L> the left element of this triple. Value is {@code null}.
  68.      * @param <M> the middle element of this triple. Value is {@code null}.
  69.      * @param <R> the right element of this triple. Value is {@code null}.
  70.      * @return an immutable triple of nulls.
  71.      * @since 3.6
  72.      */
  73.     @SuppressWarnings("unchecked")
  74.     public static <L, M, R> ImmutableTriple<L, M, R> nullTriple() {
  75.         return NULL;
  76.     }

  77.     /**
  78.      * Creates an immutable triple of three objects inferring the generic types.
  79.      *
  80.      * <p>This factory allows the triple to be created using inference to
  81.      * obtain the generic types.</p>
  82.      *
  83.      * @param <L> the left element type.
  84.      * @param <M> the middle element type.
  85.      * @param <R> the right element type.
  86.      * @param left  the left element, may be null.
  87.      * @param middle  the middle element, may be null.
  88.      * @param right  the right element, may be null.
  89.      * @return a triple formed from the three parameters, not null.
  90.      */
  91.     public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
  92.         return left != null | middle != null || right != null ? new ImmutableTriple<>(left, middle, right) : nullTriple();
  93.     }

  94.     /**
  95.      * Creates an immutable triple of three non-null objects inferring the generic types.
  96.      *
  97.      * <p>This factory allows the triple to be created using inference to
  98.      * obtain the generic types.</p>
  99.      *
  100.      * @param <L> the left element type.
  101.      * @param <M> the middle element type.
  102.      * @param <R> the right element type.
  103.      * @param left  the left element, may not be null.
  104.      * @param middle  the middle element, may not be null.
  105.      * @param right  the right element, may not be null.
  106.      * @return a triple formed from the three parameters, not null.
  107.      * @throws NullPointerException if any input is null.
  108.      * @since 3.13.0
  109.      */
  110.     public static <L, M, R> ImmutableTriple<L, M, R> ofNonNull(final L left, final M middle, final R right) {
  111.         return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right"));
  112.     }

  113.     /** Left object. */

  114.     public final L left;
  115.     /** Middle object. */
  116.     public final M middle;

  117.     /** Right object. */
  118.     public final R right;

  119.     /**
  120.      * Constructs a new triple instance.
  121.      *
  122.      * @param left  the left value, may be null.
  123.      * @param middle the middle value, may be null.
  124.      * @param right  the right value, may be null.
  125.      */
  126.     public ImmutableTriple(final L left, final M middle, final R right) {
  127.         this.left = left;
  128.         this.middle = middle;
  129.         this.right = right;
  130.     }

  131.     /**
  132.      * {@inheritDoc}
  133.      */
  134.     @Override
  135.     public L getLeft() {
  136.         return left;
  137.     }

  138.     /**
  139.      * {@inheritDoc}
  140.      */
  141.     @Override
  142.     public M getMiddle() {
  143.         return middle;
  144.     }

  145.     /**
  146.      * {@inheritDoc}
  147.      */
  148.     @Override
  149.     public R getRight() {
  150.         return right;
  151.     }
  152. }