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 * http://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 * 030 * @since 3.0 031 */ 032public class MutablePair<L, R> extends Pair<L, R> { 033 034 /** 035 * An empty array. 036 * <p> 037 * Consider using {@link #emptyArray()} to avoid generics warnings. 038 * </p> 039 * 040 * @since 3.10. 041 */ 042 public static final MutablePair<?, ?>[] EMPTY_ARRAY = {}; 043 044 /** Serialization version */ 045 private static final long serialVersionUID = 4954918890077093841L; 046 047 /** 048 * Returns the empty array singleton that can be assigned without compiler warning. 049 * 050 * @param <L> the left element type 051 * @param <R> the right element type 052 * @return the empty array singleton that can be assigned without compiler warning. 053 * 054 * @since 3.10. 055 */ 056 @SuppressWarnings("unchecked") 057 public static <L, R> MutablePair<L, R>[] emptyArray() { 058 return (MutablePair<L, R>[]) EMPTY_ARRAY; 059 } 060 061 /** 062 * Creates a mutable pair of two objects inferring the generic types. 063 * 064 * <p>This factory allows the pair to be created using inference to 065 * obtain the generic types.</p> 066 * 067 * @param <L> the left element type 068 * @param <R> the right element type 069 * @param left the left element, may be null 070 * @param right the right element, may be null 071 * @return a pair formed from the two parameters, not null 072 */ 073 public static <L, R> MutablePair<L, R> of(final L left, final R right) { 074 return new MutablePair<>(left, right); 075 } 076 077 /** 078 * Creates a mutable pair from a map entry. 079 * 080 * <p>This factory allows the pair to be created using inference to 081 * obtain the generic types.</p> 082 * 083 * @param <L> the left element type 084 * @param <R> the right element type 085 * @param pair the existing map entry. 086 * @return a pair formed from the map entry 087 */ 088 public static <L, R> MutablePair<L, R> of(final Map.Entry<L, R> pair) { 089 final L left; 090 final R right; 091 if (pair != null) { 092 left = pair.getKey(); 093 right = pair.getValue(); 094 } else { 095 left = null; 096 right = null; 097 } 098 return new MutablePair<>(left, right); 099 } 100 101 /** 102 * Creates a mutable pair of two non-null objects inferring the generic types. 103 * 104 * <p>This factory allows the pair to be created using inference to 105 * obtain the generic types.</p> 106 * 107 * @param <L> the left element type 108 * @param <R> the right element type 109 * @param left the left element, may not be null 110 * @param right the right element, may not be null 111 * @return a pair formed from the two parameters, not null 112 * @throws NullPointerException if any input is null 113 * @since 3.13.0 114 */ 115 public static <L, R> MutablePair<L, R> ofNonNull(final L left, final R right) { 116 return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(right, "right")); 117 } 118 119 /** Left object */ 120 public L left; 121 122 /** Right object */ 123 public R right; 124 125 /** 126 * Create a new pair instance of two nulls. 127 */ 128 public MutablePair() { 129 } 130 131 /** 132 * Create a new pair instance. 133 * 134 * @param left the left value, may be null 135 * @param right the right value, may be null 136 */ 137 public MutablePair(final L left, final R right) { 138 this.left = left; 139 this.right = right; 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override 146 public L getLeft() { 147 return left; 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override 154 public R getRight() { 155 return right; 156 } 157 158 /** 159 * Sets the left element of the pair. 160 * 161 * @param left the new value of the left element, may be null 162 */ 163 public void setLeft(final L left) { 164 this.left = left; 165 } 166 167 /** 168 * Sets the right element of the pair. 169 * 170 * @param right the new value of the right element, may be null 171 */ 172 public void setRight(final R right) { 173 this.right = right; 174 } 175 176 /** 177 * Sets the {@code Map.Entry} value. 178 * This sets the right element of the pair. 179 * 180 * @param value the right value to set, not null 181 * @return the old value for the right element 182 */ 183 @Override 184 public R setValue(final R value) { 185 final R result = getRight(); 186 setRight(value); 187 return result; 188 } 189 190}