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.Comparator;
29  import java.util.Objects;
30  
31  import static java.util.Comparator.*;
32  
33  /**
34   * @author pavlidis
35   */
36  abstract class AbstractOntologyResource implements OntologyResource {
37  
38      protected static final 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      @Override
64      public String getLocalName() {
65          return res.getLocalName();
66      }
67  
68      @Override
69      public String getLabel() {
70          String label = res.getLabel( "EN" );
71          if ( label == null ) {
72              label = res.getLabel( null );
73          }
74          return label;
75      }
76  
77      @Nullable
78      @Override
79      public String getComment() {
80          String label = res.getComment( "EN" );
81          if ( label == null ) {
82              label = res.getLabel( null );
83          }
84          return label;
85      }
86  
87      @Override
88      public boolean isObsolete() {
89          return res.hasLiteral( OWL2.deprecated, true );
90      }
91  
92      @Override
93      @Nullable
94      public Double getScore() {
95          return score;
96      }
97  
98      @Override
99      public int compareTo( OntologyResource other ) {
100         return Objects.compare( this, other, comparator );
101     }
102 
103     @Override
104     public boolean equals( Object obj ) {
105         if ( this == obj ) return true;
106         if ( obj == null ) return false;
107         if ( getClass() != obj.getClass() ) return false;
108         final OntologyResource./ubic/basecode/ontology/model/OntologyResource.html#OntologyResource">OntologyResource other = ( OntologyResource ) obj;
109         if ( getLabel() == null ) {
110             if ( other.getLabel() != null ) return false;
111         } else if ( !getLabel().equals( other.getLabel() ) ) return false;
112         if ( getUri() == null ) {
113             return other.getUri() == null;
114         } else return getUri().equals( other.getUri() );
115     }
116 
117     @Override
118     public int hashCode() {
119         return Objects.hash( getLabel(), getUri() );
120     }
121 
122     @Override
123     public String toString() {
124         String s = getLabel();
125         if ( s == null ) {
126             s = res.getLocalName();
127         }
128         if ( s == null ) {
129             s = res.getURI();
130         }
131         if ( s == null ) {
132             s = res.toString();
133         }
134         return s;
135     }
136 }