View Javadoc
1   /*
2    * The basecode project
3    *
4    * Copyright (c) 2007-2019 University of British Columbia
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * @author pavlidis
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 }