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