+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1init07ca65d

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