1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.ontology.jena;
20
21 import com.hp.hpl.jena.ontology.OntResource;
22 import com.hp.hpl.jena.vocabulary.OWL2;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import ubic.basecode.ontology.model.OntologyResource;
26
27 import javax.annotation.Nullable;
28 import java.util.Comparator;
29 import java.util.Objects;
30
31 import static java.util.Comparator.*;
32
33
34
35
36 abstract class AbstractOntologyResource implements OntologyResource {
37
38 protected static Logger log = LoggerFactory.getLogger( AbstractOntologyResource.class );
39
40 private static final Comparator<OntologyResource> comparator = Comparator
41 .comparing( OntologyResource::getScore, nullsLast( reverseOrder() ) )
42 .thenComparing( OntologyResource::getUri, nullsLast( naturalOrder() ) );
43
44 private final OntResource res;
45 @Nullable
46 private final Double score;
47
48 protected AbstractOntologyResource( OntResource resource ) {
49 this.res = resource;
50 this.score = null;
51 }
52
53 public AbstractOntologyResource( OntResource resource, double score ) {
54 this.res = resource;
55 this.score = score;
56 }
57
58 @Override
59 public String getUri() {
60 return res.getURI();
61 }
62
63 public String getLabel() {
64 String label = res.getLabel( "EN" );
65 if ( label == null ) {
66 label = res.getLabel( null );
67 }
68 if ( label == null ) {
69 label = res.getLocalName();
70 }
71 if ( label == null ) {
72 label = res.getURI();
73 }
74 return label;
75 }
76
77 @Override
78 public boolean isObsolete() {
79 return res.hasLiteral( OWL2.deprecated, true );
80 }
81
82 @Override
83 @Nullable
84 public Double getScore() {
85 return score;
86 }
87
88 @Override
89 public int compareTo( OntologyResource other ) {
90 return Objects.compare( this, other, comparator );
91 }
92
93 @Override
94 public boolean equals( Object obj ) {
95 if ( this == obj ) return true;
96 if ( obj == null ) return false;
97 if ( getClass() != obj.getClass() ) return false;
98 final OntologyResource./ubic/basecode/ontology/model/OntologyResource.html#OntologyResource">OntologyResource other = ( OntologyResource ) obj;
99 if ( getLabel() == null ) {
100 if ( other.getLabel() != null ) return false;
101 } else if ( !getLabel().equals( other.getLabel() ) ) return false;
102 if ( getUri() == null ) {
103 if ( other.getUri() != null ) return false;
104 } else if ( !getUri().equals( other.getUri() ) ) return false;
105 return true;
106 }
107
108 @Override
109 public int hashCode() {
110 return Objects.hash( getLabel(), getUri() );
111 }
112
113 @Override
114 public String toString() {
115 String s = getLabel();
116 if ( s == null ) {
117 s = res.toString();
118 }
119 return s;
120 }
121 }