dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/sx/scholix/ScholixEntityId.java

77 lines
1.9 KiB
Java
Raw Normal View History

package eu.dnetlib.dhp.schema.sx.scholix;
import com.google.common.collect.Iterators;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import static eu.dnetlib.dhp.schema.sx.scholix.ScholixComparator.normalizeString;
public class ScholixEntityId implements Serializable, Comparable<ScholixEntityId> {
private String name;
private List<ScholixIdentifier> identifiers;
public ScholixEntityId() {
}
public ScholixEntityId(String name, List<ScholixIdentifier> identifiers) {
this.name = name;
this.identifiers = identifiers;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ScholixIdentifier> getIdentifiers() {
return identifiers;
}
public void setIdentifiers(List<ScholixIdentifier> identifiers) {
this.identifiers = identifiers;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ScholixEntityId that = (ScholixEntityId) o;
return compareTo(that)==0;
}
@Override
public int hashCode() {
return Objects.hash(name, identifiers);
}
@Override
public int compareTo(ScholixEntityId other) {
final int nameComp = StringUtils.compare(normalizeString(name), normalizeString(other.getName()));
if (nameComp != 0)
return nameComp;
if (identifiers == null && other.getIdentifiers() == null)
return 0;
if (identifiers == null)
return 1;
if (other.getIdentifiers() == null)
return -1;
if (identifiers.size()!= other.getIdentifiers().size())
return -1;
Stream<ScholixIdentifier> sortedLeft = identifiers.stream().sorted();
Stream<ScholixIdentifier> sortedRight = other.getIdentifiers().stream().sorted();
boolean equalsStream = Iterators.elementsEqual(sortedLeft.iterator(), sortedRight.iterator());
return equalsStream?0:-1;
}
}