FinalReference.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 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.util;
021 
022 import static org.jenetics.internal.util.object.eq;
023 
024 import java.io.Serializable;
025 import java.util.Objects;
026 
027 import javolution.lang.Reference;
028 
029 import org.jenetics.internal.util.HashBuilder;
030 
031 /**
032  * A final {@link Reference}. This class is used if you want to allow to set the
033  * value of a {@link Reference} only once. If you try to set the references
034  * value twice an {@link IllegalStateException} is thrown.
035  *
036  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
037  @since 1.0
038  @version 1.0 &mdash; <em>$Date: 2014-03-01 $</em>
039  */
040 public final class FinalReference<T> implements Reference<T>, Serializable {
041     private static final long serialVersionUID = 1L;
042 
043     private T _value = null;
044     private boolean _initialized = false;
045 
046     /**
047      * Create a new final reference.
048      */
049     public FinalReference() {
050     }
051 
052     /**
053      * Create a new FinalReference with the given default value. The value of
054      * this reference can still be set, that means {@code isFinal() == false}.
055      *
056      @param devault the default value of the reference.
057      */
058     public FinalReference(final T devault) {
059         _value = devault;
060     }
061 
062     /**
063      * Test whether this {@link Reference} can be set without throwing an
064      {@link IllegalStateException} or not.
065      *
066      @return {@code true} if this {@link Reference} can't be set again,
067      *         false otherwise.
068      */
069     public synchronized boolean isFinal() {
070         return _initialized;
071     }
072 
073     /**
074      * Set the reference value. If you try to set the reference value twice an
075      {@link IllegalStateException} is thrown.
076      *
077      @throws IllegalStateException if you try to set the reference value twice.
078      */
079     @Override
080     public synchronized void set(final T value) {
081         if (_initialized) {
082             throw new IllegalStateException("Value is already initialized.");
083         }
084         _value = value;
085         _initialized = true;
086     }
087 
088     @Override
089     public synchronized T get() {
090         return _value;
091     }
092 
093     @Override
094     public int hashCode() {
095         return HashBuilder.of(getClass()).and(get()).value();
096     }
097 
098     @Override
099     public boolean equals(final Object object) {
100         if (object == this) {
101             return true;
102         }
103         if (!(object instanceof FinalReference<?>)) {
104             return false;
105         }
106 
107         final FinalReference<?> f = (FinalReference<?>)object;
108         return eq(get(), f.get());
109     }
110 
111     @Override
112     public String toString() {
113         return Objects.toString(get());
114     }
115 
116 }