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