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.regression;
021
022import static java.util.Objects.requireNonNull;
023
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.List;
027import java.util.Objects;
028import java.util.function.Function;
029
030import io.jenetics.ext.util.Tree;
031
032import io.jenetics.prog.op.Op;
033
034/**
035 * This class holds the actual sample values which are used for the symbolic
036 * regression example. This class is <em>thread-safe</em> and can be used in a
037 * <em>producer-consumer</em> setup. You can add single sample values
038 * ({@link #add(Sample)}) or a list ({@link #addAll(Collection)}) of new values.
039 * These values will be made available for evaluation after an explicit call of
040 * the {@link #publish()} method.
041 *
042 * @implNote
043 * This class is thread-safe.
044 *
045 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
046 * @version 6.0
047 * @since 6.0
048 */
049public final class SampleBuffer<T> implements Sampling<T> {
050
051        private final RingBuffer _buffer;
052
053        private volatile SampleList<T> _snapshot = null;
054
055        public SampleBuffer(final int capacity) {
056                _buffer = new RingBuffer(capacity);
057        }
058
059        /**
060         * Adding a new sample point to the buffer. <em>You need to explicitly
061         * call {@link #publish()} to make it available for the {@link #eval(Tree)}
062         * method.</em>
063         *
064         * @param sample the sample point to add
065         * @throws NullPointerException if the given {@code sample} point is
066         *         {@code null}
067         */
068        public void add(final Sample<T> sample) {
069                _buffer.add(requireNonNull(sample));
070        }
071
072        /**
073         * The given sample points to the buffer.  <em>You need to explicitly
074         * call {@link #publish()} to make it available for the {@link #eval(Tree)}
075         * method.</em>
076         *
077         * @param samples the samples to add to the buffer
078         * @throws NullPointerException if the given {@code samples} is {@code null}
079         */
080        public void addAll(final Collection<? extends Sample<? extends T>> samples) {
081                samples.forEach(Objects::requireNonNull);
082                _buffer.addAll(samples);
083        }
084
085        /**
086         * Making the current sample points available for the {@link #eval(Tree)}
087         * function.
088         *
089         * @return the number of <em>published</em> sample points
090         */
091        @SuppressWarnings({"unchecked", "rawtypes"})
092        public int publish() {
093                final Object[] values = _buffer.snapshot();
094
095                SampleList<T> snapshot = null;
096                if (values != null && values.length > 0) {
097                        final List samples = Arrays.asList(values);
098                        snapshot = new SampleList(samples);
099                }
100
101                try {
102                        return snapshot != null ? snapshot.size() : 0;
103                } finally {
104                        _snapshot = snapshot;
105                }
106        }
107
108        /**
109         * Return the currently <em>published</em> sample points.
110         *
111         * @see #publish()
112         *
113         * @return the currently <em>published</em> sample points
114         */
115        List<Sample<T>> samples() {
116                final SampleList<T> snapshot = _snapshot;
117                return snapshot != null ? snapshot : List.of();
118        }
119
120        @Override
121        public Result<T> eval(final Tree<? extends Op<T>, ?> program) {
122                requireNonNull(program);
123
124                final SampleList<T> snapshot = _snapshot;
125                return snapshot != null && !snapshot.isEmpty()
126                        ? snapshot.eval(program)
127                        : null;
128        }
129
130        @Override
131        public Result<T> eval(final Function<? super T[], ? extends T> function) {
132                requireNonNull(function);
133
134                final SampleList<T> snapshot = _snapshot;
135                return snapshot != null && !snapshot.isEmpty()
136                        ? snapshot.eval(function)
137                        : null;
138        }
139
140}