001 /*
002 * Java Genetic Algorithm Library (jenetics-4.2.0).
003 * Copyright (c) 2007-2018 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@gmail.com)
019 */
020 package io.jenetics.prog.op;
021
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024
025 import java.io.Serializable;
026 import java.util.Objects;
027 import java.util.function.Supplier;
028
029 import io.jenetics.internal.util.Lazy;
030
031 /**
032 * Implementation of an <em>ephemeral</em> constant. It causes the insertion of
033 * a <em>mutable</em> constant into the operation tree. Every time this terminal
034 * is chosen a, different value is generated which is then used for that
035 * particular terminal, and which will remain fixed for the given tree. The main
036 * usage would be to introduce random terminal values.
037 *
038 * <pre>{@code
039 * final Random random = ...;
040 * final Op<Double> val = EphemeralConst.of(random::nextDouble());
041 * }</pre>
042 *
043 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
044 * @version 4.1
045 * @since 3.9
046 */
047 public final class EphemeralConst<T> implements Op<T>, Serializable {
048
049 private static final long serialVersionUID = 1L;
050
051 private final String _name;
052 private final Supplier<T> _supplier;
053 private final Lazy<T> _value;
054
055 private EphemeralConst(final String name, final Supplier<T> supplier) {
056 _name = name;
057 _supplier = requireNonNull(supplier);
058 _value = Lazy.of(_supplier);
059 }
060
061 @Override
062 public String name() {
063 return _name;
064 }
065
066 @Override
067 public int arity() {
068 return 0;
069 }
070
071 /**
072 * Return a newly created, uninitialized constant of type {@code T}.
073 *
074 * @return a newly created, uninitialized constant of type {@code T}
075 */
076 @Override
077 public Op<T> get() {
078 return new EphemeralConst<>(_name, _supplier);
079 }
080
081 @Override
082 public T apply(final T[] ts) {
083 return _value.get();
084 }
085
086 @Override
087 public int hashCode() {
088 int hash = 17;
089 hash += 31*Objects.hashCode(_name) + 37;
090 hash += 31*Objects.hashCode(_value) + 37;
091 return hash;
092 }
093
094 @Override
095 public boolean equals(final Object obj) {
096 return obj == this ||
097 obj instanceof EphemeralConst &&
098 Objects.equals(((EphemeralConst)obj)._name, _name) &&
099 Objects.equals(((EphemeralConst)obj)._value, _value);
100 }
101
102 @Override
103 public String toString() {
104 return _name != null
105 ? format("%s(%s)", _name, _value.get())
106 : Objects.toString(_value.get());
107 }
108
109 /**
110 * Create a new ephemeral constant with the given {@code name} and value
111 * {@code supplier}. For every newly created operation tree, a new constant
112 * value is chosen for this terminal operation. The value is than kept
113 * constant for this tree.
114 *
115 * @param name the name of the ephemeral constant
116 * @param supplier the value supplier
117 * @param <T> the constant type
118 * @return a new ephemeral constant
119 * @throws NullPointerException if one of the arguments is {@code null}
120 */
121 public static <T> EphemeralConst<T> of(
122 final String name,
123 final Supplier<T> supplier
124 ) {
125 return new EphemeralConst<>(requireNonNull(name), supplier);
126 }
127
128 /**
129 * Create a new ephemeral constant with the given value {@code supplier}.
130 * For every newly created operation tree, a new constant value is chosen
131 * for this terminal operation. The value is than kept constant for this tree.
132 *
133 * @param supplier the value supplier
134 * @param <T> the constant type
135 * @return a new ephemeral constant
136 * @throws NullPointerException if the {@code supplier} is {@code null}
137 */
138 public static <T> EphemeralConst<T> of(final Supplier<T> supplier) {
139 return new EphemeralConst<>(null, supplier);
140 }
141
142 }
|