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 String contents;
37  
38      private final com.hp.hpl.jena.ontology.AnnotationProperty property;
39  
40      /**
41       * @param prop   property for the statement
42       * @param source ontology this relates to.
43       * @param object of the statement
44       */
45      public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop, @Nullable RDFNode object ) {
46          super( prop );
47          this.property = prop;
48  
49          if ( object == null ) {
50              this.contents = null;
51          } else if ( object.isResource() ) {
52              // need a level of indirection...
53              Resource r = ( Resource ) object;
54              Statement s = r.getProperty( RDFS.label );
55              if ( s != null ) {
56                  this.contents = s.getObject().toString();
57              } else {
58                  this.contents = null;
59              }
60          } else {
61              this.contents = JenaUtils.asString( object );
62          }
63  
64      }
65  
66      @Override
67      public boolean equals( @Nullable Object obj ) {
68          if ( this == obj ) return true;
69          if ( obj == null ) return false;
70          if ( getClass() != obj.getClass() ) return false;
71          final AnnotationPropertyImpl/basecode/ontology/jena/AnnotationPropertyImpl.html#AnnotationPropertyImpl">AnnotationPropertyImpl other = ( AnnotationPropertyImpl ) obj;
72          if ( contents == null ) {
73              if ( other.contents != null ) return false;
74          } else if ( !contents.equals( other.contents ) ) return false;
75          if ( property == null ) {
76              if ( other.property != null ) return false;
77          } else if ( !property.equals( other.property ) ) return false;
78          return true;
79      }
80  
81      @Override
82      public String getUri() {
83          return property.getURI();
84      }
85  
86      @Override
87      public String getContents() {
88          return contents;
89      }
90  
91      @Override
92      public String getProperty() {
93          if ( property.getLabel( null ) != null ) {
94              return property.getLabel( null );
95          } else if ( property.getLocalName() != null ) {
96              return property.getLocalName();
97          } else {
98              return property.toString();
99          }
100     }
101 
102     @Override
103     public boolean isObsolete() {
104         return super.isObsolete() || property.hasSuperProperty( OBO.ObsoleteProperty, false );
105     }
106 
107     @Override
108     public int hashCode() {
109         final int PRIME = 31;
110         int result = 1;
111         result = PRIME * result + ( ( contents == null ) ? 0 : contents.hashCode() );
112         result = PRIME * result + ( ( property == null ) ? 0 : property.hashCode() );
113         return result;
114     }
115 
116     @Override
117     public String toString() {
118         return property.getLocalName() + " " + contents;
119     }
120 
121 }