KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > CountAggregator


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.CountAggregator
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23
24 import org.apache.derby.iapi.types.DataValueDescriptor;
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.execute.ExecAggregator;
29 import org.apache.derby.iapi.services.io.StoredFormatIds;
30 import org.apache.derby.iapi.services.io.Formatable;
31 import java.io.ObjectOutput JavaDoc;
32 import java.io.ObjectInput JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 /**
36  * Aggregator for COUNT()/COUNT(*).
37  * @author jamie
38  */

39 public final class CountAggregator
40     extends SystemAggregator
41 {
42     private long value;
43     private boolean isCountStar;
44
45     /**
46      */

47     public void setup(String JavaDoc aggregateName)
48     {
49         isCountStar = aggregateName.equals("COUNT(*)");
50     }
51
52     /**
53      * @see ExecAggregator#merge
54      *
55      * @exception StandardException on error
56      */

57     public void merge(ExecAggregator addend)
58         throws StandardException
59     {
60         if (SanityManager.DEBUG)
61         {
62             SanityManager.ASSERT(addend instanceof CountAggregator,
63                 "addend is supposed to be the same type of aggregator for the merge operator");
64         }
65
66         value += ((CountAggregator)addend).value;
67     }
68
69     /**
70      * Return the result of the aggregation. Just
71      * spit out the running count.
72      *
73      * @return the value as a Long
74      */

75     public DataValueDescriptor getResult()
76     {
77         return new org.apache.derby.iapi.types.SQLLongint(value);
78     }
79
80
81     /**
82      * Accumulate for count(). Toss out all nulls in this kind of count.
83      * Increment the count for count(*). Count even the null values.
84      *
85      * @param addend value to be added in
86      * @param ga the generic aggregator that is calling me
87      *
88      * @see ExecAggregator#accumulate
89      */

90     public void accumulate(DataValueDescriptor addend, Object JavaDoc ga)
91         throws StandardException
92     {
93         if (isCountStar)
94             value++;
95         else
96             super.accumulate(addend, ga);
97     }
98
99     protected final void accumulate(DataValueDescriptor addend) {
100             value++;
101     }
102
103     /**
104      * @return ExecAggregator the new aggregator
105      */

106     public ExecAggregator newAggregator()
107     {
108         CountAggregator ca = new CountAggregator();
109         ca.isCountStar = isCountStar;
110         return ca;
111     }
112
113     public boolean isCountStar()
114     {
115         return isCountStar;
116     }
117
118     /////////////////////////////////////////////////////////////
119
//
120
// EXTERNALIZABLE INTERFACE
121
//
122
/////////////////////////////////////////////////////////////
123
/**
124      * Although we are not expected to be persistent per se,
125      * we may be written out by the sorter temporarily. So
126      * we need to be able to write ourselves out and read
127      * ourselves back in.
128      *
129      * @exception IOException thrown on error
130      */

131     public final void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
132     {
133         super.writeExternal(out);
134         out.writeBoolean(isCountStar);
135         out.writeLong(value);
136     }
137
138     /**
139     * @see java.io.Externalizable#readExternal
140     *
141     * @exception IOException io exception
142     * @exception ClassNotFoundException on error
143     */

144     public final void readExternal(ObjectInput JavaDoc in)
145         throws IOException JavaDoc, ClassNotFoundException JavaDoc
146     {
147         super.readExternal(in);
148         isCountStar = in.readBoolean();
149         value = in.readLong();
150     }
151     /////////////////////////////////////////////////////////////
152
//
153
// FORMATABLE INTERFACE
154
//
155
/////////////////////////////////////////////////////////////
156
/**
157      * Get the formatID which corresponds to this class.
158      *
159      * @return the formatID of this class
160      */

161     public int getTypeFormatId() { return StoredFormatIds.AGG_COUNT_V01_ID; }
162 }
163
Popular Tags