/*
 * 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.internal.util;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * Class for lazy value initialization.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @since 3.0
 * @version !__version__!
 */
public final class Lazy<T> implements Supplier<T>, Serializable {
	private static final long serialVersionUID = 2L;

	private final transient Supplier<T> _supplier;

	private T _value;
	private volatile boolean _evaluated = false;

	private Lazy(final Supplier<T> supplier) {
		_supplier = requireNonNull(supplier);
	}

	@Override
	public T get() {
		return _evaluated ? _value : evaluate();
	}

	/**
	 * Return the evaluation state of the {@code Lazy} variable.
	 *
	 * @return {@code true} is the {@code Lazy} variable has been evaluated,
	 *         {@code false} otherwise
	 */
	public synchronized boolean isEvaluated() {
		return _evaluated;
	}

	private synchronized T evaluate() {
		if (!_evaluated) {
			_value = _supplier.get();
			_evaluated = true;
		}

		return _value;
	}

    @Override
    public int hashCode() {
        return Objects.hashCode(get());
    }

    @Override
    public boolean equals(final Object obj) {
		if (this == obj) return true;
		if (!(obj instanceof Lazy<?>)) return false;

		final Lazy<?> lazy = (Lazy<?>)obj;
        return Objects.equals(get(), lazy.get());
    }

    @Override
    public String toString() {
        return Objects.toString(get());
    }

	/**
	 * Create a new lazy value initialization.
	 *
	 * @param supplier the lazy value supplier
	 * @param <T> the value type
	 * @return a new lazy value initialization
	 * @throws java.lang.NullPointerException if the given supplier is
	 *         {@code null}
	 */
	public static <T> Lazy<T> of(final Supplier<T> supplier) {
		return new Lazy<>(supplier);
	}


	/**************************************************************************
	 *  Java object serialization
	 *************************************************************************/

	private void writeObject(final ObjectOutputStream out)
		throws IOException
	{
		out.defaultWriteObject();
		out.writeObject(get());
	}

	@SuppressWarnings("unchecked")
	private void readObject(final ObjectInputStream in)
		throws IOException, ClassNotFoundException
	{
		in.defaultReadObject();
		_value = (T)in.readObject();
		_evaluated = true;
	}

}

@@ -1407,16 +1407,8 @@
 ated
- = false
 ;%0A%0A%09
@@ -1420,16 +1420,63 @@
 te Lazy(
+%0A%09%09final T value,%0A%09%09final boolean evaluated,%0A%09%09
 final Su
@@ -1497,25 +1497,149 @@
 lier
+%0A%09
 ) %7B%0A%09%09_
-supplier =
+value = value;%0A%09%09_evaluated = evaluated;%0A%09%09_supplier = supplier;%0A%09%7D%0A%0A%09private Lazy(final Supplier%3CT%3E supplier) %7B%0A%09%09this(null, false,
  req
@@ -1655,24 +1655,25 @@
 ll(supplier)
+)
 ;%0A%09%7D%0A%0A%09@Over
@@ -2912,16 +2912,472 @@
 r);%0A%09%7D%0A%0A
+%09/**%0A%09 * Create a new %7B@code Lazy%7D object with the given %7B@code value%7D. This%0A%09 * method allows to create a %3Cem%3Elazy%3C/em%3E object with the given%0A%09 * %7B@code value%7D.%0A%09 *%0A%09 * @since !__version__!%0A%09 *%0A%09 * @param value the value this %7B@code Lazy%7D object is initialized with%0A%09 * @param %3CT%3E the value type%0A%09 * @return return a new lazy value with the given value%0A%09 */%0A%09public static %3CT%3E Lazy%3CT%3E ofValue(final T value) %7B%0A%09%09return new Lazy%3CT%3E(value, true, null);%0A%09%7D%0A%0A
 %0A%09/*****

Add an additional factory method for the 'Lazy' object.