KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.SumAggregator
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.services.sanity.SanityManager;
25 import org.apache.derby.iapi.types.NumberDataValue;
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.sql.execute.ExecAggregator;
28 import org.apache.derby.iapi.types.DataValueDescriptor;
29
30 import org.apache.derby.iapi.services.io.StoredFormatIds;
31
32 /**
33  * Aggregator for SUM(). Defers most of its work
34  * to OrderableAggregator.
35  *
36  * @author jamie
37  */

38 public class SumAggregator
39     extends OrderableAggregator
40 {
41     /**
42      * Accumulate
43      *
44      * @param addend value to be added in
45      *
46      * @exception StandardException on error
47      *
48      * @see ExecAggregator#accumulate
49      */

50     protected void accumulate(DataValueDescriptor addend)
51         throws StandardException
52     {
53
54         /*
55         ** If we don't have any value yet, just clone
56         ** the addend.
57         */

58         if (value == null)
59         {
60             /* NOTE: We need to call getClone() since value gets
61              * reused underneath us
62              */

63             value = addend.getClone();
64         }
65         else
66         {
67             NumberDataValue input = (NumberDataValue)addend;
68             NumberDataValue nv = (NumberDataValue) value;
69
70             value = nv.plus(
71                         input, // addend 1
72
nv, // addend 2
73
nv); // result
74
}
75     }
76
77     /**
78      * @return ExecAggregator the new aggregator
79      */

80     public ExecAggregator newAggregator()
81     {
82         return new SumAggregator();
83     }
84
85     ////////////////////////////////////////////////////////////
86
//
87
// FORMATABLE INTERFACE
88
//
89
/////////////////////////////////////////////////////////////
90
/**
91      * Get the formatID which corresponds to this class.
92      *
93      * @return the formatID of this class
94      */

95     public int getTypeFormatId() { return StoredFormatIds.AGG_SUM_V01_ID; }
96 }
97
Popular Tags