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 org.jspecify.annotations.Nullable;
28 import java.util.Objects;
29
30
31
32
33 abstract class AbstractOntologyResource implements OntologyResource {
34
35 protected static final Logger log = LoggerFactory.getLogger( AbstractOntologyResource.class );
36
37 private final OntResource res;
38
39 @Nullable
40 private String _label;
41 private boolean _isLabelNull = false;
42
43 protected AbstractOntologyResource( OntResource resource ) {
44 this.res = resource;
45 }
46
47 @Nullable
48 @Override
49 public String getUri() {
50 return res.getURI();
51 }
52
53 @Override
54 public String getLocalName() {
55 return res.getLocalName();
56 }
57
58 @Nullable
59 @Override
60 public String getLabel() {
61 if ( _label != null || _isLabelNull ) {
62 return _label;
63 }
64 String label = res.getLabel( "EN" );
65 if ( label == null ) {
66 label = res.getLabel( null );
67 }
68 _label = label;
69 _isLabelNull = label == null;
70 return label;
71 }
72
73 @Nullable
74 @Override
75 public String getComment() {
76 String label = res.getComment( "EN" );
77 if ( label == null ) {
78 label = res.getLabel( null );
79 }
80 return label;
81 }
82
83 @Override
84 public boolean isObsolete() {
85 return res.hasLiteral( OWL2.deprecated, true );
86 }
87
88 @Override
89 public <T> T unwrap( Class<T> clazz ) throws ClassCastException {
90 return clazz.cast( res );
91 }
92
93 public boolean equals( Object obj ) {
94 if ( this == obj ) return true;
95 if ( obj == null ) return false;
96 if ( !( obj instanceof OntologyResource ) ) {
97 return false;
98 }
99 final OntologyResource other = ( OntologyResource ) obj;
100 if ( getUri() == null && other.getUri() == null ) {
101 return Objects.equals( getLabel(), other.getLabel() );
102 } else {
103 return Objects.equals( getUri(), other.getUri() );
104 }
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash( getUri() );
110 }
111
112 @Override
113 public String toString() {
114 String s = getLabel();
115 if ( s == null ) {
116 s = res.getLocalName();
117 }
118 if ( s == null ) {
119 s = res.getURI();
120 }
121 if ( s == null ) {
122 s = res.toString();
123 }
124 return s;
125 }
126 }