001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.MaxMinAggregator
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025: import org.apache.derby.iapi.types.DataValueDescriptor;
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.sql.execute.ExecAggregator;
029: import org.apache.derby.iapi.services.io.StoredFormatIds;
030: import java.io.ObjectOutput;
031: import java.io.ObjectInput;
032: import java.io.IOException;
033:
034: /**
035: * Aggregator for MAX()/MIN(). Defers most of its work
036: * to OrderableAggregator.
037: *
038: * @see OrderableAggregator
039: *
040: * @author jamie
041: */
042: public final class MaxMinAggregator extends OrderableAggregator {
043:
044: private boolean isMax; // true for max, false for min
045:
046: /**
047: */
048: public void setup(String aggregateName) {
049: super .setup(aggregateName);
050: isMax = aggregateName.equals("MAX");
051: }
052:
053: /**
054: * Accumulate
055: *
056: * @param addend value to be added in
057: *
058: * @exception StandardException on error
059: *
060: */
061: protected void accumulate(DataValueDescriptor addend)
062: throws StandardException {
063: if ((value == null) || (isMax && (value.compare(addend) < 0))
064: || (!isMax && (value.compare(addend) > 0))) {
065: /* NOTE: We need to call getClone() since value gets
066: * reused underneath us
067: */
068: value = addend.getClone();
069: }
070: }
071:
072: /**
073: * @return ExecAggregator the new aggregator
074: */
075: public ExecAggregator newAggregator() {
076: MaxMinAggregator ma = new MaxMinAggregator();
077: ma.isMax = isMax;
078: return ma;
079: }
080:
081: /////////////////////////////////////////////////////////////
082: //
083: // FORMATABLE INTERFACE
084: //
085: /////////////////////////////////////////////////////////////
086: public void writeExternal(ObjectOutput out) throws IOException {
087: super .writeExternal(out);
088: out.writeBoolean(isMax);
089: }
090:
091: /**
092: * @see java.io.Externalizable#readExternal
093: *
094: * @exception IOException on error
095: * @exception ClassNotFoundException on error
096: */
097: public void readExternal(ObjectInput in) throws IOException,
098: ClassNotFoundException {
099: super .readExternal(in);
100: isMax = in.readBoolean();
101: }
102:
103: /**
104: * Get the formatID which corresponds to this class.
105: *
106: * @return the formatID of this class
107: */
108: public int getTypeFormatId() {
109: return StoredFormatIds.AGG_MAX_MIN_V01_ID;
110: }
111: }
|