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

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

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

import java.util.Arrays;
import java.util.Objects;

@FunctionalInterface
public interface Eq {

  boolean eqv(T a, T b);

  default Eq and(Eq other) {
    return (a, b) -> this.eqv(a, b) && other.eqv(a, b);
  }

  static  Eq any() {
    return Objects::equals;
  }

  static  Eq always() {
    return (a, b) -> true;
  }

  static Eq throwable() {
    return comparing(Throwable::getMessage).and(comparingArray(Throwable::getStackTrace));
  }

  public static  Eq comparing(Function1 getter) {
    return (a, b) -> Objects.equals(getter.apply(a), getter.apply(b));
  }

  public static  Eq comparingArray(Function1 getter) {
    return (a, b) -> Arrays.deepEquals(getter.apply(a), getter.apply(b));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy