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

com.bestvike.collections.generic.EqualityComparer Maven / Gradle / Ivy

The newest version!
package com.bestvike.collections.generic;

import java.util.Objects;

/**
 * Created by 许崇雷 on 2017-07-18.
 */
public final class EqualityComparer implements IEqualityComparer {
    private static final EqualityComparer DEFAULT = new EqualityComparer<>();

    private EqualityComparer() {
    }

    public static  EqualityComparer Default() {
        //noinspection unchecked
        return (EqualityComparer) DEFAULT;
    }

    @Override
    public boolean equals(T x, T y) {
        return Objects.equals(x, y);
    }

    @Override
    public int hashCode(T obj) {
        return obj == null ? 0 : obj.hashCode();
    }

    public int indexOf(Object[] array, Object value, int startIndex, int count) {
        int endIndex = startIndex + count;
        if (value == null) {
            for (int i = startIndex; i < endIndex; i++) {
                if (array[i] == null)
                    return i;
            }
        } else {
            for (int i = startIndex; i < endIndex; i++) {
                if (array[i] != null && array[i].equals(value))
                    return i;
            }
        }
        return -1;
    }

    public int lastIndexOf(T[] array, T value, int startIndex, int count) {
        int endIndex = startIndex - count + 1;
        if (value == null) {
            for (int i = startIndex; i >= endIndex; i--) {
                if (array[i] == null)
                    return i;
            }
        } else {
            for (int i = startIndex; i >= endIndex; i--) {
                if (array[i] != null && array[i].equals(value))
                    return i;
            }
        }
        return -1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy