--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.execute.MaxMinAggregator\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.execute;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.sql.execute.ExecAggregator;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Aggregator for MAX()/MIN(). Defers most of its work\r
+ * to OrderableAggregator.\r
+ *\r
+ * @see OrderableAggregator\r
+ *\r
+ */\r
+public final class MaxMinAggregator \r
+ extends OrderableAggregator\r
+{\r
+\r
+ private boolean isMax; // true for max, false for min\r
+\r
+ /**\r
+ */\r
+ public void setup(String aggregateName)\r
+ {\r
+ super.setup(aggregateName);\r
+ isMax = aggregateName.equals("MAX");\r
+ }\r
+ /**\r
+ * Accumulate\r
+ *\r
+ * @param addend value to be added in\r
+ *\r
+ * @exception StandardException on error\r
+ *\r
+ */\r
+ protected void accumulate(DataValueDescriptor addend) \r
+ throws StandardException\r
+ {\r
+ if ( (value == null) ||\r
+ (isMax && (value.compare(addend) < 0)) ||\r
+ (!isMax && (value.compare(addend) > 0))\r
+ )\r
+ {\r
+ /* NOTE: We need to call getClone() since value gets\r
+ * reused underneath us\r
+ */\r
+ value = addend.getClone();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @return ExecAggregator the new aggregator\r
+ */\r
+ public ExecAggregator newAggregator()\r
+ {\r
+ MaxMinAggregator ma = new MaxMinAggregator();\r
+ ma.isMax = isMax;\r
+ return ma;\r
+ }\r
+\r
+ /////////////////////////////////////////////////////////////\r
+ // \r
+ // FORMATABLE INTERFACE\r
+ // \r
+ /////////////////////////////////////////////////////////////\r
+ public void writeExternal(ObjectOutput out) throws IOException\r
+ {\r
+ super.writeExternal(out);\r
+ out.writeBoolean(isMax);\r
+ }\r
+\r
+ /** \r
+ * @see java.io.Externalizable#readExternal \r
+ *\r
+ * @exception IOException on error\r
+ * @exception ClassNotFoundException on error\r
+ */\r
+ public void readExternal(ObjectInput in) \r
+ throws IOException, ClassNotFoundException {\r
+ super.readExternal(in);\r
+ isMax = in.readBoolean();\r
+ }\r
+ /**\r
+ * Get the formatID which corresponds to this class.\r
+ *\r
+ * @return the formatID of this class\r
+ */\r
+ public int getTypeFormatId() { return StoredFormatIds.AGG_MAX_MIN_V01_ID; }\r
+}\r