001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.model.impl;
007:
008: import org.openrdf.model.Resource;
009: import org.openrdf.model.Statement;
010: import org.openrdf.model.URI;
011: import org.openrdf.model.Value;
012:
013: /**
014: * An implementation of the {@link Statement} interface for statements that
015: * don't have an associated context. For statements that do have an associated
016: * context, {@link ContextStatementImpl} can be used.
017: */
018: public class StatementImpl implements Statement {
019:
020: /*-----------*
021: * Constants *
022: *-----------*/
023:
024: private static final long serialVersionUID = 8707542157460228077L;
025:
026: /**
027: * The statement's subject.
028: */
029: private final Resource subject;
030:
031: /**
032: * The statement's predicate.
033: */
034: private final URI predicate;
035:
036: /**
037: * The statement's object.
038: */
039: private final Value object;
040:
041: /*--------------*
042: * Constructors *
043: *--------------*/
044:
045: /**
046: * Creates a new Statement with the supplied subject, predicate and object.
047: *
048: * @param subject
049: * The statement's subject, must not be <tt>null</tt>.
050: * @param predicate
051: * The statement's predicate, must not be <tt>null</tt>.
052: * @param object
053: * The statement's object, must not be <tt>null</tt>.
054: */
055: public StatementImpl(Resource subject, URI predicate, Value object) {
056: assert (subject != null);
057: assert (predicate != null);
058: assert (object != null);
059:
060: this .subject = subject;
061: this .predicate = predicate;
062: this .object = object;
063: }
064:
065: /*---------*
066: * Methods *
067: *---------*/
068:
069: // Implements Statement.getSubject()
070: public Resource getSubject() {
071: return subject;
072: }
073:
074: // Implements Statement.getPredicate()
075: public URI getPredicate() {
076: return predicate;
077: }
078:
079: // Implements Statement.getObject()
080: public Value getObject() {
081: return object;
082: }
083:
084: // Implements Statement.getContext()
085: public Resource getContext() {
086: return null;
087: }
088:
089: // Overrides Object.equals(Object), implements Statement.equals(Object)
090: @Override
091: public boolean equals(Object other) {
092: if (this == other) {
093: return true;
094: }
095:
096: if (other instanceof Statement) {
097: Statement otherSt = (Statement) other;
098:
099: // The object is potentially the cheapest to check, as types
100: // of these references might be different.
101:
102: // In general the number of different predicates in sets of
103: // statements is the smallest, so predicate equality is checked
104: // last.
105: return object.equals(otherSt.getObject())
106: && subject.equals(otherSt.getSubject())
107: && predicate.equals(otherSt.getPredicate());
108: }
109:
110: return false;
111: }
112:
113: // Overrides Object.hashCode(), implements Statement.hashCode()
114: @Override
115: public int hashCode() {
116: return 961 * subject.hashCode() + 31 * predicate.hashCode()
117: + object.hashCode();
118: }
119:
120: /**
121: * Gives a String-representation of this Statement that can be used for
122: * debugging.
123: */
124: @Override
125: public String toString() {
126: StringBuilder sb = new StringBuilder(256);
127:
128: sb.append("(");
129: sb.append(getSubject());
130: sb.append(", ");
131: sb.append(getPredicate());
132: sb.append(", ");
133: sb.append(getObject());
134: sb.append(")");
135:
136: return sb.toString();
137: }
138: }
|