01: /*
02: * LICENSE INFORMATION
03: * Copyright 2005-2007 by FZI (http://www.fzi.de).
04: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
05: * <OWNER> = Max Völkel
06: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
07: * <YEAR> = 2007
08: *
09: * Project information at http://semweb4j.org/rdf2go
10: */
11: package org.ontoware.rdf2go.model.impl;
12:
13: import java.util.HashSet;
14: import java.util.Iterator;
15: import java.util.Set;
16:
17: import org.ontoware.aifbcommons.collection.ClosableIterator;
18: import org.ontoware.rdf2go.exception.LockException;
19: import org.ontoware.rdf2go.exception.ModelRuntimeException;
20: import org.ontoware.rdf2go.model.Diff;
21: import org.ontoware.rdf2go.model.ModelAddRemove;
22: import org.ontoware.rdf2go.model.Statement;
23: import org.ontoware.rdf2go.model.node.Node;
24: import org.ontoware.rdf2go.model.node.Resource;
25: import org.ontoware.rdf2go.model.node.URI;
26:
27: public class ModelAddRemoveMemoryImpl extends AbstractModelAddRemove
28: implements ModelAddRemove {
29:
30: protected Set<Statement> set = new HashSet<Statement>();
31:
32: @Override
33: public void addStatement(Resource subject, URI predicate,
34: Node object) throws ModelRuntimeException {
35: Statement s = new StatementImpl(null, subject, predicate,
36: object);
37: this .set.add(s);
38: }
39:
40: @Override
41: public void removeStatement(Resource subject, URI predicate,
42: Node object) throws ModelRuntimeException {
43: Statement s = new StatementImpl(null, subject, predicate,
44: object);
45: this .set.remove(s);
46: }
47:
48: public Set<Statement> getSet() {
49: return this .set;
50: }
51:
52: public void lock() throws LockException {
53: throw new UnsupportedOperationException();
54: }
55:
56: public boolean isLocked() {
57: throw new UnsupportedOperationException();
58: }
59:
60: public void unlock() {
61: throw new UnsupportedOperationException();
62: }
63:
64: public Diff getDiff(Iterator<? extends Statement> statements)
65: throws ModelRuntimeException {
66: ModelAddRemoveMemoryImpl other = new ModelAddRemoveMemoryImpl();
67: other.addAll(statements);
68:
69: ModelAddRemoveMemoryImpl add = new ModelAddRemoveMemoryImpl();
70: add.addAll(other.iterator());
71: add.removeAll(this .iterator());
72:
73: ModelAddRemoveMemoryImpl removed = new ModelAddRemoveMemoryImpl();
74: removed.addAll(this .iterator());
75: removed.removeAll(other.iterator());
76:
77: return new DiffImpl(add.iterator(), removed.iterator());
78: }
79:
80: public ClosableIterator<Statement> iterator() {
81: Iterator<Statement> it = this .set.iterator();
82: assert it != null;
83: return new PseudoClosableIterator<Statement>(it);
84: }
85:
86: public long size() {
87: return this.set.size();
88: }
89:
90: }
|