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

com.xdev.jadoth.cql.CqlAggregateCollector Maven / Gradle / Ivy

/*
 * XDEV Application Framework - XDEV Application Framework
 * Copyright © 2003 XDEV Software (https://xdev.software)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package com.xdev.jadoth.cql;

import static com.xdev.jadoth.Jadoth.keyValue;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import com.xdev.jadoth.collections.Collecting;
import com.xdev.jadoth.collections.GrowList;
import com.xdev.jadoth.collections.Sortable;
import com.xdev.jadoth.collections.XList;
import com.xdev.jadoth.lang.Equalator;
import com.xdev.jadoth.util.KeyValue;


/**
 * CQL aggregate that aggregates all entries in key -> list groups according to a given distinction equalator with
 * key being the first entry that matches a new distinction group and the list containing all entries that belong to 
 * the same distinction group, including the key entry.
 * 

* The behaviour of this aggregate resembles the COLLECT(...) aggregate in SQL with a GROUP BY (...) that is defined * by the given equalator. * * @author Thomas Muenz * */ public class CqlAggregateCollector implements CqlAggregator>>, Collecting>, Collecting, Sortable { /////////////////////////////////////////////////////////////////////////// // instance fields // ///////////////////// /* (30.09.2010)TODO replace List+Map combination by ListMap when available. * Adjust aggregate() and project() accordingly. * */ private final GrowList keys; private final Map> aggregates; /////////////////////////////////////////////////////////////////////////// // constructors // ///////////////////// public CqlAggregateCollector() { this.keys = new GrowList(); this.aggregates = new HashMap>(); } public CqlAggregateCollector(final int initialCapacity) { this.keys = new GrowList(initialCapacity); this.aggregates = new HashMap>(initialCapacity); } /////////////////////////////////////////////////////////////////////////// // override methods // ///////////////////// @Override public CqlAggregateCollector reset() { this.aggregates.clear(); this.keys.clear(); return this; } @Override public Collecting provideCollector() { return this; } public boolean add(final E s) { // does nothing as aggregate() handles keys as well return false; } @Override public Sortable sort(final Comparator comparator) { this.keys.sort(comparator); return this; } @Override public XList>> project(final Collecting data) { final GrowList keys = this.keys; final GrowList>> result = new GrowList>>(keys.size()) ; for(final E k : keys){ result.add(keyValue(k, this.aggregates.get(k))); } return result; } /** * @param element * @param equalator * @return * @see com.xdev.jadoth.cql.CqlAggregator#aggregate(java.lang.Object, com.xdev.jadoth.lang.Equalator) */ @Override public boolean aggregate(final E element, final Equalator equalator) { XList aggValues; final E key = this.keys.search(element, equalator); if(key == null){ // new key element, add new aggregation group aggValues = new GrowList(); aggValues.add(element); this.keys.add(element); this.aggregates.put(element, aggValues); return true; //return true to signal new grouping element (only for contract, has no effect for this class) } aggValues = this.aggregates.get(key); aggValues.add(element); return false; //return false to signal already existing grouping element } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy