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