
net.sf.jagg.msd.PropertiesDiscriminator 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.util.ArrayList;
import java.util.List;
import net.sf.jagg.model.ChainedMethodCall;
import net.sf.jagg.util.MethodCache;
/**
* A PropertiesDiscriminator
discriminates Lists
of
* Objects
by their properties.
*
* @author Randy Gettman
* @since 0.5.0
*/
public class PropertiesDiscriminator extends ChainedDiscriminator
{
/**
* A cache of ChainedMethodCalls
.
*/
private List myMethodCalls;
/**
* An array of property strings.
*/
private String[] myProperties;
/**
* Creates a PropertiesDiscriminator
that discriminates on
* the given properties of a list of elements.
* @param properties An array of properties on which to discriminate.
*/
public PropertiesDiscriminator(String... properties)
{
myProperties = properties;
int numProperties = myProperties.length;
myMethodCalls = new ArrayList(numProperties);
}
/**
* Creates a PropertiesDiscriminator
that discriminates on
* the given properties of a list of elements.
* @param properties A List
of properties on which to
* discriminate.
*/
public PropertiesDiscriminator(List properties)
{
myProperties = new String[properties.size()];
if (!properties.isEmpty())
properties.toArray(myProperties);
int numProperties = myProperties.length;
myMethodCalls = new ArrayList(numProperties);
}
/**
* Returns an appropriate ChainedExtractor
.
* @param extractor A ChainedExtractor
that returns appropriate
* labels.
* @return An appropriate ChainedExtractor
.
*/
@SuppressWarnings({"unchecked", "ForLoopReplaceableByForEach"})
protected ChainedExtractor getChainedExtractor(List elements, Extractor extractor)
{
// Get all MethodCalls here.
T obj = extractor.getLabel(elements.get(0));
MethodCache cache = MethodCache.getMethodCache();
for (int i = 0; i < myProperties.length; i++)
{
String property = myProperties[i];
myMethodCalls.add(cache.getMethodCallFromProperty(obj, property));
}
return new MethodCallChainedExtractor(extractor);
}
/**
* Returns the Discriminator
that discriminates on a specific
* property, indexed by the given index. If it is known that no more loops
* are necessary, then the returned Discriminator
may be
* null
.
* @param elements The list of elements.
* @param extractor The ChainedExtractor
that was obtained from
* getChainedExtractor
.
* @param index The index of the loop.
* @return A Discriminator
that discriminates on a specific
* property's type.
*/
protected Discriminator> getDiscriminator(List elements, ChainedExtractor extractor,
int index)
{
if (index < myProperties.length)
{
ChainedMethodCall mc = myMethodCalls.get(index);
Class> returnType = mc.getChainedReturnType();
return Discriminators.getDiscriminator(returnType);
}
return new NullDiscriminator(null);
}
/**
* An MethodCallChainedExtractor
extracts results of a method
* call as labels.
* @param The type of element.
* @param The type of label.
* @param The base type of the object.
*/
protected class MethodCallChainedExtractor extends ChainedExtractor
{
/**
* Create an MethodCallChainedExtractor
that uses the given
* Extractor
to retrieve the base item.
* @param extractor An Extractor
.
*/
public MethodCallChainedExtractor(Extractor extractor)
{
super(extractor);
}
/**
* The label is the result of a MethodCall
on the base
* object type.
* @param element The element.
* @return The result of a MethodCall
on the base object
* type.
*/
@SuppressWarnings("unchecked")
public L getLabel(E element)
{
B obj = myExtractor.getLabel(element);
ChainedMethodCall mc = myMethodCalls.get(myIndex);
return (L) mc.invoke(obj);
}
/**
* The discrimination is complete after all properties have been used.
* @param element The element.
* @return true
if complete,
* false
otherwise.
*/
public boolean isComplete(E element)
{
return myExtractor.isComplete(element) || myIndex >= myProperties.length;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy