@Override public int hashCode() { String id = getId(); return (id != null ? id.hashCode() : 0); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || !(obj instanceof OntologyTerm)) { return false; } return StringUtils.equals(getId(), ((OntologyTerm) obj).getId()); } }
@Override public String toString() { return "[" + this.getId() + "] " + this.getName(); }
/** * The field "term_category" in {@code this.doc} can contain the term itself. It appears that this only happens with * HPO. To avoid this problem, and to avoid writing a separate implementation for HPO specifically, this method * checks for existence of the term in the term_category and takes it out. */ private void removeSelfDuplicate() { Object value = this.doc.getFieldValue(TERM_CATEGORY); if (!(value instanceof List)) { return; } @SuppressWarnings("unchecked") List<String> listValue = (List<String>) value; listValue.remove(this.getId()); }
@Override public long getDistanceTo(final OntologyTerm other) { if (other == null) { return -1; } if (this.equals(other)) { return 0; } long distance = Integer.MAX_VALUE; Map<String, Integer> myLevelMap = new HashMap<String, Integer>(); myLevelMap.put(getId(), 0); Map<String, Integer> otherLevelMap = new HashMap<String, Integer>(); otherLevelMap.put(other.getId(), 0); Set<OntologyTerm> myCrtLevel = new HashSet<OntologyTerm>(); myCrtLevel.add(this); Set<OntologyTerm> otherCrtLevel = new HashSet<OntologyTerm>(); otherCrtLevel.add(other); for (int l = 1; l <= distance && !myCrtLevel.isEmpty() && !otherCrtLevel.isEmpty(); ++l) { distance = Math.min(distance, processAncestorsAtDistance(l, myCrtLevel, myLevelMap, otherLevelMap)); distance = Math.min(distance, processAncestorsAtDistance(l, otherCrtLevel, otherLevelMap, myLevelMap)); } return distance == Integer.MAX_VALUE ? -1 : distance; }
/** * Constructor that provides the backing {@link #doc Solr document} and the {@link #ontology owner ontology}. * * @param doc the {@link #doc Solr document} representing this term * @param ontology the {@link #ontology owner ontology} */ public SolrOntologyTerm(SolrDocument doc, OntologyService ontology) { this.doc = doc; this.ontology = ontology; if (doc != null) { this.removeSelfDuplicate(); this.parents = new LazySolrTermSet(doc.getFieldValues(IS_A), ontology); this.ancestors = new LazySolrTermSet(doc.getFieldValues(TERM_CATEGORY), ontology); Collection<Object> termSet = new HashSet<Object>(); termSet.add(this.getId()); if (doc.getFieldValues(TERM_CATEGORY) != null) { termSet.addAll(doc.getFieldValues(TERM_CATEGORY)); } this.ancestorsAndSelf = new LazySolrTermSet(termSet, this.ontology); } }