1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.reducers.impl;
1920import java.io.DataInput;
21import java.io.DataOutput;
22import java.io.IOException;
2324import org.apache.giraph.reducers.ReduceSameTypeOperation;
25import org.apache.giraph.types.ops.TypeOps;
26import org.apache.giraph.types.ops.TypeOpsUtils;
27import org.apache.giraph.writable.tuple.PairWritable;
28import org.apache.hadoop.io.Writable;
29import org.apache.hadoop.io.WritableComparable;
303132/**33 * Aggregating PairWritable<L, R>, by taking pair with34 * largest second value.35 *36 * @param <L> Type of the left value37 * @param <R> Type of the right value38 */39publicclass MaxPairReducer<L extends Writable, R extends WritableComparable>
40extends ReduceSameTypeOperation<PairWritable<L, R>> {
4142/** Left value TypeOps */43private TypeOps<L> leftTypeOps;
44/** Right value TypeOps */45private TypeOps<R> rightTypeOps;
4647/** Constructor used for deserialization only */48publicMaxPairReducer() {
49 }
5051/**52 * Constructor53 * @param leftTypeOps Left value TypeOps54 * @param rightTypeOps Right value TypeOps55 */56publicMaxPairReducer(TypeOps<L> leftTypeOps, TypeOps<R> rightTypeOps) {
57this.leftTypeOps = leftTypeOps;
58this.rightTypeOps = rightTypeOps;
59 }
6061 @Override
62public PairWritable<L, R> reduce(
63 PairWritable<L, R> curValue, PairWritable<L, R> valueToReduce) {
64if (valueToReduce.getRight().compareTo(curValue.getRight()) > 0) {
65 leftTypeOps.set(curValue.getLeft(), valueToReduce.getLeft());
66 rightTypeOps.set(curValue.getRight(), valueToReduce.getRight());
67 }
68return curValue;
69 }
7071 @Override
72public PairWritable<L, R> createInitialValue() {
73returnnew PairWritable<L, R>(
74 leftTypeOps.create(), rightTypeOps.create());
75 }
7677 @Override
78publicvoid write(DataOutput out) throws IOException {
79 TypeOpsUtils.writeTypeOps(leftTypeOps, out);
80 TypeOpsUtils.writeTypeOps(rightTypeOps, out);
81 }
8283 @Override
84publicvoid readFields(DataInput in) throws IOException {
85 leftTypeOps = TypeOpsUtils.readTypeOps(in);
86 rightTypeOps = TypeOpsUtils.readTypeOps(in);
87 }
88 }