dnet-hadoop/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java

105 lines
2.5 KiB
Java
Raw Normal View History

package eu.dnetlib.dhp.schema.oaf;
import java.util.*;
2020-02-19 17:29:05 +01:00
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.google.common.base.Preconditions.checkArgument;
2020-02-20 11:43:32 +01:00
public class Relation extends Oaf {
2020-02-20 11:43:32 +01:00
private String relType;
2020-02-20 11:43:32 +01:00
private String subRelType;
2020-02-20 11:43:32 +01:00
private String relClass;
2020-02-20 11:43:32 +01:00
private String source;
2020-02-20 11:43:32 +01:00
private String target;
2020-02-20 11:43:32 +01:00
private List<KeyValue> collectedFrom = new ArrayList<>();
2020-02-20 11:43:32 +01:00
public String getRelType() {
return relType;
}
2020-02-20 11:43:32 +01:00
public void setRelType(final String relType) {
this.relType = relType;
}
2020-02-20 11:43:32 +01:00
public String getSubRelType() {
return subRelType;
}
2020-02-20 11:43:32 +01:00
public void setSubRelType(final String subRelType) {
this.subRelType = subRelType;
}
2020-02-20 11:43:32 +01:00
public String getRelClass() {
return relClass;
}
2020-02-20 11:43:32 +01:00
public void setRelClass(final String relClass) {
this.relClass = relClass;
}
2020-02-20 11:43:32 +01:00
public String getSource() {
return source;
}
2020-02-20 11:43:32 +01:00
public void setSource(final String source) {
this.source = source;
}
2020-02-20 11:43:32 +01:00
public String getTarget() {
return target;
}
2020-02-20 11:43:32 +01:00
public void setTarget(final String target) {
this.target = target;
}
2020-02-20 11:43:32 +01:00
public List<KeyValue> getCollectedFrom() {
return collectedFrom;
}
2020-02-20 11:43:32 +01:00
public void setCollectedFrom(final List<KeyValue> collectedFrom) {
this.collectedFrom = collectedFrom;
}
2020-02-19 17:29:05 +01:00
2020-02-20 11:43:32 +01:00
public void mergeFrom(final Relation r) {
checkArgument(Objects.equals(getSource(), r.getSource()),"source ids must be equal");
checkArgument(Objects.equals(getTarget(), r.getTarget()),"target ids must be equal");
checkArgument(Objects.equals(getRelType(), r.getRelType()),"relType(s) must be equal");
checkArgument(Objects.equals(getSubRelType(), r.getSubRelType()),"subRelType(s) must be equal");
checkArgument(Objects.equals(getRelClass(), r.getRelClass()),"relClass(es) must be equal");
setCollectedFrom(
Stream
.concat(Optional.ofNullable(getCollectedFrom()).map(Collection::stream).orElse(Stream.empty()),
Optional.ofNullable(r.getCollectedFrom()).map(Collection::stream).orElse(Stream.empty()))
.distinct() // relies on KeyValue.equals
.collect(Collectors.toList()));
2020-02-20 11:43:32 +01:00
}
2020-02-19 17:29:05 +01:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Relation relation = (Relation) o;
return relType.equals(relation.relType) &&
subRelType.equals(relation.subRelType) &&
relClass.equals(relation.relClass) &&
source.equals(relation.source) &&
2020-04-04 14:03:43 +02:00
target.equals(relation.target);
}
@Override
public int hashCode() {
return Objects.hash(relType, subRelType, relClass, source, target, collectedFrom);
}
}