+/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 .examples .pi .math .Summation ; 25import org .apache .hadoop .io .Writable ; 26 27/** A class for map task results or reduce task results. */ 28public class TaskResult implements Container <Summation >,Combinable <TaskResult >,Writable { 29private Summation sigma ; 30private long duration ; 31 32public TaskResult () {} 33 34TaskResult (Summation sigma ,long duration ) { 35this .sigma =sigma ; 36this .duration =duration ; 37 } 38 39/** {@inheritDoc} */ 40@ Override 41public Summation getElement () {return sigma ;} 42 43/** @return The time duration used */ 44long getDuration () {return duration ;} 45 46/** {@inheritDoc} */ 47@ Override 48public int compareTo (TaskResult that ) { 49return this .sigma .compareTo (that .sigma ); 50 } 51 52/** {@inheritDoc} */ 53@ Override 54public boolean equals (Object obj ) { 55if (this ==obj ) 56return true ; 57else if (obj !=null &&obj instanceof TaskResult ) { 58final TaskResult that = (TaskResult )obj ; 59return this .compareTo (that ) ==0 ; 60 } 61throw new IllegalArgumentException (obj ==null ?"obj == null" : 62"obj.getClass()=" +obj .getClass ()); 63 } 64 65/** Not supported */ 66@ Override 67public int hashCode () { 68throw new UnsupportedOperationException (); 69 } 70 71/** {@inheritDoc} */ 72@ Override 73public TaskResult combine (TaskResult that ) { 74final Summation s =sigma .combine (that .sigma ); 75return s ==null ?null :new TaskResult (s ,this .duration +that .duration ); 76 } 77 78/** {@inheritDoc} */ 79@ Override 80public void readFields (DataInput in )throws IOException { 81sigma =SummationWritable .read (in ); 82duration =in .readLong (); 83 } 84 85/** {@inheritDoc} */ 86@ Override 87public void write (DataOutput out )throws IOException { 88SummationWritable .write (sigma ,out ); 89out .writeLong (duration ); 90 } 91 92/** {@inheritDoc} */ 93@ Override 94public String toString () { 95return "sigma=" +sigma +", duration=" +duration +"(" +Util .millis2String (duration ) +")" ; 96 } 97 98/** Covert a String to a TaskResult */ 99public static TaskResult valueOf (String s ) { 100int i =0 ; 101int j =s .indexOf (", duration=" ); 102if (j <0 ) 103throw new IllegalArgumentException ("i=" +i +", j=" +j +" < 0, s=" +s ); 104final Summation sigma =Summation .valueOf (Util .parseStringVariable ("sigma" ,s .substring (i ,j ))); 105 106i =j +2 ; 107j =s .indexOf ("(" ,i ); 108if (j <0 ) 109throw new IllegalArgumentException ("i=" +i +", j=" +j +" < 0, s=" +s ); 110final long duration =Util .parseLongVariable ("duration" ,s .substring (i ,j )); 111 112return new TaskResult (sigma ,duration ); 113 } 114}