MutableTriple.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.  * A mutable triple consisting of three {@link Object} elements.
  21.  *
  22.  * <p>Not #ThreadSafe#</p>
  23.  *
  24.  * @param <L> the left element type.
  25.  * @param <M> the middle element type.
  26.  * @param <R> the right element type.
  27.  * @since 3.2
  28.  */
  29. public class MutableTriple<L, M, R> extends Triple<L, M, R> {

  30.     /**
  31.      * The empty array singleton.
  32.      * <p>
  33.      * Consider using {@link #emptyArray()} to avoid generics warnings.
  34.      * </p>
  35.      *
  36.      * @since 3.10
  37.      */
  38.     public static final MutableTriple<?, ?, ?>[] EMPTY_ARRAY = {};

  39.     /** Serialization version */
  40.     private static final long serialVersionUID = 1L;

  41.     /**
  42.      * Returns the empty array singleton that can be assigned without compiler warning.
  43.      *
  44.      * @param <L> the left element type.
  45.      * @param <M> the middle element type.
  46.      * @param <R> the right element type.
  47.      * @return the empty array singleton that can be assigned without compiler warning.
  48.      * @since 3.10
  49.      */
  50.     @SuppressWarnings("unchecked")
  51.     public static <L, M, R> MutableTriple<L, M, R>[] emptyArray() {
  52.         return (MutableTriple<L, M, R>[]) EMPTY_ARRAY;
  53.     }

  54.     /**
  55.      * Obtains a mutable triple of three objects inferring the generic types.
  56.      *
  57.      * <p>This factory allows the triple to be created using inference to
  58.      * obtain the generic types.</p>
  59.      *
  60.      * @param <L> the left element type.
  61.      * @param <M> the middle element type.
  62.      * @param <R> the right element type.
  63.      * @param left  the left element, may be null.
  64.      * @param middle  the middle element, may be null.
  65.      * @param right  the right element, may be null.
  66.      * @return a triple formed from the three parameters, not null.
  67.      */
  68.     public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) {
  69.         return new MutableTriple<>(left, middle, right);
  70.     }

  71.     /**
  72.      * Obtains a mutable triple of three non-null objects inferring the generic types.
  73.      *
  74.      * <p>This factory allows the triple to be created using inference to
  75.      * obtain the generic types.</p>
  76.      *
  77.      * @param <L> the left element type.
  78.      * @param <M> the middle element type.
  79.      * @param <R> the right element type.
  80.      * @param left  the left element, may not be null.
  81.      * @param middle  the middle element, may not be null.
  82.      * @param right  the right element, may not be null.
  83.      * @return a triple formed from the three parameters, not null.
  84.      * @throws NullPointerException if any input is null.
  85.      * @since 3.13.0
  86.      */
  87.     public static <L, M, R> MutableTriple<L, M, R> ofNonNull(final L left, final M middle, final R right) {
  88.         return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right"));
  89.     }

  90.     /** Left object. */
  91.     public L left;
  92.     /** Middle object. */
  93.     public M middle;

  94.     /** Right object. */
  95.     public R right;

  96.     /**
  97.      * Create a new triple instance of three nulls.
  98.      */
  99.     public MutableTriple() {
  100.     }

  101.     /**
  102.      * Create a new triple instance.
  103.      *
  104.      * @param left  the left value, may be null.
  105.      * @param middle  the middle value, may be null.
  106.      * @param right  the right value, may be null.
  107.      */
  108.     public MutableTriple(final L left, final M middle, final R right) {
  109.         this.left = left;
  110.         this.middle = middle;
  111.         this.right = right;
  112.     }

  113.     /**
  114.      * {@inheritDoc}
  115.      */
  116.     @Override
  117.     public L getLeft() {
  118.         return left;
  119.     }

  120.     /**
  121.      * {@inheritDoc}
  122.      */
  123.     @Override
  124.     public M getMiddle() {
  125.         return middle;
  126.     }

  127.     /**
  128.      * {@inheritDoc}
  129.      */
  130.     @Override
  131.     public R getRight() {
  132.         return right;
  133.     }

  134.     /**
  135.      * Sets the left element of the triple.
  136.      *
  137.      * @param left  the new value of the left element, may be null.
  138.      */
  139.     public void setLeft(final L left) {
  140.         this.left = left;
  141.     }

  142.     /**
  143.      * Sets the middle element of the triple.
  144.      *
  145.      * @param middle  the new value of the middle element, may be null.
  146.      */
  147.     public void setMiddle(final M middle) {
  148.         this.middle = middle;
  149.     }

  150.     /**
  151.      * Sets the right element of the triple.
  152.      *
  153.      * @param right  the new value of the right element, may be null.
  154.      */
  155.     public void setRight(final R right) {
  156.         this.right = right;
  157.     }
  158. }