View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.ordi.tripleset.impl;
19  
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.Set;
24  
25  import org.openrdf.model.Resource;
26  import org.openrdf.model.Statement;
27  import org.openrdf.model.URI;
28  import org.openrdf.model.Value;
29  import org.openrdf.model.impl.ContextStatementImpl;
30  import org.openrdf.model.impl.URIImpl;
31  
32  import com.ontotext.ordi.tripleset.ORDIConst;
33  import com.ontotext.ordi.tripleset.TStatement;
34  
35  public class TStatementImpl extends ContextStatementImpl implements TStatement {
36  
37      static final long serialVersionUID = 0; // choose another value;
38      static final URI IMPLICIT_STATEMENT = new URIImpl(ORDIConst.IMPLICIT_STATEMENT); 
39  
40      protected final Set<URI> tripleSets;
41  
42      public TStatementImpl(Resource subject, URI predicate, Value object,
43              Resource context, URI... ts) {
44          super(subject, predicate, object, context);
45          if (ts == null) {
46              throw new IllegalArgumentException();
47          }
48          tripleSets = new HashSet<URI>();
49          Collections.addAll(tripleSets, ts);
50      }
51  
52      public TStatementImpl(Resource subject, URI predicate, Value object,
53              Resource context, Set<URI> ts) {
54          super(subject, predicate, object, context);
55          if (ts == null) {
56              throw new IllegalArgumentException();
57          }
58          this.tripleSets = ts;
59      }
60  
61      public TStatementImpl(Statement statement, Resource context, URI... ts) {
62          this(statement.getSubject(), statement.getPredicate(), statement
63                  .getObject(), statement.getContext(), ts);
64      }
65  
66      public TStatementImpl(Statement statement, Resource context, Set<URI> ts) {
67          this(statement.getSubject(), statement.getPredicate(), statement
68                  .getObject(), statement.getContext(), ts);
69      }
70  
71      public Iterator<URI> getTriplesetIterator() {
72          return tripleSets.iterator();
73      }
74  
75      public boolean isMemberOf(URI uri) {
76          if (uri.stringValue().equals(ORDIConst.EXPLICIT_STATEMENT))
77              return tripleSets.contains(IMPLICIT_STATEMENT) == false; 
78          return tripleSets.contains(uri);
79      }
80  
81      // Overrides Object.equals(Object), implements Statement.equals(Object)
82      public boolean equals(Object other) {
83          if (this == other) {
84              return true;
85          }
86          if (other instanceof Statement) {
87              Statement otherSt = (Statement) other;
88              return getObject().equals(otherSt.getObject())
89                      && getSubject().equals(otherSt.getSubject())
90                      && getContext().equals(otherSt.getContext());
91          }
92  
93          return false;
94      }
95  
96      public int hashCode() {
97          return 923521 * getContext().hashCode() + 961 * getSubject().hashCode()
98                  + 31 * getPredicate().hashCode() + getObject().hashCode();
99      }
100 }