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

com.mmnaseri.utils.tuples.impl.TwoTuple Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package com.mmnaseri.utils.tuples.impl;

import com.mmnaseri.utils.tuples.facade.HasFirst;
import com.mmnaseri.utils.tuples.facade.HasSecond;

import java.util.function.Function;
import java.util.function.Supplier;

import static com.mmnaseri.utils.tuples.utils.TupleUtils.checkIndex;

/**
 * Class for dealing with a {@link com.mmnaseri.utils.tuples.FixedTuple} with two elements.
 *
 * @author Milad Naseri ([email protected])
 */
public class TwoTuple extends AbstractFixedTuple>
    implements HasFirst>, HasSecond> {

  /** Creates a new instance of this class from the provided values. */
  public TwoTuple(A first, B second) {
    super(first, second);
  }

  /**
   * Returns a new tuple by keeping all the values from this tuple and overriding the value at the
   * provided index with the value returned from the supplier.
   */
  @Override
  public TwoTuple change(int index, Supplier supplier) {
    checkIndex(index, size());
    return new TwoTuple<>(
        index == 0 ? supplier.get() : first(), index == 1 ? supplier.get() : second());
  }

  /**
   * Returns a new tuple by keeping all the values from this tuple and overriding the value at the
   * provided index with the value returned from the function.
   */
  @Override
  public TwoTuple change(int index, Function, ? extends Z> function) {
    checkIndex(index, size());
    return new TwoTuple<>(
        index == 0 ? function.apply(this) : first(), index == 1 ? function.apply(this) : second());
  }

  /**
   * Returns a new tuple of one size larger by adding the provided value to the end of this tuple.
   */
  @Override
  public  ThreeTuple extend(X value) {
    return new ThreeTuple<>(first(), second(), value);
  }

  /**
   * Returns a new tuple of one size larger by adding the value returned from the supplier to the
   * end of this tuple.
   */
  @Override
  public  ThreeTuple extend(Supplier supplier) {
    return new ThreeTuple<>(first(), second(), supplier.get());
  }

  /**
   * Returns a new tuple of one size larger by adding the value returned from the function to the
   * end of this tuple.
   */
  @Override
  public  ThreeTuple extend(Function, X> function) {
    return new ThreeTuple<>(first(), second(), function.apply(this));
  }

  /**
   * Returns a new tuple of the same size by keeping all the values from this tuple and overriding
   * the first element with the provided value.
   */
  @Override
  public  TwoTuple first(X value) {
    return new TwoTuple<>(value, second());
  }

  /**
   * Returns a new tuple of the same size by keeping all the values from this tuple and overriding
   * the first element with the value returned from the given supplier.
   */
  @Override
  public  TwoTuple first(Supplier supplier) {
    return new TwoTuple<>(supplier.get(), second());
  }

  /**
   * Returns a new tuple of the same size by keeping all the values from this tuple and overriding
   * the first element with the value returned by applying the given function to this tuple's first
   * element.
   */
  @Override
  public  TwoTuple first(Function function) {
    return new TwoTuple<>(function.apply(first()), second());
  }

  /**
   * Returns a new tuple of one size smaller by keeping all the values from this tuple except the
   * first element.
   */
  @Override
  public OneTuple dropFirst() {
    return new OneTuple<>(second());
  }

  /**
   * Returns a new tuple of the same size by keeping all the values from this tuple and overriding
   * the second element with the provided value.
   */
  @Override
  public  TwoTuple second(X value) {
    return new TwoTuple<>(first(), value);
  }

  /**
   * Returns a new tuple of the same size by keeping all the values from this tuple and overriding
   * the second element with the value returned from the given supplier.
   */
  @Override
  public  TwoTuple second(Supplier supplier) {
    return new TwoTuple<>(first(), supplier.get());
  }

  /**
   * Returns a new tuple of the same size by keeping all the values from this tuple and overriding
   * the second element with the value returned by applying the given function to this tuple's
   * second element.
   */
  @Override
  public  TwoTuple second(Function function) {
    return new TwoTuple<>(first(), function.apply(second()));
  }

  /**
   * Returns a new tuple of one size smaller by keeping all the values from this tuple except the
   * second element.
   */
  @Override
  public OneTuple dropSecond() {
    return new OneTuple<>(first());
  }

  /**
   * Extends the tuple to which this is applied by adding the provided value to the end.
   *
   * 

This is especially useful in functional contexts. For instance: * *

   * threeTupleStream = twoTupleStream.map(TwoTuple.extendWith(value));
   * 
* * @see #extend(Object) */ public static Function, ThreeTuple> extendWith(X value) { return tuple -> tuple.extend(value); } /** * Extends the tuple to which this is applied by adding the value from the supplier to the end. * *

This is especially useful in functional contexts. For instance: * *

   * threeTupleStream = twoTupleStream.map(TwoTuple.extendWith(supplier));
   * 
* * @see #extend(Object) */ public static Function, ThreeTuple> extendWith(Supplier supplier) { return tuple -> tuple.extend(supplier); } /** * Extends the tuple to which this is applied by adding the value from the function to the end. * *

This is especially useful in functional contexts. For instance: * *

   * threeTupleStream = twoTupleStream.map(TwoTuple.extendWith(function));
   * 
* * @see #extend(Object) */ public static Function, ThreeTuple> extendWith( Function, X> function) { return tuple -> tuple.extend(function); } /** Creates a new instance of this class. */ public static TwoTuple of(A first, B second) { return new TwoTuple<>(first, second); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy