001//
002// MIT License
003//
004// Copyright (c) 2021 Alexander Söderberg & Contributors
005//
006// Permission is hereby granted, free of charge, to any person obtaining a copy
007// of this software and associated documentation files (the "Software"), to deal
008// in the Software without restriction, including without limitation the rights
009// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010// copies of the Software, and to permit persons to whom the Software is
011// furnished to do so, subject to the following conditions:
012//
013// The above copyright notice and this permission notice shall be included in all
014// copies or substantial portions of the Software.
015//
016// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022// SOFTWARE.
023//
024package cloud.commandframework.types.tuples;
025
026import org.checkerframework.checker.nullness.qual.NonNull;
027
028import java.util.Objects;
029
030/**
031 * Immutable generic 5-tuple
032 *
033 * @param <U> First type
034 * @param <V> Second type
035 * @param <W> Third type
036 * @param <X> Fourth type
037 */
038public class Quartet<U, V, W, X> implements Tuple {
039
040    private final U first;
041    private final V second;
042    private final W third;
043    private final X fourth;
044
045    protected Quartet(
046            final @NonNull U first,
047            final @NonNull V second,
048            final @NonNull W third,
049            final @NonNull X fourth
050    ) {
051        this.first = first;
052        this.second = second;
053        this.third = third;
054        this.fourth = fourth;
055    }
056
057    /**
058     * Create a new 4-tuple
059     *
060     * @param first  First value
061     * @param second Second value
062     * @param third  Third value
063     * @param fourth Fourth value
064     * @param <U>    First type
065     * @param <V>    Second type
066     * @param <W>    Third type
067     * @param <X>    Fourth type
068     * @return Created quartet
069     */
070    public static <U, V, W, X> @NonNull Quartet<@NonNull U, @NonNull V, @NonNull W, @NonNull X> of(
071            final @NonNull U first,
072            final @NonNull V second,
073            final @NonNull W third,
074            final @NonNull X fourth
075    ) {
076        return new Quartet<>(first, second, third, fourth);
077    }
078
079    /**
080     * Get the first value
081     *
082     * @return First value
083     */
084    public final @NonNull U getFirst() {
085        return this.first;
086    }
087
088    /**
089     * Get the second value
090     *
091     * @return Second value
092     */
093    public final @NonNull V getSecond() {
094        return this.second;
095    }
096
097    /**
098     * Get the third value
099     *
100     * @return Third value
101     */
102    public final @NonNull W getThird() {
103        return this.third;
104    }
105
106    /**
107     * Get the fourth value
108     *
109     * @return Fourth value
110     */
111    public final @NonNull X getFourth() {
112        return this.fourth;
113    }
114
115    @Override
116    public final boolean equals(final Object o) {
117        if (this == o) {
118            return true;
119        }
120        if (o == null || getClass() != o.getClass()) {
121            return false;
122        }
123        final Quartet<?, ?, ?, ?> quartet = (Quartet<?, ?, ?, ?>) o;
124        return Objects.equals(getFirst(), quartet.getFirst())
125                && Objects.equals(getSecond(), quartet.getSecond())
126                && Objects.equals(getThird(), quartet.getThird())
127                && Objects.equals(getFourth(), quartet.getFourth());
128    }
129
130    @Override
131    public final int hashCode() {
132        return Objects.hash(getFirst(), getSecond(), getThird(), getFourth());
133    }
134
135    @Override
136    public final String toString() {
137        return String.format("(%s, %s, %s, %s)", this.first, this.second, this.third, this.fourth);
138    }
139
140    @Override
141    public final int getSize() {
142        return 4;
143    }
144
145    @Override
146    public final @NonNull Object @NonNull [] toArray() {
147        final Object[] array = new Object[4];
148        array[0] = this.first;
149        array[1] = this.second;
150        array[3] = this.third;
151        array[4] = this.fourth;
152        return array;
153    }
154
155}