001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import java.util.HashSet;
031: import java.util.Set;
032:
033: import net.sf.jasperreports.engine.JRVariable;
034:
035: /**
036: * @author Teodor Danciu (teodord@users.sourceforge.net)
037: * @version $Id: JRDistinctCountIncrementerFactory.java 1311 2006-06-23 09:19:26Z teodord $
038: */
039: public class JRDistinctCountIncrementerFactory implements
040: JRIncrementerFactory {
041:
042: /**
043: *
044: */
045: private static JRDistinctCountIncrementerFactory mainInstance = new JRDistinctCountIncrementerFactory();
046:
047: /**
048: *
049: */
050: public JRDistinctCountIncrementerFactory() {
051: }
052:
053: /**
054: *
055: */
056: public static JRDistinctCountIncrementerFactory getInstance() {
057: return mainInstance;
058: }
059:
060: /**
061: *
062: */
063: public JRIncrementer getIncrementer(byte calculation) {
064: return new JRDistinctCountIncrementer();
065: }
066: }
067:
068: /**
069: *
070: */
071: class JRDistinctCountIncrementer implements JRIncrementer {
072:
073: private DistinctCountHolder lastHolder = new DistinctCountHolder();
074:
075: /**
076: *
077: */
078: public JRDistinctCountIncrementer() {
079: }
080:
081: /**
082: *
083: */
084: public Object increment(JRFillVariable variable,
085: Object expressionValue, AbstractValueProvider valueProvider) {
086: DistinctCountHolder holder = (DistinctCountHolder) variable
087: .getIncrementedValue();
088:
089: if (holder == null) {
090: holder = lastHolder;
091: } else {
092: lastHolder = holder;
093: }
094:
095: if (variable.getResetType() == JRVariable.RESET_TYPE_REPORT
096: || variable.isInitialized()) {
097: holder.addLastValue();
098: }
099:
100: return new DistinctCountHolder(holder, expressionValue);
101: }
102:
103: }
104:
105: /**
106: *
107: */
108: class DistinctCountHolder {
109: private Set distinctValues = null;
110: private Object lastValue = null;
111:
112: public DistinctCountHolder() {
113: distinctValues = new HashSet();
114: }
115:
116: public DistinctCountHolder(Set distinctValues) {
117: this .distinctValues = distinctValues;
118: }
119:
120: public DistinctCountHolder(DistinctCountHolder holder,
121: Object lastValue) {
122: this .distinctValues = holder.getDistinctValues();
123: this .lastValue = lastValue;
124: }
125:
126: public void init() {
127: distinctValues = new HashSet();
128: }
129:
130: public Set getDistinctValues() {
131: return distinctValues;
132: }
133:
134: public Object getLastValue() {
135: return lastValue;
136: }
137:
138: public void addLastValue() {
139: if (lastValue != null) {
140: distinctValues.add(lastValue);
141: }
142: lastValue = null;
143: }
144:
145: public long getCount() {
146: return distinctValues.size()
147: + (lastValue == null
148: || distinctValues.contains(lastValue) ? 0 : 1);
149: }
150: }
|