001 /*
002 * Java Genetic Algorithm Library (jenetics-3.9.0).
003 * Copyright (c) 2007-2017 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.prog.op;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.util.Objects;
025
026 /**
027 * Represents an operation which always returns the same, constant, value. To
028 * improve readability, constants may have a name. If a name is given, this name
029 * is used when printing the program tree.
030 *
031 * <pre>{@code
032 * final static Op<Double> PI = Const.of("π", Math.PI);
033 * final static Op<Double> ONE = Const.of(1.0);
034 * }</pre>
035 *
036 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
037 * @version 3.9
038 * @since 3.9
039 */
040 public final class Const<T> implements Op<T> {
041
042 private String _name;
043 private final T _const;
044
045 private Const(final String name, final T constant) {
046 _name = name;
047 _const = constant;
048 }
049
050 @Override
051 public T apply(final T[] value) {
052 return _const;
053 }
054
055 @Override
056 public String name() {
057 return _name;
058 }
059
060 @Override
061 public int arity() {
062 return 0;
063 }
064
065 @Override
066 public int hashCode() {
067 int hash = 17;
068 hash += 31*Objects.hashCode(_name) + 37;
069 hash += 31*Objects.hashCode(_const) + 37;
070 return hash;
071 }
072
073 @Override
074 public boolean equals(final Object obj) {
075 return obj instanceof Const<?> &&
076 Objects.equals(((Const)obj)._name, _name) &&
077 Objects.equals(((Const)obj)._const, _const);
078 }
079
080 @Override
081 public String toString() {
082 return _name != null ? _name : Objects.toString(_const);
083 }
084
085 /**
086 * Return a new constant with the given name and value.
087 *
088 * @param name the constant name
089 * @param value the constant value
090 * @param <T> the constant type
091 * @return a new constant
092 * @throws NullPointerException if the given constant {@code name} is
093 * {@code null}
094 */
095 public static <T> Const<T> of(final String name, final T value) {
096 return new Const<>(requireNonNull(name), value);
097 }
098
099 /**
100 * Return a new constant with the given value.
101 *
102 * @param value the constant value
103 * @param <T> the constant type
104 * @return a new constant
105 */
106 public static <T> Const<T> of(final T value) {
107 return new Const<>(null, value);
108 }
109
110 }
|