| 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 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | /** |
| 22 | * A mutable triple consisting of three {@link Object} elements. |
| 23 | * |
| 24 | * <p>Not #ThreadSafe#</p> |
| 25 | * |
| 26 | * @param <L> the left element type |
| 27 | * @param <M> the middle element type |
| 28 | * @param <R> the right element type |
| 29 | * |
| 30 | * @since 3.2 |
| 31 | */ |
| 32 | public class MutableTriple<L, M, R> extends Triple<L, M, R> { |
| 33 | |
| 34 | /** |
| 35 | * The empty array singleton. |
| 36 | * <p> |
| 37 | * Consider using {@link #emptyArray()} to avoid generics warnings. |
| 38 | * </p> |
| 39 | * |
| 40 | * @since 3.10. |
| 41 | */ |
| 42 | public static final MutableTriple<?, ?, ?>[] EMPTY_ARRAY = {}; |
| 43 | |
| 44 | /** Serialization version */ |
| 45 | private static final long serialVersionUID = 1L; |
| 46 | |
| 47 | /** |
| 48 | * Returns the empty array singleton that can be assigned without compiler warning. |
| 49 | * |
| 50 | * @param <L> the left element type |
| 51 | * @param <M> the middle element type |
| 52 | * @param <R> the right element type |
| 53 | * @return the empty array singleton that can be assigned without compiler warning. |
| 54 | * |
| 55 | * @since 3.10. |
| 56 | */ |
| 57 | @SuppressWarnings("unchecked") |
| 58 | public static <L, M, R> MutableTriple<L, M, R>[] emptyArray() { |
| 59 | return (MutableTriple<L, M, R>[]) EMPTY_ARRAY; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Obtains a mutable triple of three objects inferring the generic types. |
| 64 | * |
| 65 | * <p>This factory allows the triple to be created using inference to |
| 66 | * obtain the generic types.</p> |
| 67 | * |
| 68 | * @param <L> the left element type |
| 69 | * @param <M> the middle element type |
| 70 | * @param <R> the right element type |
| 71 | * @param left the left element, may be null |
| 72 | * @param middle the middle element, may be null |
| 73 | * @param right the right element, may be null |
| 74 | * @return a triple formed from the three parameters, not null |
| 75 | */ |
| 76 | public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) { |
| 77 | return new MutableTriple<>(left, middle, right); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Obtains a mutable triple of three non-null objects inferring the generic types. |
| 82 | * |
| 83 | * <p>This factory allows the triple to be created using inference to |
| 84 | * obtain the generic types.</p> |
| 85 | * |
| 86 | * @param <L> the left element type |
| 87 | * @param <M> the middle element type |
| 88 | * @param <R> the right element type |
| 89 | * @param left the left element, may not be null |
| 90 | * @param middle the middle element, may not be null |
| 91 | * @param right the right element, may not be null |
| 92 | * @return a triple formed from the three parameters, not null |
| 93 | * @throws NullPointerException if any input is null |
| 94 | * @since 3.13.0 |
| 95 | */ |
| 96 | public static <L, M, R> MutableTriple<L, M, R> ofNonNull(final L left, final M middle, final R right) { |
| 97 | return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right")); |
| 98 | } |
| 99 | |
| 100 | /** Left object */ |
| 101 | public L left; |
| 102 | /** Middle object */ |
| 103 | public M middle; |
| 104 | |
| 105 | /** Right object */ |
| 106 | public R right; |
| 107 | |
| 108 | /** |
| 109 | * Create a new triple instance of three nulls. |
| 110 | */ |
| 111 | public MutableTriple() { |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Create a new triple instance. |
| 116 | * |
| 117 | * @param left the left value, may be null |
| 118 | * @param middle the middle value, may be null |
| 119 | * @param right the right value, may be null |
| 120 | */ |
| 121 | public MutableTriple(final L left, final M middle, final R right) { |
| 122 | this.left = left; |
| 123 | this.middle = middle; |
| 124 | this.right = right; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * {@inheritDoc} |
| 129 | */ |
| 130 | @Override |
| 131 | public L getLeft() { |
| 132 | return left; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * {@inheritDoc} |
| 137 | */ |
| 138 | @Override |
| 139 | public M getMiddle() { |
| 140 | return middle; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | */ |
| 146 | @Override |
| 147 | public R getRight() { |
| 148 | return right; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets the left element of the triple. |
| 153 | * |
| 154 | * @param left the new value of the left element, may be null |
| 155 | */ |
| 156 | public void setLeft(final L left) { |
| 157 | this.left = left; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Sets the middle element of the triple. |
| 162 | * |
| 163 | * @param middle the new value of the middle element, may be null |
| 164 | */ |
| 165 | public void setMiddle(final M middle) { |
| 166 | this.middle = middle; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the right element of the triple. |
| 171 | * |
| 172 | * @param right the new value of the right element, may be null |
| 173 | */ |
| 174 | public void setRight(final R right) { |
| 175 | this.right = right; |
| 176 | } |
| 177 | } |
| 178 |