01: package org.ontoware.rdf2go.model;
02:
03: import java.util.Iterator;
04:
05: import org.ontoware.aifbcommons.collection.ClosableIterable;
06: import org.ontoware.aifbcommons.collection.ClosableIterator;
07: import org.ontoware.rdf2go.exception.ModelRuntimeException;
08:
09: /** Utils for test, not testing utils */
10: public class TestUtils {
11:
12: /**
13: * Copy data from the source modelset to the target modelset. Iterates
14: * through all named models of the source and adds each to the target. If a
15: * named graph already exists in the target, the data will be added to it,
16: * target models will not be replaced.
17: *
18: * @param source
19: * the source, data from here is taken
20: * @param target
21: * the target, data will be put here.
22: * @throws ModelRuntimeException
23: * if the copying process has an error
24: */
25: public static void copy(ModelSet source, ModelSet target)
26: throws ModelRuntimeException {
27: for (Iterator<? extends Model> i = source.getModels(); i
28: .hasNext();) {
29: Model m = i.next();
30: m.open();
31: Model tm = target.getModel(m.getContextURI());
32: tm.open();
33: tm.addAll(m.iterator());
34: }
35: // copy default model
36: Model m = source.getDefaultModel();
37: m.open();
38: Model tm = target.getDefaultModel();
39: tm.open();
40: tm.addAll(m.iterator());
41: }
42:
43: /**
44: * Runs through the iterator of the iterable, closes it and returns number of items.
45: *
46: * @author clemente
47: *
48: * @param it Any type of iterator, not null
49: * @returns Number of items
50: */
51: public static int countAndClose(ClosableIterable<?> iterable) {
52: if (iterable == null)
53: throw new IllegalArgumentException(
54: "Cannot count a null-iterable");
55: ClosableIterator<?> it = iterable.iterator();
56: return countAndClose(it);
57: }
58:
59: /**
60: * Runs through the iterator of the iterable, closes it and returns number of items.
61: *
62: * @author clemente
63: *
64: * @param it Any type of iterator, not null
65: * @returns Number of items
66: */
67: public static int countAndClose(ClosableIterator<?> it) {
68: if (it == null)
69: throw new IllegalArgumentException(
70: "Cannot count a null-iterable");
71: int count = 0;
72: while (it.hasNext()) {
73: count++;
74: it.next();
75: }
76: it.close();
77: return count;
78: }
79:
80: }
|