Const.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.1.0).
003  * Copyright (c) 2007-2020 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.SerialIO.readNullableString;
024 import static io.jenetics.internal.util.SerialIO.writeNullableString;
025 
026 import java.io.IOException;
027 import java.io.InvalidObjectException;
028 import java.io.ObjectInput;
029 import java.io.ObjectInputStream;
030 import java.io.ObjectOutput;
031 import java.io.Serializable;
032 import java.util.Objects;
033 
034 /**
035  * Represents an operation which always returns the same, constant, value. To
036  * improve readability, constants may have a name. If a name is given, this name
037  * is used when printing the program tree. The {@code Const} operation is a
038  <em>terminal</em> operation.
039  *
040  <pre>{@code
041  * final static Op<Double> PI = Const.of("π", Math.PI);
042  * final static Op<Double> ONE = Const.of(1.0);
043  * }</pre>
044  *
045  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
046  @version 5.0
047  @since 3.9
048  */
049 public final class Const<T> extends Val<T> implements Serializable {
050 
051     private static final long serialVersionUID = 2L;
052 
053     private final T _const;
054 
055     private Const(final String name, final T constant) {
056         super(name);
057         _const = constant;
058     }
059 
060     @Override
061     public T value() {
062         return _const;
063     }
064 
065     @Override
066     public String toString() {
067         return name() != null ? name() : Objects.toString(_const);
068     }
069 
070     /**
071      * Return a new constant with the given name and value.
072      *
073      @param name the constant name
074      @param value the constant value
075      @param <T> the constant type
076      @return a new constant
077      @throws NullPointerException if the given constant {@code name} is
078      *        {@code null}
079      */
080     public static <T> Const<T> of(final String name, final T value) {
081         return new Const<>(requireNonNull(name), value);
082     }
083 
084     /**
085      * Return a new constant with the given value.
086      *
087      @param value the constant value
088      @param <T> the constant type
089      @return a new constant
090      */
091     public static <T> Const<T> of(final T value) {
092         return new Const<>(null, value);
093     }
094 
095 
096     /* *************************************************************************
097      *  Java object serialization
098      * ************************************************************************/
099 
100     private Object writeReplace() {
101         return new Serial(Serial.CONST, this);
102     }
103 
104     private void readObject(final ObjectInputStream stream)
105         throws InvalidObjectException
106     {
107         throw new InvalidObjectException("Serialization proxy required.");
108     }
109 
110     void write(final ObjectOutput outthrows IOException {
111         writeNullableString(name(), out);
112         out.writeObject(_const);
113     }
114 
115     @SuppressWarnings({"unchecked""rawtypes"})
116     static Object read(final ObjectInput in)
117         throws IOException, ClassNotFoundException
118     {
119         final String name = readNullableString(in);
120         final Object value = in.readObject();
121         return new Const(name, value);
122     }
123 
124 }