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.sail.memory.model;
007:
008: import org.openrdf.model.URI;
009:
010: /**
011: * A MemoryStore-specific implementation of URI that stores separated namespace
012: * and local name information to enable reuse of namespace String objects
013: * (reducing memory usage) and that gives it node properties.
014: */
015: public class MemURI implements URI, MemResource {
016:
017: /*-----------*
018: * Variables *
019: *-----------*/
020:
021: /**
022: *
023: */
024: private static final long serialVersionUID = 9118488004995852467L;
025:
026: /**
027: * The URI's namespace.
028: */
029: private final String namespace;
030:
031: /**
032: * The URI's local name.
033: */
034: private final String localName;
035:
036: /**
037: * The object that created this MemURI.
038: */
039: transient private Object creator;
040:
041: /**
042: * The MemURI's hash code, 0 if not yet initialized.
043: */
044: private int hashCode = 0;
045:
046: /**
047: * The list of statements for which this MemURI is the subject.
048: */
049: transient private MemStatementList subjectStatements = null;
050:
051: /**
052: * The list of statements for which this MemURI is the predicate.
053: */
054: transient private MemStatementList predicateStatements = null;
055:
056: /**
057: * The list of statements for which this MemURI is the object.
058: */
059: transient private MemStatementList objectStatements = null;
060:
061: /**
062: * The list of statements for which this MemURI represents the context.
063: */
064: transient private MemStatementList contextStatements = null;
065:
066: /*--------------*
067: * Constructors *
068: *--------------*/
069:
070: /**
071: * Creates a new MemURI for a URI.
072: *
073: * @param creator
074: * The object that is creating this MemURI.
075: * @param namespace
076: * namespace part of URI.
077: * @param localName
078: * localname part of URI.
079: */
080: public MemURI(Object creator, String namespace, String localName) {
081: this .creator = creator;
082: this .namespace = namespace;
083: this .localName = localName;
084: }
085:
086: /*---------*
087: * Methods *
088: *---------*/
089:
090: // Overrides Object.toString(), implements URI.toString()
091: @Override
092: public String toString() {
093: return namespace + localName;
094: }
095:
096: public String stringValue() {
097: return toString();
098: }
099:
100: // Implements URI.getNamespace()
101: public String getNamespace() {
102: return namespace;
103: }
104:
105: // Implements URI.getLocalName()
106: public String getLocalName() {
107: return localName;
108: }
109:
110: // Overrides Object.equals(Object), implements URI.equals(Object)
111: @Override
112: public boolean equals(Object other) {
113: if (this == other) {
114: return true;
115: }
116:
117: if (other instanceof MemURI) {
118: MemURI o = (MemURI) other;
119: return namespace.equals(o.getNamespace())
120: && localName.equals(o.getLocalName());
121: } else if (other instanceof URI) {
122: String otherStr = other.toString();
123:
124: return namespace.length() + localName.length() == otherStr
125: .length()
126: && otherStr.endsWith(localName)
127: && otherStr.startsWith(namespace);
128: }
129:
130: return false;
131: }
132:
133: // Overrides Object.hashCode(), implements URI.hashCode()
134: @Override
135: public int hashCode() {
136: if (hashCode == 0) {
137: hashCode = toString().hashCode();
138: }
139:
140: return hashCode;
141: }
142:
143: // Implements MemValue.getCreator()
144: public Object getCreator() {
145: return creator;
146: }
147:
148: // Implements MemValue.getSubjectStatementList()
149: public MemStatementList getSubjectStatementList() {
150: if (subjectStatements == null) {
151: return EMPTY_LIST;
152: } else {
153: return subjectStatements;
154: }
155: }
156:
157: // Implements MemValue.getSubjectStatementCount()
158: public int getSubjectStatementCount() {
159: if (subjectStatements == null) {
160: return 0;
161: } else {
162: return subjectStatements.size();
163: }
164: }
165:
166: // Implements MemValue.addSubjectStatement(MemStatement)
167: public void addSubjectStatement(MemStatement st) {
168: if (subjectStatements == null) {
169: subjectStatements = new MemStatementList(4);
170: }
171:
172: subjectStatements.add(st);
173: }
174:
175: // Implements MemValue.removeSubjectStatement(MemStatement)
176: public void removeSubjectStatement(MemStatement st) {
177: subjectStatements.remove(st);
178:
179: if (subjectStatements.isEmpty()) {
180: subjectStatements = null;
181: }
182: }
183:
184: /**
185: * Gets the list of statements for which this MemURI is the predicate.
186: *
187: * @return a MemStatementList containing the statements.
188: */
189: public MemStatementList getPredicateStatementList() {
190: if (predicateStatements == null) {
191: return EMPTY_LIST;
192: } else {
193: return predicateStatements;
194: }
195: }
196:
197: /**
198: * Gets the number of Statements for which this MemURI is the predicate.
199: *
200: * @return An integer larger than or equal to 0.
201: */
202: public int getPredicateStatementCount() {
203: if (predicateStatements == null) {
204: return 0;
205: } else {
206: return predicateStatements.size();
207: }
208: }
209:
210: /**
211: * Adds a statement to this MemURI's list of statements for which it is the
212: * predicate.
213: */
214: public void addPredicateStatement(MemStatement st) {
215: if (predicateStatements == null) {
216: predicateStatements = new MemStatementList(4);
217: }
218:
219: predicateStatements.add(st);
220: }
221:
222: /**
223: * Removes a statement from this MemURI's list of statements for which it is
224: * the predicate.
225: */
226: public void removePredicateStatement(MemStatement st) {
227: predicateStatements.remove(st);
228:
229: if (predicateStatements.isEmpty()) {
230: predicateStatements = null;
231: }
232: }
233:
234: // Implements MemValue.getObjectStatementList()
235: public MemStatementList getObjectStatementList() {
236: if (objectStatements == null) {
237: return EMPTY_LIST;
238: } else {
239: return objectStatements;
240: }
241: }
242:
243: // Implements MemValue.getObjectStatementCount()
244: public int getObjectStatementCount() {
245: if (objectStatements == null) {
246: return 0;
247: } else {
248: return objectStatements.size();
249: }
250: }
251:
252: // Implements MemValue.addObjectStatement(MemStatement)
253: public void addObjectStatement(MemStatement st) {
254: if (objectStatements == null) {
255: objectStatements = new MemStatementList(4);
256: }
257: objectStatements.add(st);
258: }
259:
260: // Implements MemValue.removeObjectStatement(MemStatement)
261: public void removeObjectStatement(MemStatement st) {
262: objectStatements.remove(st);
263: if (objectStatements.isEmpty()) {
264: objectStatements = null;
265: }
266: }
267:
268: // Implements MemResource.getContextStatementList()
269: public MemStatementList getContextStatementList() {
270: if (contextStatements == null) {
271: return EMPTY_LIST;
272: } else {
273: return contextStatements;
274: }
275: }
276:
277: // Implements MemResource.getContextStatementCount()
278: public int getContextStatementCount() {
279: if (contextStatements == null) {
280: return 0;
281: } else {
282: return contextStatements.size();
283: }
284: }
285:
286: // Implements MemResource.addContextStatement(MemStatement)
287: public void addContextStatement(MemStatement st) {
288: if (contextStatements == null) {
289: contextStatements = new MemStatementList(4);
290: }
291:
292: contextStatements.add(st);
293: }
294:
295: // Implements MemResource.removeContextStatement(MemStatement)
296: public void removeContextStatement(MemStatement st) {
297: contextStatements.remove(st);
298:
299: if (contextStatements.isEmpty()) {
300: contextStatements = null;
301: }
302: }
303: }
|