KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.MaxMinAggregator
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.DataValueDescriptor;
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 java.io.ObjectOutput JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * Aggregator for MAX()/MIN(). Defers most of its work
36  * to OrderableAggregator.
37  *
38  * @see OrderableAggregator
39  *
40  * @author jamie
41  */

42 public final class MaxMinAggregator
43     extends OrderableAggregator
44 {
45
46     private boolean isMax; // true for max, false for min
47

48     /**
49      */

50     public void setup(String JavaDoc aggregateName)
51     {
52         super.setup(aggregateName);
53         isMax = aggregateName.equals("MAX");
54     }
55     /**
56      * Accumulate
57      *
58      * @param addend value to be added in
59      *
60      * @exception StandardException on error
61      *
62      */

63     protected void accumulate(DataValueDescriptor addend)
64         throws StandardException
65     {
66         if ( (value == null) ||
67                   (isMax && (value.compare(addend) < 0)) ||
68                   (!isMax && (value.compare(addend) > 0))
69                   )
70         {
71             /* NOTE: We need to call getClone() since value gets
72              * reused underneath us
73              */

74             value = addend.getClone();
75         }
76     }
77
78     /**
79      * @return ExecAggregator the new aggregator
80      */

81     public ExecAggregator newAggregator()
82     {
83         MaxMinAggregator ma = new MaxMinAggregator();
84         ma.isMax = isMax;
85         return ma;
86     }
87
88     /////////////////////////////////////////////////////////////
89
//
90
// FORMATABLE INTERFACE
91
//
92
/////////////////////////////////////////////////////////////
93
public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
94     {
95         super.writeExternal(out);
96         out.writeBoolean(isMax);
97     }
98
99     /**
100      * @see java.io.Externalizable#readExternal
101      *
102      * @exception IOException on error
103      * @exception ClassNotFoundException on error
104      */

105     public void readExternal(ObjectInput JavaDoc in)
106         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
107         super.readExternal(in);
108         isMax = in.readBoolean();
109     }
110     /**
111      * Get the formatID which corresponds to this class.
112      *
113      * @return the formatID of this class
114      */

115     public int getTypeFormatId() { return StoredFormatIds.AGG_MAX_MIN_V01_ID; }
116 }
117
Popular Tags