com.teamscale.commons.lang.IntSummaryStatisticsHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-commons Show documentation
Show all versions of teamscale-commons Show documentation
Provides common DTOs for Teamscale
The newest version!
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.teamscale.commons.lang;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.IntSummaryStatistics;
import java.util.Objects;
import org.conqat.lib.commons.test.IndexValueClass;
/**
* {@link java.io.Serializable} holder for {@link IntSummaryStatistics}.
*
* @see LongSummaryStatisticsHolder
*/
@IndexValueClass
public class IntSummaryStatisticsHolder implements Externalizable {
private static final long serialVersionUID = 1L;
/** The delegate {@link IntSummaryStatistics}. */
private IntSummaryStatistics statistics;
public IntSummaryStatisticsHolder() {
this(new IntSummaryStatistics());
}
public IntSummaryStatisticsHolder(IntSummaryStatistics statistics) {
this.statistics = Objects.requireNonNull(statistics, "statistics");
}
public IntSummaryStatistics getStatistics() {
return statistics;
}
/**
* Records a new value into the summary information.
*
* @see IntSummaryStatistics#accept(int)
*/
public void update(int value) {
statistics.accept(value);
}
/**
* Merges the state of another {@link IntSummaryStatisticsHolder} into this one.
*
* @see #merge(IntSummaryStatistics)
*/
public void merge(IntSummaryStatisticsHolder other) {
merge(other.statistics);
}
/**
* Merges the state of another {@link IntSummaryStatistics} into this one.
*
* @see IntSummaryStatistics#combine(IntSummaryStatistics)
*/
private void merge(IntSummaryStatistics other) {
statistics.combine(other);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
long count = statistics.getCount();
out.writeLong(count);
if (count > 0) {
out.writeInt(statistics.getMin());
out.writeInt(statistics.getMax());
out.writeLong(statistics.getSum());
}
}
@Override
public void readExternal(ObjectInput in) throws IOException {
long count = in.readLong();
if (count == 0) {
statistics = new IntSummaryStatistics();
} else {
int min = in.readInt();
int max = in.readInt();
long sum = in.readLong();
statistics = new IntSummaryStatistics(count, min, max, sum);
}
}
@Override
public String toString() {
return statistics.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy