All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jenetics.internal.util.Lazy Maven / Gradle / Ivy

There is a newer version: 8.1.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-4.0.0).
 * Copyright (c) 2007-2017 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 ([email protected])
 */
package io.jenetics.internal.util;

import static java.lang.String.format;
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 Franz Wilhelmstötter
 * @since 3.0
 * @version 3.7
 */
public final class Lazy implements Supplier, Serializable {
	private static final long serialVersionUID = 2L;

	private final transient Supplier _supplier;

	private T _value;
	private volatile boolean _evaluated;

	private Lazy(
		final T value,
		final boolean evaluated,
		final Supplier supplier
	) {
		_value = value;
		_evaluated = evaluated;
		_supplier = supplier;
	}

	private Lazy(final Supplier supplier) {
		this(null, false, 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 format("Lazy[%s]", _evaluated ? get() : "?");
    }

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

	/**
	 * Create a new {@code Lazy} object with the given {@code value}. This
	 * method allows to create a lazy object with the given
	 * {@code value}.
	 *
	 * @since 3.7
	 *
	 * @param value the value this {@code Lazy} object is initialized with
	 * @param  the value type
	 * @return return a new lazy value with the given value
	 */
	public static  Lazy ofValue(final T value) {
		return new Lazy(value, true, null);
	}


	/**************************************************************************
	 *  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;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy