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 triple consisting of three {@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 triple, then the triple
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 all three objects are thread-safe</p>
028 *
029 * @param <L> the left element type
030 * @param <M> the middle element type
031 * @param <R> the right element type
032 *
033 * @since 3.2
034 */
035public final class ImmutableTriple<L, M, R> extends Triple<L, M, R> {
036
037    /**
038     * An immutable triple of nulls.
039     */
040    // This is not defined with generics to avoid warnings in call sites.
041    @SuppressWarnings("rawtypes")
042    private static final ImmutableTriple NULL = of(null, null, null);
043
044    /** Serialization version */
045    private static final long serialVersionUID = 1L;
046
047    /**
048     * Returns an immutable triple of nulls.
049     *
050     * @param <L> the left element of this triple. Value is {@code null}.
051     * @param <M> the middle element of this triple. Value is {@code null}.
052     * @param <R> the right element of this triple. Value is {@code null}.
053     * @return an immutable triple of nulls.
054     * @since 3.6
055     */
056    @SuppressWarnings("unchecked")
057    public static <L, M, R> ImmutableTriple<L, M, R> nullTriple() {
058        return NULL;
059    }
060
061    /** Left object */
062    public final L left;
063    /** Middle object */
064    public final M middle;
065    /** Right object */
066    public final R right;
067
068    /**
069     * <p>Obtains an immutable triple of three objects inferring the generic types.</p>
070     *
071     * <p>This factory allows the triple to be created using inference to
072     * obtain the generic types.</p>
073     *
074     * @param <L> the left element type
075     * @param <M> the middle element type
076     * @param <R> the right element type
077     * @param left  the left element, may be null
078     * @param middle  the middle element, may be null
079     * @param right  the right element, may be null
080     * @return a triple formed from the three parameters, not null
081     */
082    public static <L, M, R> ImmutableTriple<L, M, R> of(final L left, final M middle, final R right) {
083        return new ImmutableTriple<>(left, middle, right);
084    }
085
086    /**
087     * Create a new triple instance.
088     *
089     * @param left  the left value, may be null
090     * @param middle the middle value, may be null
091     * @param right  the right value, may be null
092     */
093    public ImmutableTriple(final L left, final M middle, final R right) {
094        super();
095        this.left = left;
096        this.middle = middle;
097        this.right = right;
098    }
099
100    //-----------------------------------------------------------------------
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public L getLeft() {
106        return left;
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    public M getMiddle() {
114        return middle;
115    }
116
117    /**
118     * {@inheritDoc}
119     */
120    @Override
121    public R getRight() {
122        return right;
123    }
124}
125