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