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       * <p>This factory allows the triple to be created using inference to
64       * obtain the generic types.</p>
65       *
66       * @param <L> the left element type.
67       * @param <M> the middle element type.
68       * @param <R> the right element type.
69       * @param left  the left element, may be null.
70       * @param middle  the middle element, may be null.
71       * @param right  the right element, may be null.
72       * @return a triple formed from the three parameters, not null.
73       */
74      public static <L, M, R> MutableTriple<L, M, R> of(final L left, final M middle, final R right) {
75          return new MutableTriple<>(left, middle, right);
76      }
77  
78      /**
79       * Obtains a mutable triple of three non-null objects inferring the generic types.
80       *
81       * <p>This factory allows the triple to be created using inference to
82       * obtain the generic types.</p>
83       *
84       * @param <L> the left element type.
85       * @param <M> the middle element type.
86       * @param <R> the right element type.
87       * @param left  the left element, may not be null.
88       * @param middle  the middle element, may not be null.
89       * @param right  the right element, may not be null.
90       * @return a triple formed from the three parameters, not null.
91       * @throws NullPointerException if any input is null.
92       * @since 3.13.0
93       */
94      public static <L, M, R> MutableTriple<L, M, R> ofNonNull(final L left, final M middle, final R right) {
95          return of(Objects.requireNonNull(left, "left"), Objects.requireNonNull(middle, "middle"), Objects.requireNonNull(right, "right"));
96      }
97  
98      /** Left object. */
99      public L left;
100     /** Middle object. */
101     public M middle;
102 
103     /** Right object. */
104     public R right;
105 
106     /**
107      * Create a new triple instance of three nulls.
108      */
109     public MutableTriple() {
110     }
111 
112     /**
113      * Create a new triple instance.
114      *
115      * @param left  the left value, may be null.
116      * @param middle  the middle value, may be null.
117      * @param right  the right value, may be null.
118      */
119     public MutableTriple(final L left, final M middle, final R right) {
120         this.left = left;
121         this.middle = middle;
122         this.right = right;
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public L getLeft() {
130         return left;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public M getMiddle() {
138         return middle;
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public R getRight() {
146         return right;
147     }
148 
149     /**
150      * Sets the left element of the triple.
151      *
152      * @param left  the new value of the left element, may be null.
153      */
154     public void setLeft(final L left) {
155         this.left = left;
156     }
157 
158     /**
159      * Sets the middle element of the triple.
160      *
161      * @param middle  the new value of the middle element, may be null.
162      */
163     public void setMiddle(final M middle) {
164         this.middle = middle;
165     }
166 
167     /**
168      * Sets the right element of the triple.
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