All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ua.co.gravy.tuplets.Triplet Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 Vitaliy Berdinskikh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ua.co.gravy.tuplets;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;


/**
 * Group of three.
 *
 * @param  type of first element
 * @param  type of second element
 * @param  type of third element
 * @author Vitaliy Berdinskikh, ur6lad
 * @since 1.0
 */
public final class Triplet extends AbstractTuplet {

	private static final int ARITY = 3;

	private final S first;
	private final D second;
	private final T third;


	public Triplet(S first, D second, T third) {
		this.first = first;
		this.second = second;
		this.third = third;
	}


	/**
	 * Get first element.
	 *
	 * @return first element
	 */
	public S first() {
		return first;
	}


	/**
	 * Get second element.
	 *
	 * @return second element
	 */
	public D second() {
		return second;
	}


	/**
	 * Get third element.
	 *
	 * @return third element
	 */
	public T third() {
		return third;
	}


	@Override
	public Object get(int index) throws IndexOutOfBoundsException {
		Object result;

		switch(index) {
			case 0:
				result = first;
				break;
			case 1:
				result = second;
				break;
			case 2:
				result = third;
				break;
			default:
				throw new IndexOutOfBoundsException("Index " + index + " is out of bounds of triplet!");
		}

		return result;
	}


	@Override
	public int length() {
		return ARITY;
	}


	@Override
	public Stream stream() {
		return Stream.of(first, second, third);
	}


	@Override
	public Object[] toArray() {
		return new Object[] { first, second, third };
	}


	@Override
	public List toList() {
		return Arrays.asList(first, second, third);
	}


	/**
	 * Make new triplet: set parameter as first element and copy other.
	 *
	 * @param first first element
	 * @return new triplet
	 */
	public Triplet withFirst(S first) {
		return new Triplet<>(first, second, third);
	}


	/**
	 * Make new triplet: set parameter as second element and copy other.
	 *
	 * @param second second element
	 * @return new triplet
	 */
	public Triplet withSecond(D second) {
		return new Triplet<>(first, second, third);
	}


	/**
	 * Make new triplet: set parameter as third element and copy other.
	 *
	 * @param third third element
	 * @return new triplet
	 */
	public Triplet withThird(T third) {
		return new Triplet<>(first, second, third);
	}

}