All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.commons.lang.LongSummaryStatisticsHolder Maven / Gradle / Ivy

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.LongSummaryStatistics;
import java.util.Objects;

import org.conqat.lib.commons.test.IndexValueClass;

/**
 * {@link java.io.Serializable} holder for {@link LongSummaryStatistics}.
 *
 * @see IntSummaryStatisticsHolder
 */
@IndexValueClass
public class LongSummaryStatisticsHolder implements Externalizable {

	private static final long serialVersionUID = 1L;

	/** The delegate {@link LongSummaryStatistics}. */
	private LongSummaryStatistics statistics;

	public LongSummaryStatisticsHolder() {
		this(new LongSummaryStatistics());
	}

	public LongSummaryStatisticsHolder(LongSummaryStatistics statistics) {
		this.statistics = Objects.requireNonNull(statistics, "statistics");
	}

	public LongSummaryStatistics getStatistics() {
		return statistics;
	}

	/**
	 * Records a new value into the summary information.
	 *
	 * @see LongSummaryStatistics#accept(int)
	 */
	public void update(int value) {
		statistics.accept(value);
	}

	/**
	 * Records a new value into the summary information.
	 *
	 * @see LongSummaryStatistics#accept(long)
	 */
	public void update(long value) {
		statistics.accept(value);
	}

	/**
	 * Merges the state of another {@link LongSummaryStatisticsHolder} into this one.
	 *
	 * @see #merge(LongSummaryStatistics)
	 */
	public void merge(LongSummaryStatisticsHolder other) {
		merge(other.statistics);
	}

	/**
	 * Merges the state of another {@link LongSummaryStatistics} into this one.
	 *
	 * @see #merge(LongSummaryStatisticsHolder)
	 * @see LongSummaryStatistics#combine(LongSummaryStatistics)
	 */
	public void merge(LongSummaryStatistics other) {
		statistics.combine(other);
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		long count = statistics.getCount();
		out.writeLong(count);
		if (count > 0) {
			out.writeLong(statistics.getMin());
			out.writeLong(statistics.getMax());
			out.writeLong(statistics.getSum());
		}
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException {
		long count = in.readLong();
		if (count == 0) {
			statistics = new LongSummaryStatistics();
		} else {
			long min = in.readLong();
			long max = in.readLong();
			long sum = in.readLong();
			statistics = new LongSummaryStatistics(count, min, max, sum);
		}
	}

	@Override
	public String toString() {
		return statistics.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy