implemented comparable interface on few fields
This commit is contained in:
parent
ee4a01f711
commit
fb05d15ff2
|
@ -1,10 +1,12 @@
|
|||
|
||||
package eu.dnetlib.dhp.schema.sx.scholix;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public class Scholix implements Serializable {
|
||||
public class Scholix implements Serializable, Comparable<Scholix> {
|
||||
private String publicationDate;
|
||||
|
||||
private List<ScholixEntityId> publisher;
|
||||
|
@ -74,4 +76,26 @@ public class Scholix implements Serializable {
|
|||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(Scholix other) {
|
||||
|
||||
if (other == null)
|
||||
return -1;
|
||||
|
||||
// Comparing publication date
|
||||
final int publicationDateCompare =StringUtils.compare(publicationDate, other.getPublicationDate());
|
||||
|
||||
if (publicationDateCompare != 0)
|
||||
return publicationDateCompare;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package eu.dnetlib.dhp.schema.sx.scholix;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.text.Normalizer;
|
||||
import java.util.List;
|
||||
|
||||
public class ScholixComparator {
|
||||
|
||||
|
||||
public static String normalizeString(final String input) {
|
||||
if (input == null)
|
||||
return null;
|
||||
|
||||
return Normalizer.normalize(input, Normalizer.Form.NFD)
|
||||
.toLowerCase()
|
||||
.replaceAll("[^a-zA-Z0-9]", "");
|
||||
}
|
||||
|
||||
|
||||
public static int compareScholixEntityId(final List<ScholixEntityId> first, final List<ScholixEntityId> second) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int compareString(final String first, final String second) {
|
||||
|
||||
|
||||
if (first == null && second == null)
|
||||
return 0;
|
||||
if (first==null )
|
||||
return 1;
|
||||
if (second == null)
|
||||
return -1;
|
||||
return first.compareTo(second);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,17 @@
|
|||
|
||||
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;
|
||||
|
||||
public class ScholixEntityId implements Serializable {
|
||||
import static eu.dnetlib.dhp.schema.sx.scholix.ScholixComparator.normalizeString;
|
||||
|
||||
public class ScholixEntityId implements Serializable, Comparable<ScholixEntityId> {
|
||||
private String name;
|
||||
private List<ScholixIdentifier> identifiers;
|
||||
|
||||
|
@ -31,4 +38,39 @@ public class ScholixEntityId implements Serializable {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
|
||||
package eu.dnetlib.dhp.schema.sx.scholix;
|
||||
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
||||
import static eu.dnetlib.dhp.schema.sx.scholix.ScholixComparator.normalizeString;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ScholixIdentifier implements Serializable {
|
||||
public class ScholixIdentifier implements Serializable, Comparable<ScholixIdentifier> {
|
||||
private String identifier;
|
||||
private String schema;
|
||||
private String url;
|
||||
|
@ -27,20 +32,6 @@ public class ScholixIdentifier implements Serializable {
|
|||
this.url = url;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ScholixIdentifier that = (ScholixIdentifier) o;
|
||||
return identifier.equals(that.identifier) && schema.equals(that.schema);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(identifier, schema);
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
@ -56,4 +47,33 @@ public class ScholixIdentifier implements Serializable {
|
|||
public void setSchema(String schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ScholixIdentifier o) {
|
||||
final int idComp = StringUtils.compare(normalizeString(identifier), normalizeString(o.getIdentifier()));
|
||||
if (idComp !=0)
|
||||
return idComp;
|
||||
final int schemaComp = StringUtils.compare(normalizeString(schema), normalizeString(o.getSchema()));
|
||||
if (schemaComp !=0)
|
||||
return schemaComp;
|
||||
final int urlComp = StringUtils.compare(normalizeString(url), normalizeString(o.getUrl()));
|
||||
return urlComp;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ScholixIdentifier that = (ScholixIdentifier) o;
|
||||
return compareTo(that) == 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(normalizeString(identifier), normalizeString(schema), normalizeString(url));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package eu.dnetlib.dhp.schema.sx.scholix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ScholixCompareTest {
|
||||
|
||||
@Test
|
||||
public void testNormalization() {
|
||||
final String input = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġhttps://doi.org/< >10.11646/zootaxa.5099.1.3";
|
||||
final String expected = "thisisafunkystringhttpsdoiorg1011646zootaxa509913";
|
||||
final String normalized = ScholixComparator.normalizeString(input);
|
||||
assertEquals(normalized, expected);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testScholixIdentifierComparison() {
|
||||
final String id = "10.11646/zootaxa.5099.1.3";
|
||||
final String schema = "DOI";
|
||||
final String url ="http://dx.dOI.org/10.11646/Zootaxa.5099.1.3";
|
||||
|
||||
final ScholixIdentifier left = new ScholixIdentifier();
|
||||
left.setIdentifier(id.toUpperCase());
|
||||
left.setSchema(schema.toUpperCase());
|
||||
left.setUrl(url.toUpperCase());
|
||||
|
||||
|
||||
final ScholixIdentifier right = new ScholixIdentifier();
|
||||
right.setIdentifier(id.toUpperCase());
|
||||
right.setSchema(schema.toUpperCase());
|
||||
right.setUrl(url.toLowerCase());
|
||||
|
||||
|
||||
assertEquals(0,left.compareTo(right));
|
||||
|
||||
assertEquals(left, right);
|
||||
assertEquals(left.hashCode(), right.hashCode());
|
||||
|
||||
|
||||
left.setIdentifier(null);
|
||||
|
||||
assertEquals(-1, left.compareTo(right));
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue