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 not {@code final}, so a subclass
25   * could 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   * @version $Id: ImmutablePair.java 1436768 2013-01-22 07:07:42Z ggregory $
34   */
35  public final class ImmutablePair<L, R> extends Pair<L, R> {
36  
37      /** Serialization version */
38      private static final long serialVersionUID = 4954918890077093841L;
39  
40      /** Left object */
41      public final L left;
42      /** Right object */
43      public final R right;
44  
45      /**
46       * <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
47       * 
48       * <p>This factory allows the pair to be created using inference to
49       * obtain the generic types.</p>
50       * 
51       * @param <L> the left element type
52       * @param <R> the right element type
53       * @param left  the left element, may be null
54       * @param right  the right element, may be null
55       * @return a pair formed from the two parameters, not null
56       */
57      public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
58          return new ImmutablePair<L, R>(left, right);
59      }
60  
61      /**
62       * Create a new pair instance.
63       *
64       * @param left  the left value, may be null
65       * @param right  the right value, may be null
66       */
67      public ImmutablePair(final L left, final R right) {
68          super();
69          this.left = left;
70          this.right = right;
71      }
72  
73      //-----------------------------------------------------------------------
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public L getLeft() {
79          return left;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public R getRight() {
87          return right;
88      }
89  
90      /**
91       * <p>Throws {@code UnsupportedOperationException}.</p>
92       * 
93       * <p>This pair is immutable, so this operation is not supported.</p>
94       *
95       * @param value  the value to set
96       * @return never
97       * @throws UnsupportedOperationException as this operation is not supported
98       */
99      @Override
100     public R setValue(final R value) {
101         throw new UnsupportedOperationException();
102     }
103 
104 }