LongSummary.java
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.LongSummaryStatistics;
026 import java.util.function.ToLongFunction;
027 import java.util.stream.Collector;
028 
029 /**
030  <i>Value</i> objects which contains statistical summary information.
031  *
032  @see java.util.LongSummaryStatistics
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 LongSummary implements Serializable {
039 
040     private static final long serialVersionUID = 1L;
041 
042     private final long _count;
043     private final long _min;
044     private final long _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 LongSummary(
058         final long count,
059         final long min,
060         final long 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 Long.MAX_VALUE} if no
082      * values have been recorded.
083      *
084      @return the minimum value, or {@code Long.MAX_VALUE} if none
085      */
086     public long getMin() {
087         return _min;
088     }
089 
090     /**
091      * Return the maximum value recorded, or {@code Long.MIN_VALUE} if no
092      * values have been recorded.
093      *
094      @return the maximum value, or {@code Long.MIN_VALUE} if none
095      */
096     public long 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(_mean37;
128         return hash;
129     }
130 
131     @Override
132     public boolean equals(final Object obj) {
133         return obj instanceof LongSummary &&
134             _count == ((LongSummary)obj)._count &&
135             _sum == ((LongSummary)obj)._sum &&
136             _min == ((LongSummary)obj)._min &&
137             _max == ((LongSummary)obj)._max &&
138             Double.compare(_mean, ((LongSummary)obj)._mean== 0;
139     }
140 
141     @Override
142     public String toString() {
143         return String.format(
144             "LongSummary[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 LongSummary of(
160         final long count,
161         final long min,
162         final long max,
163         final long sum,
164         final double mean
165     ) {
166         return new LongSummary(
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 LongSummary of(final LongSummaryStatistics statistics) {
183         return new LongSummary(
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 long-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 LongSummary summary = stream
200      *     .collect(toLongSummary(v -> v.longValue()));
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, ?, LongSummary>
210     toLongSummary(final ToLongFunction<? super T> mapper) {
211         requireNonNull(mapper);
212         return Collector.of(
213             LongSummaryStatistics::new,
214             (a, b-> a.accept(mapper.applyAsLong(b)),
215             (a, b-> {a.combine(b)return a;},
216             LongSummary::of
217         );
218     }
219 
220 }