Const.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.3.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.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.
032  *
033  <pre>{@code
034  * final static Op<Double> PI = Const.of("π", Math.PI);
035  * final static Op<Double> ONE = Const.of(1.0);
036  * }</pre>
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
039  @version 4.1
040  @since 3.9
041  */
042 public final class Const<T> implements Op<T>, Serializable {
043 
044     private static final long serialVersionUID = 1L;
045 
046     private final String _name;
047     private final T _const;
048 
049     private Const(final String name, final T constant) {
050         _name = name;
051         _const = constant;
052     }
053 
054     @Override
055     public T apply(final T[] value) {
056         return _const;
057     }
058 
059     /**
060      * Return the constant value.
061      *
062      @since 4.1
063      *
064      @return the constant value
065      */
066     public T value() {
067         return _const;
068     }
069 
070     @Override
071     public String name() {
072         return _name;
073     }
074 
075     @Override
076     public int arity() {
077         return 0;
078     }
079 
080     @Override
081     public int hashCode() {
082         return hash(_name, hash(_const));
083     }
084 
085     @Override
086     public boolean equals(final Object obj) {
087         return obj == this ||
088             obj instanceof Const &&
089             Objects.equals(((Const)obj)._name, _name&&
090             Objects.equals(((Const)obj)._const, _const);
091     }
092 
093     @Override
094     public String toString() {
095         return _name != null ? _name : Objects.toString(_const);
096     }
097 
098     /**
099      * Return a new constant with the given name and value.
100      *
101      @param name the constant name
102      @param value the constant value
103      @param <T> the constant type
104      @return a new constant
105      @throws NullPointerException if the given constant {@code name} is
106      *        {@code null}
107      */
108     public static <T> Const<T> of(final String name, final T value) {
109         return new Const<>(requireNonNull(name), value);
110     }
111 
112     /**
113      * Return a new constant with the given value.
114      *
115      @param value the constant value
116      @param <T> the constant type
117      @return a new constant
118      */
119     public static <T> Const<T> of(final T value) {
120         return new Const<>(null, value);
121     }
122 
123 }