/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.json.stream;

import static java.util.Objects.requireNonNull;

import java.io.IOException;

import com.google.gson.stream.JsonWriter;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
@FunctionalInterface
public interface Writer<T> {

	public void write(final T value, final JsonWriter json) throws IOException;


	public static <T> Writer<T> obj(final String name, final Writer<? super T>... children) {
		requireNonNull(name);
		requireNonNull(children);

		return (data, json) -> {
			if (data != null) {
				json.beginObject();
				json.name(name);
				for (Writer<? super T> child : children) {
					child.write(data, json);
				}
				json.endObject();
			}
		};
	}

}

@@ -845,16 +845,52 @@
 ception;
+%0Aimport java.util.function.Function;
 %0A%0Aimport
@@ -1195,16 +1195,258 @@
 ption;%0A%0A
+%09public default %3CB%3E Writer%3CB%3E map(final Function%3C? super B, ? extends T%3E mapper) %7B%0A%09%09return (data, json) -%3E %7B%0A%09%09%09if (data != null) %7B%0A%09%09%09%09final T value = mapper.apply(data);%0A%09%09%09%09if (value != null) %7B%0A%09%09%09%09%09write(value, json);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D%0A
 %0A%09public
@@ -1782,16 +1782,16 @@
 ject();%0A
-
 %09%09%09%7D%0A%09%09%7D
@@ -1796,10 +1796,158 @@
 %09%7D;%0A%09%7D%0A%0A
+%09public static Writer%3CInteger%3E value(final String name, final Integer value) %7B%0A%09%09return (data, json) -%3E %7B%0A%09%09%09json.name(name).value(value);%0A%09%09%7D;%0A%09%7D%0A%0A
 %7D%0A

Implement Writer.