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    *      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  /**
20   * <p>An immutable triple consisting of three {@code Object} elements.</p>
21   * 
22   * <p>Although the implementation is immutable, there is no restriction on the objects
23   * that may be stored. If mutable objects are stored in the triple, then the triple
24   * itself effectively becomes mutable. The class is also not {@code final}, so a subclass
25   * could add undesirable behaviour.</p>
26   * 
27   * <p>#ThreadSafe# if all three objects are thread-safe</p>
28   *
29   * @param <L> the left element type
30   * @param <M> the middle element type
31   * @param <R> the right element type
32   *
33   * @version $Id: ImmutableTriple.java 1552635 2013-12-20 13:02:03Z britter $
34   * @since 3.2
35   */
36  public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
37  
38      /** Serialization version */
39      private static final long serialVersionUID = 1L;
40  
41      /** Left object */
42      public final L left;
43      /** Middle object */
44      public final M middle;
45      /** Right object */
46      public final R right;
47  
48      /**
49       * <p>Obtains an immutable triple of from three objects inferring the generic types.</p>
50       * 
51       * <p>This factory allows the triple to be created using inference to
52       * obtain the generic types.</p>
53       * 
54       * @param <L> the left element type
55       * @param <M> the middle element type
56       * @param <R> the right element type
57       * @param left  the left element, may be null
58       * @param middle  the middle element, may be null
59       * @param right  the right element, may be null
60       * @return a triple formed from the three parameters, not null
61       */
62      public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
63          return new ImmutableTriple<L, M, R>(left, middle, right);
64      }
65  
66      /**
67       * Create a new triple instance.
68       *
69       * @param left  the left value, may be null
70       * @param middle the middle value, may be null
71       * @param right  the right value, may be null
72       */
73      public ImmutableTriple(final L left, final M middle, final R right) {
74          super();
75          this.left = left;
76          this.middle = middle;
77          this.right = right;
78      }
79  
80      //-----------------------------------------------------------------------
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public L getLeft() {
86          return left;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public M getMiddle() {
94          return middle;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public R getRight() {
102         return right;
103     }
104 }
105