001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.lang3.tuple; 018 019import java.util.Map; 020import java.util.Objects; 021 022/** 023 * A mutable pair consisting of two {@link Object} elements. 024 * 025 * <p>Not #ThreadSafe#</p> 026 * 027 * @param <L> the left element type. 028 * @param <R> the right element type. 029 * @since 3.0 030 */ 031public class MutablePair<L, R> extends Pair<L, R> { 032 033 /** 034 * An empty array. 035 * <p> 036 * Consider using {@link #emptyArray()} to avoid generics warnings. 037 * </p> 038 * 039 * @since 3.10 040 */ 041 public static final MutablePair<?, ?>[] EMPTY_ARRAY = {}; 042 043 /** Serialization version */ 044 private static final long serialVersionUID = 4954918890077093841L; 045 046 /** 047 * Returns the empty array singleton that can be assigned without compiler warning. 048 * 049 * @param <L> the left element type. 050 * @param <R> the right element type. 051 * @return the empty array singleton that can be assigned without compiler warning. 052 * @since 3.10 053 */ 054 @SuppressWarnings("unchecked") 055 public static <L, R> MutablePair<L, R>[] emptyArray() { 056 return (MutablePair<L, R>[]) EMPTY_ARRAY; 057 } 058 059 /** 060 * Creates a mutable pair of two objects inferring the generic types. 061 * 062 * @param <L> the left element type. 063 * @param <R> the right element type. 064 * @param left the left element, may be null. 065 * @param right the right element, may be null. 066 * @return a mutable pair formed from the two parameters, not null. 067 */ 068 public static <L, R> MutablePair<L, R> of(final L left, final R right) { 069 return new MutablePair<>(left, right); 070 } 071 072 /** 073 * Creates a mutable pair from a map entry. 074 * 075 * @param <L> the left element type. 076 * @param <R> the right element type. 077 * @param pair the existing map entry. 078 * @return a mutable pair formed from the map entry. 079 */ 080 public static <L, R> MutablePair<L, R> of(final Map.Entry<L, R> pair) { 081 final L left; 082 final R right; 083 if (pair != null) { 084 left = pair.getKey(); 085 right = pair.getValue(); 086 } else { 087 left = null; 088 right = null; 089 } 090 return new MutablePair<>(left, right); 091 } 092 093 /** 094 * Creates a mutable pair of two non-null objects inferring the generic types. 095 * 096 * @param <L> the left element type. 097 * @param <R> the right element type. 098 * @param left the left element, may not be null. 099 * @param right the right element, may not be null. 100 * @return a mutable pair formed from the two parameters, not null. 101 * @throws NullPointerException if any input is null. 102 * @since 3.13.0 103 */ 104 public static <L, R> MutablePair<L, R> ofNonNull(final L left, final R right) { 105 return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right")); 106 } 107 108 /** 109 * Creates a mutable pair from a map entry. 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 mutable pair formed from the map entry 115 * @throws NullPointerException if the pair is null. 116 * @since 3.20 117 */ 118 public static <L, R> MutablePair<L, R> ofNonNull(final Map.Entry<L, R> pair) { 119 return of(Objects.requireNonNull(pair, "pair")); 120 } 121 122 /** Left object. */ 123 public L left; 124 125 /** Right object. */ 126 public R right; 127 128 /** 129 * Create a new pair instance of two nulls. 130 */ 131 public MutablePair() { 132 } 133 134 /** 135 * Create a new pair instance. 136 * 137 * @param left the left value, may be null. 138 * @param right the right value, may be null. 139 */ 140 public MutablePair(final L left, final R right) { 141 this.left = left; 142 this.right = right; 143 } 144 145 /** 146 * {@inheritDoc} 147 */ 148 @Override 149 public L getLeft() { 150 return left; 151 } 152 153 /** 154 * {@inheritDoc} 155 */ 156 @Override 157 public R getRight() { 158 return right; 159 } 160 161 /** 162 * Sets the left element of the pair. 163 * 164 * @param left the new value of the left element, may be null. 165 */ 166 public void setLeft(final L left) { 167 this.left = left; 168 } 169 170 /** 171 * Sets the right element of the pair. 172 * 173 * @param right the new value of the right element, may be null. 174 */ 175 public void setRight(final R right) { 176 this.right = right; 177 } 178 179 /** 180 * Sets the {@code Map.Entry} value. 181 * This sets the right element of the pair. 182 * 183 * @param value the right value to set, not null. 184 * @return the old value for the right element. 185 */ 186 @Override 187 public R setValue(final R value) { 188 final R result = getRight(); 189 setRight(value); 190 return result; 191 } 192 193}