001 /*
002 * Java Genetic Algorithm Library (jenetics-3.9.0).
003 * Copyright (c) 2007-2017 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@gmx.at)
019 */
020 package org.jenetics.stat;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.io.Serializable;
025 import java.util.IntSummaryStatistics;
026 import java.util.function.ToIntFunction;
027 import java.util.stream.Collector;
028
029 /**
030 * <i>Value</i> objects which contains statistical summary information.
031 *
032 * @see java.util.IntSummaryStatistics
033 *
034 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
035 * @since 3.0
036 * @version 3.0
037 */
038 public final class IntSummary implements Serializable {
039
040 private static final long serialVersionUID = 1L;
041
042 private final long _count;
043 private final int _min;
044 private final int _max;
045 private final long _sum;
046 private final double _mean;
047
048 /**
049 * Create an immutable object which contains statistical summary values.
050 *
051 * @param count the count of values recorded
052 * @param min the minimum value
053 * @param max the maximum value
054 * @param sum the sum of the recorded values
055 * @param mean the arithmetic mean of values
056 */
057 private IntSummary(
058 final long count,
059 final int min,
060 final int max,
061 final long sum,
062 final double mean
063 ) {
064 _count = count;
065 _min = min;
066 _max = max;
067 _sum = sum;
068 _mean = mean;
069 }
070
071 /**
072 * Returns the count of values recorded.
073 *
074 * @return the count of recorded values
075 */
076 public long getCount() {
077 return _count;
078 }
079
080 /**
081 * Return the minimum value recorded, or {@code Integer.MAX_VALUE} if no
082 * values have been recorded.
083 *
084 * @return the minimum value, or {@code Integer.MAX_VALUE} if none
085 */
086 public int getMin() {
087 return _min;
088 }
089
090 /**
091 * Return the maximum value recorded, or {@code Integer.MIN_VALUE} if no
092 * values have been recorded.
093 *
094 * @return the maximum value, or {@code Integer.MIN_VALUE} if none
095 */
096 public int getMax() {
097 return _max;
098 }
099
100 /**
101 * Return the sum of values recorded, or zero if no values have been
102 * recorded.
103 *
104 * @return the sum of values, or zero if none
105 */
106 public long getSum() {
107 return _sum;
108 }
109
110 /**
111 * Return the arithmetic mean of values recorded, or zero if no values have
112 * been recorded.
113 *
114 * @return the arithmetic mean of values, or zero if none
115 */
116 public double getMean() {
117 return _mean;
118 }
119
120 @Override
121 public int hashCode() {
122 int hash = 17;
123 hash += 33*_count + 37;
124 hash += 33*_sum + 37;
125 hash += 33*_min + 37;
126 hash += 33*_max + 37;
127 hash += 33*Double.doubleToLongBits(_mean) + 37;
128 return hash;
129 }
130
131 @Override
132 public boolean equals(final Object obj) {
133 return obj instanceof IntSummary &&
134 _count == ((IntSummary)obj)._count &&
135 _sum == ((IntSummary)obj)._sum &&
136 _min == ((IntSummary)obj)._min &&
137 _max == ((IntSummary)obj)._max &&
138 Double.compare(_mean, ((IntSummary)obj)._mean) == 0;
139 }
140
141 @Override
142 public String toString() {
143 return String.format(
144 "IntSummary[N=%d, ∧=%s, ∨=%s, Σ=%s, μ=%s]",
145 getCount(), getMin(), getMax(), getSum(), getMean()
146 );
147 }
148
149 /**
150 * Create an immutable object which contains statistical summary values.
151 *
152 * @param count the count of values recorded
153 * @param min the minimum value
154 * @param max the maximum value
155 * @param sum the sum of the recorded values
156 * @param mean the arithmetic mean of values
157 * @return an immutable object which contains statistical values
158 */
159 public static IntSummary of(
160 final long count,
161 final int min,
162 final int max,
163 final long sum,
164 final double mean
165 ) {
166 return new IntSummary(
167 count,
168 min,
169 max,
170 sum,
171 mean
172 );
173 }
174
175 /**
176 * Return a new value object of the statistical summary, currently
177 * represented by the {@code statistics} object.
178 *
179 * @param statistics the creating (mutable) statistics class
180 * @return the statistical moments
181 */
182 public static IntSummary of(final IntSummaryStatistics statistics) {
183 return new IntSummary(
184 statistics.getCount(),
185 statistics.getMin(),
186 statistics.getMax(),
187 statistics.getSum(),
188 statistics.getAverage()
189 );
190 }
191
192 /**
193 * Return a {@code Collector} which applies an int-producing mapping
194 * function to each input element, and returns summary-statistics for the
195 * resulting values.
196 *
197 * <pre>{@code
198 * final Stream<SomeObject> stream = ...
199 * final IntSummary summary = stream
200 * .collect(toIntSummary(v -> v.intValue()));
201 * }</pre>
202 *
203 * @param mapper a mapping function to apply to each element
204 * @param <T> the type of the input elements
205 * @return a {@code Collector} implementing the summary-statistics reduction
206 * @throws java.lang.NullPointerException if the given {@code mapper} is
207 * {@code null}
208 */
209 public static <T> Collector<T, ?, IntSummary>
210 toIntSummary(final ToIntFunction<? super T> mapper) {
211 requireNonNull(mapper);
212 return Collector.of(
213 IntSummaryStatistics::new,
214 (a, b) -> a.accept(mapper.applyAsInt(b)),
215 (a, b) -> {a.combine(b); return a;},
216 IntSummary::of
217 );
218 }
219
220 }
|