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.stat;
021
022import static java.lang.Double.NaN;
023import static java.util.Objects.requireNonNull;
024
025import java.io.Serial;
026import java.io.Serializable;
027import java.util.LongSummaryStatistics;
028import java.util.function.ToLongFunction;
029import java.util.stream.Collector;
030
031/**
032 * <i>Value</i> objects which contains statistical summary information.
033 *
034 * @see java.util.LongSummaryStatistics
035 *
036 * @param count the count of values recorded
037 * @param min the minimum value recorded, or {@link Long#MAX_VALUE} if no
038 *            values have been recorded
039 * @param max the maximum value recorded, or {@link Long#MIN_VALUE} if no
040 *            values have been recorded
041 * @param sum the sum of values recorded, or zero if no values have been recorded
042 * @param mean the arithmetic mean of values recorded, or zero if no values have
043 *            been recorded
044 *
045 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
046 * @since 3.0
047 * @version 7.0
048 */
049public record LongSummary(
050        long count,
051        long min,
052        long max,
053        long sum,
054        double mean
055)
056        implements Serializable
057{
058
059        @Serial
060        private static final long serialVersionUID = 2L;
061
062        @Override
063        public String toString() {
064                return String.format(
065                        "LongSummary[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s]",
066                        count(), min(), max(), sum(), mean()
067                );
068        }
069
070        /**
071         * Return a new value object of the statistical summary, currently
072         * represented by the {@code statistics} object.
073         *
074         * @param statistics the creating (mutable) statistics class
075         * @return the statistical moments
076         */
077        public static LongSummary of(final LongSummaryStatistics statistics) {
078                return new LongSummary(
079                        statistics.getCount(),
080                        statistics.getMin(),
081                        statistics.getMax(),
082                        statistics.getSum(),
083                        statistics.getAverage()
084                );
085        }
086
087        /**
088         * Return a {@code Collector} which applies a long-producing mapping
089         * function to each input element, and return summary-statistics for the
090         * resulting values.
091         * {@snippet lang="java":
092         * final Stream<SomeObject> stream = null; // @replace substring='null' replacement="..."
093         * final LongSummary summary = stream
094         *     .collect(toLongSummary(v -> v.longValue()));
095         * }
096         *
097         * @param mapper a mapping function to apply to each element
098         * @param <T> the type of the input elements
099         * @return a {@code Collector} implementing the summary-statistics reduction
100         * @throws java.lang.NullPointerException if the given {@code mapper} is
101         *         {@code null}
102         */
103        public static <T> Collector<T, ?, LongSummary>
104        toLongSummary(final ToLongFunction<? super T> mapper) {
105                requireNonNull(mapper);
106                return Collector.of(
107                        LongSummaryStatistics::new,
108                        (a, b) -> a.accept(mapper.applyAsLong(b)),
109                        (a, b) -> {a.combine(b); return a;},
110                        LongSummary::of
111                );
112        }
113
114
115        /* *************************************************************************
116         * Some static helper methods.
117         **************************************************************************/
118
119        /**
120         * Return the minimum value of the given double array.
121         *
122         * @since 4.0
123         *
124         * @param values the array.
125         * @return the minimum value or {@link Long#MAX_VALUE} if the given array is
126         *         empty.
127         * @throws NullPointerException if the given array is {@code null}.
128         */
129        public static long min(final long[] values) {
130                long min = Long.MAX_VALUE;
131                if (values.length > 0) {
132                        min = values[0];
133                        for (long value : values) {
134                                if (value < min) {
135                                        min = value;
136                                }
137                        }
138                }
139
140                return min;
141        }
142
143        /**
144         * Return the maximum value of the given double array.
145         *
146         * @since 4.0
147         *
148         * @param values the array.
149         * @return the maximum value or {@link Long#MIN_VALUE} if the given array is
150         *         empty.
151         * @throws NullPointerException if the given array is {@code null}.
152         */
153        public static long max(final long[] values) {
154                long max = Long.MIN_VALUE;
155                if (values.length > 0) {
156                        max = values[0];
157                        for (long value : values) {
158                                if (value > max) {
159                                        max = value;
160                                }
161                        }
162                }
163
164                return max;
165        }
166
167        /**
168         * Return the sum of the given double array.
169         *
170         * @since 4.0
171         *
172         * @param values the values to sum up.
173         * @return the sum of the given {@code values}.
174         * @throws NullPointerException if the given array is {@code null}.
175         */
176        public static long sum(final long[] values) {
177                long sum = 0;
178                for (int i = values.length; --i >= 0;) {
179                        sum += values[i];
180                }
181                return sum;
182        }
183
184        /**
185         * Returns a double describing the arithmetic mean of the values, or
186         * {@link Double#NaN} if the {@code values} array is empty.
187         *
188         * @since 4.0
189         *
190         * @param values the values to calculate the mean of
191         * @return the arithmetic mean of the given {@code values} or
192         *         {@link Double#NaN} if the {@code values} array is empty
193         * @throws NullPointerException if the given array is {@code null}.
194         */
195        public static double mean(final long[] values) {
196                return values.length > 0 ? (double)sum(values)/values.length : NaN;
197        }
198
199}