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