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 */ 017package 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 {@code final}, so a subclass 025 * can not add undesirable behaviour.</p> 026 * 027 * <p>#ThreadSafe# if both paired objects are thread-safe</p> 028 * 029 * @param <L> the left element type 030 * @param <R> the right element type 031 * 032 * @since Lang 3.0 033 */ 034public final class ImmutablePair<L, R> extends Pair<L, R> { 035 036 /** Serialization version */ 037 private static final long serialVersionUID = 4954918890077093841L; 038 039 /** Left object */ 040 public final L left; 041 /** Right object */ 042 public final R right; 043 044 /** 045 * <p>Obtains an immutable pair of from two objects inferring the generic types.</p> 046 * 047 * <p>This factory allows the pair to be created using inference to 048 * obtain the generic types.</p> 049 * 050 * @param <L> the left element type 051 * @param <R> the right element type 052 * @param left the left element, may be null 053 * @param right the right element, may be null 054 * @return a pair formed from the two parameters, not null 055 */ 056 public static <L, R> ImmutablePair<L, R> of(final L left, final R right) { 057 return new ImmutablePair<L, R>(left, right); 058 } 059 060 /** 061 * Create a new pair instance. 062 * 063 * @param left the left value, may be null 064 * @param right the right value, may be null 065 */ 066 public ImmutablePair(final L left, final R right) { 067 super(); 068 this.left = left; 069 this.right = right; 070 } 071 072 //----------------------------------------------------------------------- 073 /** 074 * {@inheritDoc} 075 */ 076 @Override 077 public L getLeft() { 078 return left; 079 } 080 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public R getRight() { 086 return right; 087 } 088 089 /** 090 * <p>Throws {@code UnsupportedOperationException}.</p> 091 * 092 * <p>This pair is immutable, so this operation is not supported.</p> 093 * 094 * @param value the value to set 095 * @return never 096 * @throws UnsupportedOperationException as this operation is not supported 097 */ 098 @Override 099 public R setValue(final R value) { 100 throw new UnsupportedOperationException(); 101 } 102 103}