001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.lang3.tuple; 018 019 /** 020 * <p>An immutable pair consisting of two {@code Object} elements.</p> 021 * 022 * <p>Although the implementation is immutable, there is no restriction on the objects 023 * that may be stored. If mutable objects are stored in the pair, then the pair 024 * itself effectively becomes mutable. The class is also not {@code final}, so a subclass 025 * could add undesirable behaviour.</p> 026 * 027 * <p>#ThreadSafe# if the objects are threadsafe</p> 028 * 029 * @param <L> the left element type 030 * @param <R> the right element type 031 * 032 * @since Lang 3.0 033 * @version $Id: ImmutablePair.java 1127544 2011-05-25 14:35:42Z scolebourne $ 034 */ 035 public final class ImmutablePair<L, R> extends Pair<L, R> { 036 037 /** Serialization version */ 038 private static final long serialVersionUID = 4954918890077093841L; 039 040 /** Left object */ 041 public final L left; 042 /** Right object */ 043 public final R right; 044 045 /** 046 * <p>Obtains an immutable pair of from two objects inferring the generic types.</p> 047 * 048 * <p>This factory allows the pair to be created using inference to 049 * obtain the generic types.</p> 050 * 051 * @param <L> the left element type 052 * @param <R> the right element type 053 * @param left the left element, may be null 054 * @param right the right element, may be null 055 * @return a pair formed from the two parameters, not null 056 */ 057 public static <L, R> ImmutablePair<L, R> of(L left, R right) { 058 return new ImmutablePair<L, R>(left, right); 059 } 060 061 /** 062 * Create a new pair instance. 063 * 064 * @param left the left value, may be null 065 * @param right the right value, may be null 066 */ 067 public ImmutablePair(L left, R right) { 068 super(); 069 this.left = left; 070 this.right = right; 071 } 072 073 //----------------------------------------------------------------------- 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 public L getLeft() { 079 return left; 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 public R getRight() { 087 return right; 088 } 089 090 /** 091 * <p>Throws {@code UnsupportedOperationException}.</p> 092 * 093 * <p>This pair is immutable, so this operation is not supported.</p> 094 * 095 * @param value the value to set 096 * @return never 097 * @throws UnsupportedOperationException as this operation is not supported 098 */ 099 public R setValue(R value) { 100 throw new UnsupportedOperationException(); 101 } 102 103 }