com.bestvike.collections.generic.EqualityComparer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of linq Show documentation
Show all versions of linq Show documentation
LINQ to Objects for Java.
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;
}
}