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 boolean equals( Object obj ) {
87          if ( this == obj ) return true;
88          if ( obj == null ) return false;
89          if ( !( obj instanceof OntologyResource ) ) {
90              return false;
91          }
92          final OntologyResource./ubic/basecode/ontology/model/OntologyResource.html#OntologyResource">OntologyResource other = ( OntologyResource ) obj;
93          if ( getUri() == null && other.getUri() == null ) {
94              return Objects.equals( getLabel(), other.getLabel() );
95          } else {
96              return Objects.equals( getUri(), other.getUri() );
97          }
98      }
99  
100     @Override
101     public int hashCode() {
102         return Objects.hash( getUri() );
103     }
104 
105     @Override
106     public String toString() {
107         String s = getLabel();
108         if ( s == null ) {
109             s = res.getLocalName();
110         }
111         if ( s == null ) {
112             s = res.getURI();
113         }
114         if ( s == null ) {
115             s = res.toString();
116         }
117         return s;
118     }
119 }