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