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

com.github.tonivade.purefun.Tuple1 Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.purefun;

import java.io.Serializable;
import java.util.Objects;
import com.github.tonivade.purefun.data.Sequence;

@HigherKind
public final class Tuple1 implements Tuple, Tuple1Of, Serializable {

  private static final long serialVersionUID = 6343431593011527978L;

  private static final Equal> EQUAL = Equal.>of().comparing(Tuple1::get1);

  private final A value1;

  private Tuple1(A value1) {
    this.value1 = value1;
  }

  public A get1() {
    return value1;
  }

  @Override
  public Sequence toSequence() {
    return Sequence.listOf(value1);
  }

  public  Tuple1 map1(Function1 mapper) {
    return new Tuple1<>(mapper.apply(value1));
  }

  public static  Tuple1 of(A value1) {
    return new Tuple1<>(value1);
  }

  public  R applyTo(Function1 function) {
    return function.apply(value1);
  }

  @Override
  public int hashCode() {
    return Objects.hash(value1);
  }

  @Override
  public boolean equals(Object obj) {
    return EQUAL.applyTo(this, obj);
  }

  @Override
  public String toString() {
    return "Tuple1(" + value1 + ")";
  }
}