org.pkl.thirdparty.paguro.collections.Equator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-tools Show documentation
Show all versions of pkl-tools Show documentation
Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.
// Copyright 2015 PlanBase Inc. & Glen Peterson
//
// 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 org.pkl.thirdparty.paguro.collections;
import org.pkl.thirdparty.jetbrains.annotations.NotNull;
import org.pkl.thirdparty.jetbrains.annotations.Nullable;
import java.util.Comparator;
/**
An Equator represents an equality context in a way that is analgous to the java.util.Comparator
interface.
Comparing Objects is Relative
This will need to be passed to Hash-based collections the way a Comparator is passed to tree-based
ones.
The method names hash() and eq() are intentionally elisions of hashCode() and equals() so that your
IDE will suggest the shorter name as you start typing which is almost always what you want.
You want the hash() and eq() methods because that's how Equators compare things. You don't want
an equator's .hashCode() or .equals() methods because those are for comparing *Equators* and are
inherited from java.lang.Object. I'd deprecate those methods, but you can't do that on an
interface.
A common mistake is to implement an Equator, ComparisonContext, or Comparator as an anonymous class
or lambda, then be surprised when it is can't be serialized, or is deserialized as null. These
one-off classes are often singletons, which are easiest to serialize as enums. If your
implementation requires generic type parameters, observe how {@link #defaultEquator()} tricks
the type system into using generic type parameters (correctly) with an enum.
*/
public interface Equator {
// ============================================= Static ========================================
// Enums are serializable and lambdas are not. Therefore enums make better singletons.
enum Equat implements Equator
© 2015 - 2024 Weber Informatics LLC | Privacy Policy