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