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.combiner;
1920import org.apache.giraph.types.ops.DoubleTypeOps;
21import org.apache.giraph.types.ops.FloatTypeOps;
22import org.apache.giraph.types.ops.IntTypeOps;
23import org.apache.giraph.types.ops.LongTypeOps;
24import org.apache.giraph.types.ops.NumericTypeOps;
25import org.apache.hadoop.io.DoubleWritable;
26import org.apache.hadoop.io.FloatWritable;
27import org.apache.hadoop.io.IntWritable;
28import org.apache.hadoop.io.LongWritable;
29import org.apache.hadoop.io.Writable;
30import org.apache.hadoop.io.WritableComparable;
3132/**33 * Message combiner which sums all messages.34 *35 * @param <M> Message type36 */37publicclass SumMessageCombiner<M extends Writable>
38implements MessageCombiner<WritableComparable, M> {
39/** DoubleWritable specialization */40publicstaticfinal SumMessageCombiner<DoubleWritable> DOUBLE =
41new SumMessageCombiner<>(DoubleTypeOps.INSTANCE);
42/** DoubleWritable specialization */43publicstaticfinal SumMessageCombiner<FloatWritable> FLOAT =
44new SumMessageCombiner<>(FloatTypeOps.INSTANCE);
45/** LongWritable specialization */46publicstaticfinal SumMessageCombiner<LongWritable> LONG =
47new SumMessageCombiner<>(LongTypeOps.INSTANCE);
48/** IntWritable specialization */49publicstaticfinal SumMessageCombiner<IntWritable> INT =
50new SumMessageCombiner<>(IntTypeOps.INSTANCE);
5152/** Value type operations */53privatefinal NumericTypeOps<M> typeOps;
5455/**56 * Constructor57 * @param typeOps Value type operations58 */59publicSumMessageCombiner(NumericTypeOps<M> typeOps) {
60this.typeOps = typeOps;
61 }
6263 @Override
64publicvoid combine(
65 WritableComparable vertexIndex, M originalMessage, M messageToCombine) {
66 typeOps.plusInto(originalMessage, messageToCombine);
67 }
6869 @Override
70public M createInitialMessage() {
71return typeOps.createZero();
72 }
73 }