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.DoubleTypeOps;
26import org.apache.giraph.types.ops.IntTypeOps;
27import org.apache.giraph.types.ops.LongTypeOps;
28import org.apache.giraph.types.ops.NumericTypeOps;
29import org.apache.giraph.types.ops.TypeOpsUtils;
30import org.apache.hadoop.io.DoubleWritable;
31import org.apache.hadoop.io.IntWritable;
32import org.apache.hadoop.io.LongWritable;
33import org.apache.hadoop.io.Writable;
3435/**36 * Reducer for calculating sum of values37 * @param <T> Value type38 */39publicclass SumReduce<T extends Writable>
40extends ReduceSameTypeOperation<T> {
41/** DoubleWritable specialization */42publicstaticfinal SumReduce<DoubleWritable> DOUBLE =
43new SumReduce<>(DoubleTypeOps.INSTANCE);
44/** LongWritable specialization */45publicstaticfinal SumReduce<LongWritable> LONG =
46new SumReduce<>(LongTypeOps.INSTANCE);
47/** IntWritable specialization */48publicstaticfinal SumReduce<IntWritable> INT =
49new SumReduce<>(IntTypeOps.INSTANCE);
5051/** Value type operations */52private NumericTypeOps<T> typeOps;
5354/** Constructor used for deserialization only */55publicSumReduce() {
56 }
5758/**59 * Constructor60 * @param typeOps Value type operations61 */62publicSumReduce(NumericTypeOps<T> typeOps) {
63this.typeOps = typeOps;
64 }
6566 @Override
67public T createInitialValue() {
68return typeOps.createZero();
69 }
7071 @Override
72public T reduce(T curValue, T valueToReduce) {
73 typeOps.plusInto(curValue, valueToReduce);
74return curValue;
75 }
7677 @Override
78publicvoid write(DataOutput out) throws IOException {
79 TypeOpsUtils.writeTypeOps(typeOps, out);
80 }
8182 @Override
83publicvoid readFields(DataInput in) throws IOException {
84 typeOps = TypeOpsUtils.readTypeOps(in);
85 }
86 }