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