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