Const.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.4.0).
003  * Copyright (c) 2007-2019 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.util.Objects.requireNonNull;
023 import static io.jenetics.internal.util.Hashes.hash;
024 
025 import java.io.Serializable;
026 import java.util.Objects;
027 
028 /**
029  * Represents an operation which always returns the same, constant, value. To
030  * improve readability, constants may have a name. If a name is given, this name
031  * is used when printing the program tree. The {@code Const} operation is a
032  <em>terminal</em> operation.
033  *
034  <pre>{@code
035  * final static Op<Double> PI = Const.of("π", Math.PI);
036  * final static Op<Double> ONE = Const.of(1.0);
037  * }</pre>
038  *
039  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
040  @version 4.1
041  @since 3.9
042  */
043 public final class Const<T> implements Op<T>, Serializable {
044 
045     private static final long serialVersionUID = 1L;
046 
047     private final String _name;
048     private final T _const;
049 
050     private Const(final String name, final T constant) {
051         _name = name;
052         _const = constant;
053     }
054 
055     @Override
056     public T apply(final T[] value) {
057         return _const;
058     }
059 
060     /**
061      * Return the constant value.
062      *
063      @since 4.1
064      *
065      @return the constant value
066      */
067     public T value() {
068         return _const;
069     }
070 
071     @Override
072     public String name() {
073         return _name;
074     }
075 
076     @Override
077     public int arity() {
078         return 0;
079     }
080 
081     @Override
082     public int hashCode() {
083         return hash(_name, hash(_const));
084     }
085 
086     @Override
087     public boolean equals(final Object obj) {
088         return obj == this ||
089             obj instanceof Const &&
090             Objects.equals(((Const)obj)._name, _name&&
091             equal(((Const)obj)._const, _const);
092     }
093 
094     private static boolean equal(final Object a, final Object b) {
095         if (instanceof Double && b instanceof Double) {
096             return ((Double)a).doubleValue() == ((Double)b).doubleValue();
097         else if (instanceof Float && b instanceof Float) {
098             return ((Float)a).floatValue() == ((Float)b).floatValue();
099         }
100 
101         return Objects.equals(a, b);
102     }
103 
104     @Override
105     public String toString() {
106         return _name != null ? _name : Objects.toString(_const);
107     }
108 
109     /**
110      * Return a new constant with the given name and value.
111      *
112      @param name the constant name
113      @param value the constant value
114      @param <T> the constant type
115      @return a new constant
116      @throws NullPointerException if the given constant {@code name} is
117      *        {@code null}
118      */
119     public static <T> Const<T> of(final String name, final T value) {
120         return new Const<>(requireNonNull(name), value);
121     }
122 
123     /**
124      * Return a new constant with the given value.
125      *
126      @param value the constant value
127      @param <T> the constant type
128      @return a new constant
129      */
130     public static <T> Const<T> of(final T value) {
131         return new Const<>(null, value);
132     }
133 
134 }