001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.impl;
007:
008: import java.util.Collections;
009: import java.util.LinkedHashSet;
010: import java.util.Set;
011:
012: import org.openrdf.model.URI;
013: import org.openrdf.query.Dataset;
014:
015: /**
016: * @author Arjohn Kampman
017: */
018: public class DatasetImpl implements Dataset {
019:
020: protected Set<URI> defaultGraphs = new LinkedHashSet<URI>();
021:
022: protected Set<URI> namedGraphs = new LinkedHashSet<URI>();
023:
024: public DatasetImpl() {
025: }
026:
027: public Set<URI> getDefaultGraphs() {
028: return Collections.unmodifiableSet(defaultGraphs);
029: }
030:
031: /**
032: * Adds a graph URI to the set of default graph URIs.
033: */
034: public void addDefaultGraph(URI graphURI) {
035: defaultGraphs.add(graphURI);
036: }
037:
038: /**
039: * Removes a graph URI from the set of default graph URIs.
040: *
041: * @return <tt>true</tt> if the URI was removed from the set,
042: * <tt>false</tt> if the set did not contain the URI.
043: */
044: public boolean removeDefaultGraph(URI graphURI) {
045: return defaultGraphs.remove(graphURI);
046: }
047:
048: /**
049: * Gets the (unmodifiable) set of named graph URIs.
050: */
051: public Set<URI> getNamedGraphs() {
052: return Collections.unmodifiableSet(namedGraphs);
053: }
054:
055: /**
056: * Adds a graph URI to the set of named graph URIs.
057: */
058: public void addNamedGraph(URI graphURI) {
059: namedGraphs.add(graphURI);
060: }
061:
062: /**
063: * Removes a graph URI from the set of named graph URIs.
064: *
065: * @return <tt>true</tt> if the URI was removed from the set,
066: * <tt>false</tt> if the set did not contain the URI.
067: */
068: public boolean removeNamedGraph(URI graphURI) {
069: return namedGraphs.remove(graphURI);
070: }
071:
072: /**
073: * Removes all graph URIs (both default and named) from this dataset.
074: */
075: public void clear() {
076: defaultGraphs.clear();
077: namedGraphs.clear();
078: }
079:
080: @Override
081: public String toString() {
082: if (defaultGraphs.isEmpty() && namedGraphs.isEmpty())
083: return "## empty dataset ##";
084: StringBuilder sb = new StringBuilder();
085: for (URI uri : defaultGraphs) {
086: sb.append("FROM ");
087: appendURI(sb, uri);
088: }
089: for (URI uri : namedGraphs) {
090: sb.append("FROM NAMED ");
091: appendURI(sb, uri);
092: }
093: return sb.toString();
094: }
095:
096: private void appendURI(StringBuilder sb, URI uri) {
097: String str = uri.toString();
098: if (str.length() > 50) {
099: sb.append("<").append(str, 0, 19).append("..");
100: sb.append(str, str.length() - 29, str.length()).append(
101: ">\n");
102: } else {
103: sb.append("<").append(uri).append(">\n");
104: }
105: }
106: }
|