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.  *      http://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.  *
  28.  * @since 3.2
  29.  */
  30. public class MutableTriple<L, M, R> extends Triple<L, M, R> {

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

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

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

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

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

  92.     /** Left object */
  93.     public L left;
  94.     /** Middle object */
  95.     public M middle;

  96.     /** Right object */
  97.     public R right;

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

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

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

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

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

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

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

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