MutablePair.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.  * A mutable pair consisting of two {@link Object} elements.
  22.  *
  23.  * <p>Not #ThreadSafe#</p>
  24.  *
  25.  * @param <L> the left element type.
  26.  * @param <R> the right element type.
  27.  * @since 3.0
  28.  */
  29. public class MutablePair<L, R> extends Pair<L, R> {

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

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

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

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

  68.     /**
  69.      * Creates a mutable pair from a map entry.
  70.      *
  71.      * <p>This factory allows the pair to be created using inference to
  72.      * obtain the generic types.</p>
  73.      *
  74.      * @param <L> the left element type.
  75.      * @param <R> the right element type.
  76.      * @param pair the existing map entry.
  77.      * @return a pair formed from the map entry.
  78.      */
  79.     public static <L, R> MutablePair<L, R> of(final Map.Entry<L, R> pair) {
  80.         final L left;
  81.         final R right;
  82.         if (pair != null) {
  83.             left = pair.getKey();
  84.             right = pair.getValue();
  85.         } else {
  86.             left = null;
  87.             right = null;
  88.         }
  89.         return new MutablePair<>(left, right);
  90.     }

  91.     /**
  92.      * Creates a mutable pair of two non-null objects inferring the generic types.
  93.      *
  94.      * <p>This factory allows the pair to be created using inference to
  95.      * obtain the generic types.</p>
  96.      *
  97.      * @param <L> the left element type.
  98.      * @param <R> the right element type.
  99.      * @param left  the left element, may not be null.
  100.      * @param right  the right element, may not be null.
  101.      * @return a pair formed from the two parameters, not null.
  102.      * @throws NullPointerException if any input is null.
  103.      * @since 3.13.0
  104.      */
  105.     public static <L, R> MutablePair<L, R> ofNonNull(final L left, final R right) {
  106.         return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right"));
  107.     }

  108.     /** Left object. */
  109.     public L left;

  110.     /** Right object. */
  111.     public R right;

  112.     /**
  113.      * Create a new pair instance of two nulls.
  114.      */
  115.     public MutablePair() {
  116.     }

  117.     /**
  118.      * Create a new pair instance.
  119.      *
  120.      * @param left  the left value, may be null.
  121.      * @param right  the right value, may be null.
  122.      */
  123.     public MutablePair(final L left, final R right) {
  124.         this.left = left;
  125.         this.right = right;
  126.     }

  127.     /**
  128.      * {@inheritDoc}
  129.      */
  130.     @Override
  131.     public L getLeft() {
  132.         return left;
  133.     }

  134.     /**
  135.      * {@inheritDoc}
  136.      */
  137.     @Override
  138.     public R getRight() {
  139.         return right;
  140.     }

  141.     /**
  142.      * Sets the left element of the pair.
  143.      *
  144.      * @param left  the new value of the left element, may be null.
  145.      */
  146.     public void setLeft(final L left) {
  147.         this.left = left;
  148.     }

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

  157.     /**
  158.      * Sets the {@code Map.Entry} value.
  159.      * This sets the right element of the pair.
  160.      *
  161.      * @param value  the right value to set, not null.
  162.      * @return the old value for the right element.
  163.      */
  164.     @Override
  165.     public R setValue(final R value) {
  166.         final R result = getRight();
  167.         setRight(value);
  168.         return result;
  169.     }

  170. }