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