Lazy.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.0).
003  * Copyright (c) 2007-2016 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.internal.util;
021 
022 import static java.util.Objects.requireNonNull;
023 
024 import java.io.Serializable;
025 import java.util.Objects;
026 import java.util.function.Supplier;
027 
028 /**
029  * Class for lazy value initialization.
030  *
031  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
032  * @since 3.0
033  * @version 3.4
034  */
035 public final class Lazy<T> implements Supplier<T>, Serializable {
036     private static final long serialVersionUID = 1L;
037 
038     private final Supplier<T> _supplier;
039 
040     private T _value;
041     private volatile boolean _evaluated = false;
042 
043     private Lazy(final Supplier<T> supplier) {
044         _supplier = requireNonNull(supplier);
045     }
046 
047     @Override
048     public T get() {
049         return _evaluated ? _value : evaluate();
050     }
051 
052     /**
053      * Return the evaluation state of the {@code Lazy} variable.
054      *
055      * @return {@code true} is the {@code Lazy} variable has been evaluated,
056      *         {@code false} otherwise
057      */
058     public synchronized boolean isEvaluated() {
059         return _evaluated;
060     }
061 
062     private synchronized T evaluate() {
063         if (!_evaluated) {
064             _value = _supplier.get();
065             _evaluated = true;
066         }
067 
068         return _value;
069     }
070 
071     @Override
072     public int hashCode() {
073         return Objects.hashCode(get());
074     }
075 
076     @Override
077     public boolean equals(final Object obj) {
078         if (this == obj) return true;
079         if (!(obj instanceof Lazy<?>)) return false;
080 
081         final Lazy<?> lazy = (Lazy<?>)obj;
082         return Objects.equals(get(), lazy.get());
083     }
084 
085     @Override
086     public String toString() {
087         return Objects.toString(get());
088     }
089 
090     /**
091      * Create a new lazy value initialization.
092      *
093      * @param supplier the lazy value supplier
094      * @param <T> the value type
095      * @return a new lazy value initialization
096      * @throws java.lang.NullPointerException if the given supplier is
097      *         {@code null}
098      */
099     public static <T> Lazy<T> of(final Supplier<T> supplier) {
100         return new Lazy<>(supplier);
101     }
102 
103 }