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