+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
07ca65d
master
1/** 2* Licensed to the Apache Software Foundation (ASF) under one 3* or more contributor license agreements. See the NOTICE file 4* distributed with this work for additional information 5* regarding copyright ownership. The ASF licenses this file 6* to you under the Apache License, Version 2.0 (the 7* "License"); you may not use this file except in compliance 8* with the License. You may obtain a copy of the License at 9* 10* http://www.apache.org/licenses/LICENSE-2.0 11* 12* Unless required by applicable law or agreed to in writing, software 13* 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 and 16* limitations under the License. 17*/ 18package org .apache .hadoop .examples .pi ; 19 20import java .io .DataInput ; 21import java .io .DataOutput ; 22import java .io .IOException ; 23 24import org .apache .hadoop .conf .Configuration ; 25import org .apache .hadoop .examples .pi .math .ArithmeticProgression ; 26import org .apache .hadoop .examples .pi .math .Summation ; 27import org .apache .hadoop .io .WritableComparable ; 28 29/** A Writable class for Summation */ 30public final class SummationWritable implements WritableComparable <SummationWritable >,Container <Summation > { 31private Summation sigma ; 32 33public SummationWritable () {} 34 35SummationWritable (Summation sigma ) {this .sigma =sigma ;} 36 37/** {@inheritDoc} */ 38@ Override 39public String toString () {return getClass ().getSimpleName () +sigma ;} 40 41/** {@inheritDoc} */ 42@ Override 43public Summation getElement () {return sigma ;} 44 45/** Read sigma from conf */ 46public static Summation read (Class <?>clazz ,Configuration conf ) { 47return Summation .valueOf (conf .get (clazz .getSimpleName () +".sigma" )); 48 } 49 50/** Write sigma to conf */ 51public static void write (Summation sigma ,Class <?>clazz ,Configuration conf ) { 52conf .set (clazz .getSimpleName () +".sigma" ,sigma .toString ()); 53 } 54 55/** Read Summation from DataInput */ 56static Summation read (DataInput in )throws IOException { 57final SummationWritable s =new SummationWritable (); 58s .readFields (in ); 59return s .getElement (); 60 } 61 62/** {@inheritDoc} */ 63@ Override 64public void readFields (DataInput in )throws IOException { 65final ArithmeticProgression N =ArithmeticProgressionWritable .read (in ); 66final ArithmeticProgression E =ArithmeticProgressionWritable .read (in ); 67sigma =new Summation (N ,E ); 68 69if (in .readBoolean ()) { 70sigma .setValue (in .readDouble ()); 71 } 72 } 73 74/** Write sigma to DataOutput */ 75public static void write (Summation sigma ,DataOutput out )throws IOException { 76ArithmeticProgressionWritable .write (sigma .N ,out ); 77ArithmeticProgressionWritable .write (sigma .E ,out ); 78 79final Double v =sigma .getValue (); 80if (v ==null ) 81out .writeBoolean (false ); 82else { 83out .writeBoolean (true ); 84out .writeDouble (v ); 85 } 86 } 87 88/** {@inheritDoc} */ 89@ Override 90public void write (DataOutput out )throws IOException { 91write (sigma ,out ); 92 } 93 94/** {@inheritDoc} */ 95@ Override 96public int compareTo (SummationWritable that ) { 97return this .sigma .compareTo (that .sigma ); 98 } 99 100/** {@inheritDoc} */ 101@ Override 102public boolean equals (Object obj ) { 103if (this ==obj ) 104return true ; 105else if (obj !=null &&obj instanceof SummationWritable ) { 106final SummationWritable that = (SummationWritable )obj ; 107return this .compareTo (that ) ==0 ; 108 } 109throw new IllegalArgumentException (obj ==null ?"obj == null" : 110"obj.getClass()=" +obj .getClass ()); 111 } 112 113/** Not supported */ 114@ Override 115public int hashCode () { 116throw new UnsupportedOperationException (); 117 } 118 119/** A writable class for ArithmeticProgression */ 120private static class ArithmeticProgressionWritable { 121/** Read ArithmeticProgression from DataInput */ 122private static ArithmeticProgression read (DataInput in )throws IOException { 123return new ArithmeticProgression (in .readChar (), 124in .readLong (),in .readLong (),in .readLong ()); 125 } 126 127/** Write ArithmeticProgression to DataOutput */ 128private static void write (ArithmeticProgression ap ,DataOutput out 129 )throws IOException { 130out .writeChar (ap .symbol ); 131out .writeLong (ap .value ); 132out .writeLong (ap .delta ); 133out .writeLong (ap .limit ); 134 } 135 } 136}