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: /**
034: * @author Teodor Danciu (teodord@users.sourceforge.net)
035: * @version $Id: JRDistinctCountExtendedIncrementerFactory.java 1311 2006-06-23 09:19:26Z teodord $
036: */
037: public class JRDistinctCountExtendedIncrementerFactory extends
038: JRAbstractExtendedIncrementerFactory {
039:
040: /**
041: *
042: */
043: private static JRDistinctCountExtendedIncrementerFactory mainInstance = new JRDistinctCountExtendedIncrementerFactory();
044:
045: /**
046: *
047: */
048: public JRDistinctCountExtendedIncrementerFactory() {
049: }
050:
051: /**
052: *
053: */
054: public static JRDistinctCountExtendedIncrementerFactory getInstance() {
055: return mainInstance;
056: }
057:
058: /**
059: *
060: */
061: public JRExtendedIncrementer getExtendedIncrementer(byte calculation) {
062: return new JRDistinctCountExtendedIncrementer();
063: }
064:
065: public static JRExtendedIncrementerFactory getFactory(
066: Class valueClass) {
067: return JRDistinctCountExtendedIncrementerFactory.getInstance();
068: }
069: }
070:
071: /**
072: *
073: */
074: class JRDistinctCountExtendedIncrementer extends
075: JRAbstractExtendedIncrementer {
076:
077: private DistinctCountHolder lastHolder = new DistinctCountHolder();
078:
079: /**
080: *
081: */
082: public JRDistinctCountExtendedIncrementer() {
083: }
084:
085: /**
086: *
087: */
088: public Object increment(JRCalculable variable,
089: Object expressionValue, AbstractValueProvider valueProvider) {
090: DistinctCountHolder holder = (DistinctCountHolder) variable
091: .getIncrementedValue();
092:
093: if (holder == null) {
094: holder = lastHolder;
095: } else {
096: lastHolder = holder;
097: }
098:
099: holder.addLastValue();
100:
101: return new DistinctCountHolder(holder, expressionValue);
102: }
103:
104: /**
105: *
106: */
107: public Object combine(JRCalculable calculable1,
108: JRCalculable calculable2,
109: AbstractValueProvider valueProvider) {
110: Set distinctValues = new HashSet();
111:
112: DistinctCountHolder holder1 = (DistinctCountHolder) calculable1
113: .getValue();
114: if (holder1 != null) {
115: distinctValues.addAll(holder1.getDistinctValues());
116: if (holder1.getLastValue() != null)
117: distinctValues.add(holder1.getLastValue());
118: }
119:
120: DistinctCountHolder holder2 = (DistinctCountHolder) calculable2
121: .getValue();
122: if (holder2 != null) {
123: distinctValues.addAll(holder2.getDistinctValues());
124: if (holder2.getLastValue() != null)
125: distinctValues.add(holder2.getLastValue());
126: }
127:
128: return new DistinctCountHolder(distinctValues);
129: }
130:
131: /**
132: *
133: */
134: public Object initialValue() {
135: return null;
136: }
137:
138: }
|