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 pair consisting of two {@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 pair, then the pair
24   * itself effectively becomes mutable. The class is also {@code final}, so a subclass
25   * can not add undesirable behaviour.</p>
26   * 
27   * <p>#ThreadSafe# if both paired objects are thread-safe</p>
28   *
29   * @param <L> the left element type
30   * @param <R> the right element type
31   *
32   * @since Lang 3.0
33   */
34  public final class ImmutablePair<L, R> extends Pair<L, R> {
35  
36      /** Serialization version */
37      private static final long serialVersionUID = 4954918890077093841L;
38  
39      /** Left object */
40      public final L left;
41      /** Right object */
42      public final R right;
43  
44      /**
45       * <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
46       * 
47       * <p>This factory allows the pair to be created using inference to
48       * obtain the generic types.</p>
49       * 
50       * @param <L> the left element type
51       * @param <R> the right element type
52       * @param left  the left element, may be null
53       * @param right  the right element, may be null
54       * @return a pair formed from the two parameters, not null
55       */
56      public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
57          return new ImmutablePair<L, R>(left, right);
58      }
59  
60      /**
61       * Create a new pair instance.
62       *
63       * @param left  the left value, may be null
64       * @param right  the right value, may be null
65       */
66      public ImmutablePair(final L left, final R right) {
67          super();
68          this.left = left;
69          this.right = right;
70      }
71  
72      //-----------------------------------------------------------------------
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public L getLeft() {
78          return left;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public R getRight() {
86          return right;
87      }
88  
89      /**
90       * <p>Throws {@code UnsupportedOperationException}.</p>
91       * 
92       * <p>This pair is immutable, so this operation is not supported.</p>
93       *
94       * @param value  the value to set
95       * @return never
96       * @throws UnsupportedOperationException as this operation is not supported
97       */
98      @Override
99      public R setValue(final R value) {
100         throw new UnsupportedOperationException();
101     }
102 
103 }