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.rdf.model.RDFNode;
22  import com.hp.hpl.jena.rdf.model.Resource;
23  import com.hp.hpl.jena.rdf.model.Statement;
24  import com.hp.hpl.jena.vocabulary.RDFS;
25  import ubic.basecode.ontology.model.AnnotationProperty;
26  
27  import javax.annotation.Nullable;
28  
29  /**
30   * Note that this is a concrete instance of the annotation.
31   *
32   * @author pavlidis
33   */
34  class AnnotationPropertyImpl extends AbstractOntologyResource implements AnnotationProperty {
35  
36      private final com.hp.hpl.jena.ontology.AnnotationProperty property;
37      @Nullable
38      private final RDFNode object;
39  
40      /**
41       * @param prop   property for the statement
42       * @param object of the statement
43       */
44      public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop, @Nullable RDFNode object ) {
45          super( prop );
46          this.property = prop;
47          this.object = object;
48      }
49  
50      @Override
51      public boolean equals( @Nullable Object obj ) {
52          if ( this == obj ) return true;
53          if ( obj == null ) return false;
54          if ( getClass() != obj.getClass() ) return false;
55          final AnnotationPropertyImpl/basecode/ontology/jena/AnnotationPropertyImpl.html#AnnotationPropertyImpl">AnnotationPropertyImpl other = ( AnnotationPropertyImpl ) obj;
56          if ( object == null ) {
57              if ( other.object != null ) return false;
58          } else if ( !object.equals( other.object ) ) return false;
59          if ( property == null ) {
60              return other.property == null;
61          } else return property.equals( other.property );
62      }
63  
64      @Override
65      public String getProperty() {
66          if ( property.getLabel( null ) != null ) {
67              return property.getLabel( null );
68          } else if ( property.getLocalName() != null ) {
69              return property.getLocalName();
70          } else {
71              return property.toString();
72          }
73      }
74  
75      @Override
76      public String getContents() {
77          if ( this.object == null ) {
78              return null;
79          } else if ( object.isResource() ) {
80              // need a level of indirection...
81              Resource r = ( Resource ) object;
82              Statement s = r.getProperty( RDFS.label );
83              if ( s != null ) {
84                  return s.getObject().toString();
85              } else {
86                  return null;
87              }
88          } else {
89              return JenaUtils.asString( object );
90          }
91      }
92  
93      @Override
94      public boolean isObsolete() {
95          return super.isObsolete() || property.hasSuperProperty( OBO.ObsoleteProperty, false );
96      }
97  
98      @Override
99      public int hashCode() {
100         final int PRIME = 31;
101         int result = 1;
102         result = PRIME * result + ( ( object == null ) ? 0 : object.hashCode() );
103         result = PRIME * result + ( ( property == null ) ? 0 : property.hashCode() );
104         return result;
105     }
106 
107     @Override
108     public String toString() {
109         return property.getLocalName() + " " + object;
110     }
111 
112 }