/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.rdf.simple;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.stream.Stream;

import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.commons.rdf.api.BlankNode;
import org.apache.commons.rdf.api.IRI;

public class TestWritingGraph {

	/*
	 * 200k triples should do - about 7 MB on disk. Override with
	 * -Dtriples=20000000 to exercise your memory banks.
	 */
	private static final int TRIPLES = Integer.getInteger("triples", 200000);

	/** Run tests with -Dkeepfiles=true to inspect /tmp files **/
	private static boolean KEEP_FILES = Boolean.getBoolean("keepfiles");

	private static GraphImpl graph;

	@BeforeClass
	public static void createGraph() throws Exception {
		graph = new GraphImpl();
		BlankNode subject = new BlankNodeImpl(Optional.of(graph), "subj");
		IRI predicate = new IRIImpl("pred");
		for (int i = 0; i < TRIPLES; i++) {
			graph.add(subject, predicate, new LiteralImpl("Example " + i, "en"));
		}
	}

	@Test
	public void createGraphTiming() throws Exception {
		createGraph();
	}
	
	@Test
	public void countQuery() {
		BlankNode subject = new BlankNodeImpl(Optional.of(graph), "subj");
		IRI predicate = new IRIImpl("pred");
		long count = graph.getTriples(subject, predicate, null).unordered()
				.parallel().count();
		System.out.println("Counted - " + count);
	}

	@Test
	public void writeGraphFromStream() throws Exception {
		Path graphFile = Files.createTempFile("graph", ".nt");
		if (KEEP_FILES) {
			System.out.println("From stream: " + graphFile);
		} else {
			graphFile.toFile().deleteOnExit();
		}
		
		Stream<CharSequence> stream = graph.getTriples().unordered()
				.map(Object::toString);
		Files.write(graphFile, stream::iterator, Charset.forName("UTF-8"));
	}

	@Test
	public void writeGraphFromStreamFiltered() throws Exception {
		Path graphFile = Files.createTempFile("graph", ".nt");
		if (KEEP_FILES) {
			System.out.println("Filtered stream: " + graphFile);
		} else {
			graphFile.toFile().deleteOnExit();
		}

		BlankNode subject = new BlankNodeImpl(Optional.of(graph), "subj");
		IRI predicate = new IRIImpl("pred");
		Stream<CharSequence> stream = graph
				.getTriples(subject, predicate, null).map(Object::toString);
		Files.write(graphFile, stream::iterator, Charset.forName("UTF-8"));

	}

}

@@ -926,16 +926,97 @@
 e.Path;%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A
 import j
@@ -1067,16 +1067,45 @@
 tream;%0A%0A
+import org.junit.AfterClass;%0A
 import o
@@ -1149,17 +1149,16 @@
 t.Test;%0A
-%0A
 import o
@@ -1839,16 +1839,234 @@
 pred%22);%0A
+%09%09List%3CIRI%3E types = new ArrayList%3C%3E(Types.values());%0A%09%09// Ensure we don't try to create a literal with rdf:langString but%0A%09%09// without a language tag%0A%09%09types.remove(Types.RDF_LANGSTRING);%0A%09%09Collections.shuffle(types);%0A
 %09%09for (i
@@ -2095,16 +2095,38 @@
  i++) %7B%0A
+%09%09%09if (i %25 5 == 0) %7B%0A%09
 %09%09%09graph
@@ -2185,20 +2185,348 @@
 + i,
- %22en%22));%0A%09%09%7D
+%0A%09%09%09%09%09%09%22en%22));%0A%09%09%09%7D else if (i %25 3 == 0) %7B%0A%09%09%09%09graph.add(subject, predicate, new LiteralImpl(%22Example %22 + i,%0A%09%09%09%09%09%09types.get(i %25 types.size())));%0A%09%09%09%7D else %7B%0A%09%09%09%09graph.add(subject, predicate, new LiteralImpl(%22Example %22 + i));%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09@AfterClass%0A%09public static void tearDownClass() throws Exception %7B%0A%09%09graph.clear();%0A%09%09graph = null;
 %0A%09%7D%0A
@@ -2605,17 +2605,16 @@
 h();%0A%09%7D%0A
-%09
 %0A%09@Test%0A
@@ -3136,18 +3136,16 @@
 ();%0A%09%09%7D%0A
-%09%09
 %0A%09%09Strea

Test graph without homogeneous language literals, use datatyped literals or xsd:string also