com.sun.btrace.aggregation.Aggregation Maven / Gradle / Ivy
/*
* Copyright 2008-2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.btrace.aggregation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
/**
* BTrace stores the results of aggregating functions in an Aggregation. The aggregated values may be grouped using a
* composite {@link AggregationKey}.
*
*
* @author Christian Glencross
*/
public class Aggregation implements Cloneable {
private static final AggregationKey NULL_AGGREGATION_KEY = new AggregationKey(new Object[0]);
private final AggregationFunction type;
private final ConcurrentHashMap values = new ConcurrentHashMap();
/**
* Creates an aggregation.
*
* @param type
* the type of aggregation function to use
*
*/
public Aggregation(AggregationFunction type) {
super();
this.type = type;
}
/**
* Adds an item of data to the aggregation with an empty key. This method is recommended if the aggregation will
* contain only a single value.
*
* @param data
* the value to be added
*/
public void add(long data) {
add(NULL_AGGREGATION_KEY, data);
}
/**
* Adds an item of data to the aggregation with the specified grouping key.
*
* @param key
* the aggregation key
* @param data
* the value to be added
*/
public void add(AggregationKey key, long data) {
AggregationValue aggregationValue = values.get(key);
if (aggregationValue == null) {
aggregationValue = type.newValue();
AggregationValue existing = values.putIfAbsent(key, aggregationValue);
if (existing != null) {
aggregationValue = existing;
}
}
aggregationValue.add(data);
}
/**
* Resets all values in the aggregation to their default.
*/
public void clear() {
for (AggregationValue value : values.values()) {
value.clear();
}
}
/**
* Reduces the size of the aggregation to the absolute value of count
. If count is greater than
* zero, the largest aggregated values are preserved. If it is less than zero, the smallest aggregated values are
* preserved. Passing a value of zero clears the aggregation completely.
*
* @param count
* the absolute number indicates the number of aggregated values to preserve.
*/
public void truncate(int count) {
if (count == 0) {
values.clear();
} else {
List> sortedContents = sort();
int collectionSize = sortedContents.size();
int numberToRemove = collectionSize - Math.abs(count);
if (numberToRemove < 0) {
return;
}
List> removeContents;
if (count > 0) {
// Remove from the start of the list
removeContents = sortedContents.subList(0, numberToRemove);
} else {
removeContents = sortedContents.subList(collectionSize - numberToRemove, collectionSize);
}
for (int i = 0; i < removeContents.size(); i++) {
values.remove(removeContents.get(i).getKey());
}
}
}
/**
* Returns details of the aggregation in a tabular format which can be serialized across the wire and formatted for
* display. The data is represented as a List of rows. The last element in each row represents the aggregated value,
* the elements before this in the row contain the elements of the aggregating key.
*
* @return details of the aggregation in a tabular format.
*/
public List