
net.sf.jagg.msd.BigDecimalDiscriminator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jagg-core Show documentation
Show all versions of jagg-core Show documentation
jAgg is a Java 5.0 API that supports “group by” operations on Lists of Java objects: aggregate operations such as count, sum, max, min, avg, and many more. It also allows custom aggregate operations.
The newest version!
package net.sf.jagg.msd;
import java.math.BigDecimal;
/**
* A BigDecimalDiscriminator
discriminates Lists
of
* BigDecimals
.
*
* @author Randy Gettman
* @since 0.5.0
*/
public class BigDecimalDiscriminator extends NumberDiscriminator
{
/**
* Returns a portion extractor appropriate for BigDecimals
.
* @param e An Extractor
that returns appropriate labels.
* @return An appropriate PortionExtractor
.
*/
protected PortionExtractor getPortionExtractor(Extractor e)
{
return new PortionExtractor(e) {
/**
* If index is 0 or 1, then the label will be the high-order or low-
* order 16 bits of the scale, respectively. All other indexes will
* have 2 subtracted, then be treated as an index into the byte array
* of the significand, which is a BigInteger
.
* @param element The element.
* @return The specific portion of the element, cast as an integer.
*/
public int getLabel(E element)
{
// Stripping trailing zeroes makes BDs with same mathematical value
// have the same representation. I.e. [6.00] => [6]
BigDecimal bd = myExtractor.getLabel(element).stripTrailingZeros();
if (myIndex < 2)
{
int scalePortion = bd.scale();
scalePortion >>= (1 - myIndex) * PORTION_BITS;
return scalePortion & PORTION_MASK;
}
// Treat "our index minus two" as the index into the byte array.
return bd.unscaledValue().toByteArray()[myIndex - 2] & PORTION_MASK;
}
/**
* Complete after the end of the byte array representation.
* @param element The element.
* @return true
if we are off the end of the byte array,
* representation, false
otherwise.
*/
public boolean isComplete(E element)
{
// Take into the account that we are "cramming" two index positions
// for the scale first, before the actual byte array.
return myExtractor.isComplete(element) ||
myIndex >= myExtractor.getLabel(element).stripTrailingZeros().unscaledValue().toByteArray().length + 2;
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy