KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.OrderableAggregator
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.sql.execute.ExecAggregator;
26 import org.apache.derby.iapi.types.DataValueDescriptor;
27 import org.apache.derby.iapi.error.StandardException;
28 import org.apache.derby.iapi.services.io.Formatable;
29
30
31 import java.io.ObjectOutput JavaDoc;
32 import java.io.ObjectInput JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 /**
36  * Abstract aggregator for Orderable aggregates (max/min).
37  *
38  * @author jamie
39  */

40 abstract class OrderableAggregator extends SystemAggregator
41 {
42     protected DataValueDescriptor value;
43
44     /**
45      */

46     public void setup(String JavaDoc aggregateName)
47     {
48     }
49
50     /**
51      * @see ExecAggregator#merge
52      *
53      * @exception StandardException on error
54      */

55     public void merge(ExecAggregator addend)
56         throws StandardException
57     {
58         if (SanityManager.DEBUG)
59         {
60             SanityManager.ASSERT(addend instanceof OrderableAggregator,
61                 "addend is supposed to be the same type of aggregator for the merge operator");
62         }
63
64         // Don't bother merging if the other has never been used.
65
DataValueDescriptor bv = ((OrderableAggregator)addend).value;
66         if (bv != null)
67             this.accumulate(bv);
68     }
69
70     /**
71      * Return the result of the operations that we
72      * have been performing. Returns a DataValueDescriptor.
73      *
74      * @return the result as a DataValueDescriptor
75      */

76     public DataValueDescriptor getResult() throws StandardException
77     {
78         return value;
79     }
80
81     /////////////////////////////////////////////////////////////
82
//
83
// EXTERNALIZABLE INTERFACE
84
//
85
/////////////////////////////////////////////////////////////
86
/**
87      * Although we are not expected to be persistent per se,
88      * we may be written out by the sorter temporarily. So
89      * we need to be able to write ourselves out and read
90      * ourselves back in. We rely on formatable to handle
91      * situations where <I>value</I> is null.
92      * <p>
93      * Why would we be called to write ourselves out if we
94      * are null? For scalar aggregates, we don't bother
95      * setting up the aggregator since we only need a single
96      * row. So for a scalar aggregate that needs to go to
97      * disk, the aggregator might be null.
98      *
99      * @exception IOException on error
100      *
101      * @see java.io.Externalizable#writeExternal
102      */

103     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
104     {
105         super.writeExternal(out);
106         out.writeObject(value);
107     }
108
109     /**
110      * @see java.io.Externalizable#readExternal
111      *
112      * @exception IOException on error
113      * @exception ClassNotFoundException on error
114      */

115     public void readExternal(ObjectInput JavaDoc in)
116         throws IOException JavaDoc, ClassNotFoundException JavaDoc
117     {
118         super.readExternal(in);
119         value = (DataValueDescriptor) in.readObject();
120     }
121 }
122
Popular Tags