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  public abstract class AbstractOntologyResource implements OntologyResource {
37  
38      protected static Logger log = LoggerFactory.getLogger( AbstractOntologyResource.class );
39  
40      private static final long serialVersionUID = 1L;
41  
42      private static final Comparator<OntologyResource> comparator = Comparator
43              .comparing( OntologyResource::getScore, nullsLast( reverseOrder() ) )
44              .thenComparing( OntologyResource::getUri, nullsLast( naturalOrder() ) );
45  
46      private transient final OntResource res;
47      @Nullable
48      private final Double score;
49  
50      protected AbstractOntologyResource( OntResource resource ) {
51          this.res = resource;
52          this.score = null;
53      }
54  
55      public AbstractOntologyResource( OntResource resource, double score ) {
56          this.res = resource;
57          this.score = score;
58      }
59  
60      @Override
61      public String getUri() {
62          return res.getURI();
63      }
64  
65      public String getLabel() {
66          String label = res.getLabel( "EN" );
67          if ( label == null ) {
68              label = res.getLabel( null );
69          }
70          if ( label == null ) {
71              label = res.getLocalName();
72          }
73          if ( label == null ) {
74              label = res.getURI();
75          }
76          return label;
77      }
78  
79      @Override
80      public boolean isObsolete() {
81          return res.hasLiteral( OWL2.deprecated, true );
82      }
83  
84      @Override
85      @Nullable
86      public Double getScore() {
87          return score;
88      }
89  
90      @Override
91      public int compareTo( OntologyResource other ) {
92          return Objects.compare( this, other, comparator );
93      }
94  
95      @Override
96      public boolean equals( Object obj ) {
97          if ( this == obj ) return true;
98          if ( obj == null ) return false;
99          if ( getClass() != obj.getClass() ) return false;
100         final OntologyResource./ubic/basecode/ontology/model/OntologyResource.html#OntologyResource">OntologyResource other = ( OntologyResource ) obj;
101         if ( getLabel() == null ) {
102             if ( other.getLabel() != null ) return false;
103         } else if ( !getLabel().equals( other.getLabel() ) ) return false;
104         if ( getUri() == null ) {
105             if ( other.getUri() != null ) return false;
106         } else if ( !getUri().equals( other.getUri() ) ) return false;
107         return true;
108     }
109 
110     @Override
111     public int hashCode() {
112         return Objects.hash( getLabel(), getUri() );
113     }
114 
115     @Override
116     public String toString() {
117         String s = getLabel();
118         if ( s == null ) {
119             s = res.toString();
120         }
121         return s;
122     }
123 }