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

com.simplaex.bedrock.Quadruple Maven / Gradle / Ivy

package com.simplaex.bedrock;

import lombok.Value;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.List;
import java.util.function.Function;

@Value(staticConstructor = "of")
@Immutable
public class Quadruple implements Serializable, Comparable>, Tuple4 {

  private A first;
  private B second;
  private C third;
  private D fourth;

  @SuppressWarnings("unchecked")
  @Override
  public int compareTo(@Nonnull final Quadruple tuple) {
    int r;
    if (first == null) {
      if (tuple.first == null) {
        r = 0;
      } else {
        return -1;
      }
    } else if (tuple.first == null) {
      return 1;
    } else {
      r = ((Comparable) first).compareTo(tuple.first);
    }
    if (r != 0) {
      return r;
    }
    if (second == null) {
      if (tuple.second == null) {
        r = 0;
      } else {
        return -1;
      }
    } else if (tuple.second == null) {
      return 1;
    } else {
      r = ((Comparable) second).compareTo(tuple.second);
    }
    if (r != 0) {
      return r;
    }
    if (third == null) {
      if (tuple.third == null) {
        r = 0;
      } else {
        return -1;
      }
    } else if (tuple.third == null) {
      return 1;
    } else {
      r = ((Comparable) third).compareTo(tuple.third);
    }
    if (r != 0) {
      return r;
    }
    if (fourth == null) {
      if (tuple.fourth == null) {
        return 0;
      } else {
        return -1;
      }
    } else if (tuple.fourth == null) {
      return 1;
    } else {
      return ((Comparable) fourth).compareTo(tuple.fourth);
    }
  }

  @Nonnull
  public static  List toList(final Quadruple tuple) {
    return new AbstractList() {
      @Override
      public E get(final int index) {
        switch (index) {
          case 0:
            return tuple.getFirst();
          case 1:
            return tuple.getSecond();
          case 2:
            return tuple.getThird();
          case 3:
            return tuple.getFourth();
          default:
            return null;
        }
      }

      @Override
      public int size() {
        return 4;
      }
    };
  }

  @Nonnull
  public List toList() {
    return toList(this);
  }

  @Nonnull
  public static  Seq toSeq(final Tuple4 quadruple) {
    return Seq.of(quadruple.getFirst(), quadruple.getSecond(), quadruple.getThird(), quadruple.getFourth());
  }

  @Nonnull
  public Seq toSeq() {
    return toSeq(this);
  }

  @Nonnull
  public  Quadruple map(
    final Function f,
    final Function g,
    final Function h,
    final Function i
  ) {
    return Quadruple.of(f.apply(getFirst()), g.apply(getSecond()), h.apply(getThird()), i.apply(getFourth()));
  }

  @Nonnull
  public  Quadruple mapFirst(final Function f) {
    return Quadruple.of(f.apply(getFirst()), getSecond(), getThird(), getFourth());
  }

  @Nonnull
  public  Quadruple mapSecond(final Function f) {
    return Quadruple.of(getFirst(), f.apply(getSecond()), getThird(), getFourth());
  }

  @Nonnull
  public  Quadruple mapThird(final Function f) {
    return Quadruple.of(getFirst(), getSecond(), f.apply(getThird()), getFourth());
  }

  @Nonnull
  public  Quadruple mapFourth(final Function f) {
    return Quadruple.of(getFirst(), getSecond(), getThird(), f.apply(getFourth()));
  }

  @Nonnull
  public  Quadruple withFirst(final E v) {
    return Quadruple.of(v, getSecond(), getThird(), getFourth());
  }

  @Nonnull
  public  Quadruple withSecond(final E v) {
    return Quadruple.of(getFirst(), v, getThird(), getFourth());
  }

  @Nonnull
  public  Quadruple withThird(final E v) {
    return Quadruple.of(getFirst(), getSecond(), v, getFourth());
  }

  @Nonnull
  public  Quadruple withFourth(final E v) {
    return Quadruple.of(getFirst(), getSecond(), getThird(), v);
  }

  public static  Quadruple quadruple(final A a, final B b, final C c, final D d) {
    return Quadruple.of(a, b, c, d);
  }
}