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

io.permazen.util.ConvertedComparator Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package io.permazen.util;

import com.google.common.base.Converter;
import com.google.common.base.Preconditions;

import java.util.Comparator;
import java.util.Objects;

/**
 * Comparator that compares using converted values.
 */
class ConvertedComparator implements Comparator {

    private final Comparator comparator;
    private final Converter converter;

    ConvertedComparator(Comparator comparator, Converter converter) {
        Preconditions.checkArgument(converter != null, "null converter");
        this.comparator = comparator;
        this.converter = converter;
    }

    /**
     * Get the wrapped {@link Comparator}.
     *
     * @return the wrapped {@link Comparator}.
     */
    public Comparator getWrappedComparator() {
        return this.comparator;
    }

    /**
     * Get the {@link Converter} associated with this instance.
     *
     * @return associated {@link Converter}
     */
    public Converter getConverter() {
        return this.converter;
    }

    @Override
    @SuppressWarnings("unchecked")
    public int compare(E obj1, E obj2) {
        final W wobj1 = this.converter.convert(obj1);
        final W wobj2 = this.converter.convert(obj2);
        return NavigableSets.comparatorOrNatural(this.comparator).compare(wobj1, wobj2);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null || obj.getClass() != this.getClass())
            return false;
        final ConvertedComparator that = (ConvertedComparator)obj;
        return Objects.equals(this.comparator, that.comparator)
          && this.converter.equals(that.converter);
    }

    @Override
    public int hashCode() {
        return this.getClass().hashCode()
          ^ Objects.hashCode(this.comparator)
          ^ this.converter.hashCode();
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "[converter=" + this.converter + ",comparator=" + this.comparator + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy