View Javadoc
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  
95      /** Middle object. */
96      public M middle;
97  
98      /** Right object. */
99      public R right;
100 
101     /**
102      * Create a new triple instance of three nulls.
103      */
104     public MutableTriple() {
105     }
106 
107     /**
108      * Create a new triple instance.
109      *
110      * @param left  the left value, may be null.
111      * @param middle  the middle value, may be null.
112      * @param right  the right value, may be null.
113      */
114     public MutableTriple(final L left, final M middle, final R right) {
115         this.left = left;
116         this.middle = middle;
117         this.right = right;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public L getLeft() {
125         return left;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public M getMiddle() {
133         return middle;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public R getRight() {
141         return right;
142     }
143 
144     /**
145      * Sets the left element of the triple.
146      *
147      * @param left  the new value of the left element, may be null.
148      */
149     public void setLeft(final L left) {
150         this.left = left;
151     }
152 
153     /**
154      * Sets the middle element of the triple.
155      *
156      * @param middle  the new value of the middle element, may be null.
157      */
158     public void setMiddle(final M middle) {
159         this.middle = middle;
160     }
161 
162     /**
163      * Sets the right element of the triple.
164      *
165      * @param right  the new value of the right element, may be null.
166      */
167     public void setRight(final R right) {
168         this.right = right;
169     }
170 }
171