void collectVals(BucketMap map, boolean sum) throws JRException { ListIterator totalIt = entries.listIterator(); MapEntry totalItEntry = totalIt.hasNext() ? (MapEntry) totalIt.next() : null; Iterator it = map.entryIterator(); Map.Entry entry = it.hasNext() ? (Map.Entry) it.next() : null; while (entry != null) { Bucket key = (Bucket) entry.getKey(); int compare = totalItEntry == null ? -1 : key.compareTo(totalItEntry.key); if (compare <= 0) { Object addVal = null; if (last) { if (sum) { MeasureValue[] totalVals = compare == 0 ? (MeasureValue[]) totalItEntry.value : null; if (totalVals == null) { totalVals = initMeasureValues(); addVal = totalVals; } sumVals(totalVals, (MeasureValue[]) entry.getValue()); } } else { BucketListMap nextTotals = compare == 0 ? (BucketListMap) totalItEntry.value : null; if (nextTotals == null) { nextTotals = createCollectBucketMap(level + 1); addVal = nextTotals; } nextTotals.collectVals((BucketMap) entry.getValue(), sum); } if (compare < 0) { if (totalItEntry != null) { totalIt.previous(); } totalIt.add(new MapEntry(key, addVal)); if (totalItEntry != null) { totalIt.next(); } } entry = it.hasNext() ? (Map.Entry) it.next() : null; } if (compare >= 0) { totalItEntry = totalIt.hasNext() ? (MapEntry) totalIt.next() : null; } } }
void sumValues(BucketMap bucketMap) throws JRException { for (Iterator<Map.Entry<Bucket, Object>> it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry<Bucket, Object> entry = it.next(); // find the total entry that matches the map entry. // the total map is should contain all collected entries // FIXME optimize this for sorted maps where we can assume that the order is the same Object value = get(entry.getKey()); if (last) { // last level, sum the values sumVals((MeasureValue[]) value, (MeasureValue[]) entry.getValue()); } else { // go to the next level ((BucketMapMap) value).sumValues((BucketMap) entry.getValue()); } } }
protected void computeColumnTotal(BucketMap bucketMap) throws JRException { MeasureValue[] totals = initMeasureValues(); for (Iterator it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); for (int i = bucketMap.level + 1; i < allBuckets.length; ++i) { entry = ((BucketMap) entry.getValue()).getTotalEntry(); } sumVals(totals, (MeasureValue[]) entry.getValue()); } for (int i = bucketMap.level + 1; i < allBuckets.length; ++i) { bucketMap = bucketMap.addTotalNextMap(); } bucketMap.addTotalEntry(totals); }
void copyEntries(BucketMap bucketMap) { for (Iterator<Entry<Bucket, Object>> bucketIterator = bucketMap.entryIterator(); bucketIterator.hasNext(); ) { Entry<Bucket, Object> bucketEntry = bucketIterator.next(); Bucket bucketKey = bucketEntry.getKey(); Object copyBucketValue; if (bucketMap.last) { copyBucketValue = initMeasureValues(); } else { BucketMap bucketSubMap = (BucketMap) bucketEntry.getValue(); BucketMapMap copyBucketSubMap = new BucketMapMap(level + 1, false); copyBucketSubMap.copyEntries(bucketSubMap); copyBucketValue = copyBucketSubMap; } map.put(bucketKey, copyBucketValue); } }
protected CollectedList createHeadersList( byte dimension, BucketMap bucketMap, int level, boolean total) { CollectedList headers = new CollectedList(); for (Iterator it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); Bucket bucketValue = (Bucket) entry.getKey(); boolean totalBucket = bucketValue.isTotal(); byte totalPosition = allBuckets[bucketMap.level].getTotalPosition(); boolean createHeader = !totalBucket || total || totalPosition != BucketDefinition.TOTAL_POSITION_NONE; if (createHeader) { CollectedList nextHeaders; if (level + 1 < buckets[dimension].length) { BucketMap nextMap = (BucketMap) entry.getValue(); nextHeaders = createHeadersList(dimension, nextMap, level + 1, total || totalBucket); } else { nextHeaders = new CollectedList(); nextHeaders.span = 1; } nextHeaders.key = bucketValue; if (totalBucket) { if (totalPosition == BucketDefinition.TOTAL_POSITION_START) { headers.addFirst(nextHeaders); } else { headers.add(nextHeaders); } } else { headers.add(nextHeaders); } } } if (headers.span == 0) { headers.span = 1; } return headers; }
protected void computeRowTotals(BucketMap bucketMap) throws JRException { BucketListMap totals = createCollectBucketMap(rowBucketCount); for (Iterator it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); for (int i = bucketMap.level + 1; i < rowBucketCount; ++i) { entry = ((BucketMap) entry.getValue()).getTotalEntry(); } totals.collectVals((BucketMap) entry.getValue(), true); } BucketMap totalBucketMap = bucketMap; for (int i = bucketMap.level + 1; i < rowBucketCount; ++i) { totalBucketMap = totalBucketMap.addTotalNextMap(); } totalBucketMap.addTotalEntry(totals); }
protected void computeRowTotals(BucketMap bucketMap) throws JRException { BucketMapMap totals = createRowTotalsBucketMap(); for (Iterator<Map.Entry<Bucket, Object>> it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry<Bucket, Object> entry = it.next(); for (int i = bucketMap.level + 1; i < rowBucketCount; ++i) { entry = ((BucketMap) entry.getValue()).getTotalEntry(); } totals.sumValues((BucketMap) entry.getValue()); } BucketMap totalBucketMap = bucketMap; for (int i = bucketMap.level + 1; i < rowBucketCount; ++i) { totalBucketMap = totalBucketMap.addTotalNextMap(); } totalBucketMap.addTotalEntry(totals); }
protected void collectCols(BucketListMap collectedCols, BucketMap bucketMap) throws JRException { if (allBuckets[bucketMap.level].computeTotal()) { BucketMap map = bucketMap; for (int i = bucketMap.level; i < rowBucketCount; ++i) { map = (BucketMap) map.getTotalEntry().getValue(); } collectedCols.collectVals(map, false); return; } for (Iterator it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); BucketMap nextMap = (BucketMap) entry.getValue(); if (bucketMap.level == rowBucketCount - 1) { collectedCols.collectVals(nextMap, false); } else { collectCols(collectedCols, nextMap); } } }
protected void computeTotals(BucketMap bucketMap) throws JRException { byte dimension = bucketMap.level < rowBucketCount ? DIMENSION_ROW : DIMENSION_COLUMN; if (dimension == DIMENSION_COLUMN && !allBuckets[allBuckets.length - 1].computeTotal()) { return; } if (!bucketMap.last) { for (Iterator it = bucketMap.entryIterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); computeTotals((BucketMap) entry.getValue()); } } if (allBuckets[bucketMap.level].computeTotal()) { if (dimension == DIMENSION_COLUMN) { computeColumnTotal(bucketMap); } else { computeRowTotals(bucketMap); } } }
protected void fillCells( CollectedList[] collectedHeaders, BucketMap bucketMap, int level, int[] pos, List vals, List bucketMaps) { bucketMaps.add(bucketMap); byte dimension = level < rowBucketCount ? DIMENSION_ROW : DIMENSION_COLUMN; boolean last = level == allBuckets.length - 1; CollectedList[] nextCollected = null; if (!last) { nextCollected = new CollectedList[DIMENSIONS]; for (int d = 0; d < DIMENSIONS; ++d) { if (d != dimension) { nextCollected[d] = collectedHeaders[d]; } } } boolean incrementRow = level == buckets[BucketingService.DIMENSION_ROW].length - 1; CollectedList collectedList = collectedHeaders[dimension]; Iterator bucketIt = bucketMap == null ? null : bucketMap.entryIterator(); Map.Entry bucketItEntry = bucketIt != null && bucketIt.hasNext() ? (Map.Entry) bucketIt.next() : null; for (Iterator it = collectedList.iterator(); it.hasNext(); ) { CollectedList list = (CollectedList) it.next(); Map.Entry bucketEntry = null; if (list.key.isTotal()) { if (bucketMap != null) { bucketEntry = bucketMap.getTotalEntry(); } } else { if (bucketItEntry != null && bucketItEntry.getKey().equals(list.key)) { bucketEntry = bucketItEntry; bucketItEntry = bucketIt.hasNext() ? (Map.Entry) bucketIt.next() : null; } } vals.add(list.key); if (last) { fillCell(pos, vals, bucketMaps, bucketEntry); } else { nextCollected[dimension] = list; BucketMap nextMap = bucketEntry == null ? null : (BucketMap) bucketEntry.getValue(); fillCells(nextCollected, nextMap, level + 1, pos, vals, bucketMaps); } vals.remove(vals.size() - 1); if (incrementRow) { ++pos[0]; pos[1] = 0; } } bucketMaps.remove(bucketMaps.size() - 1); }