+/EE360P

distributed systems

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

vijaygarg1init07ca65d

master
4.2 KiB136 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.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> {
31  private Summation sigma;
32
33  public SummationWritable() {}
34  
35  SummationWritable(Summation sigma) {this.sigma = sigma;}
36
37  /** {@inheritDoc} */
38  @Override
39  public String toString() {return getClass().getSimpleName() + sigma;}
40
41  /** {@inheritDoc} */
42  @Override
43  public Summation getElement() {return sigma;}
44
45  /** Read sigma from conf */
46  public static Summation read(Class<?> clazz, Configuration conf) {
47    return Summation.valueOf(conf.get(clazz.getSimpleName() + ".sigma")); 
48  }
49
50  /** Write sigma to conf */
51  public static void write(Summation sigma, Class<?> clazz, Configuration conf) {
52    conf.set(clazz.getSimpleName() + ".sigma", sigma.toString());
53  }
54
55  /** Read Summation from DataInput */
56  static Summation read(DataInput in) throws IOException {
57    final SummationWritable s = new SummationWritable();
58    s.readFields(in);
59    return s.getElement();
60  }
61
62  /** {@inheritDoc} */
63  @Override
64  public void readFields(DataInput in) throws IOException {
65    final ArithmeticProgression N = ArithmeticProgressionWritable.read(in);
66    final ArithmeticProgression E = ArithmeticProgressionWritable.read(in);
67    sigma = new Summation(N, E); 
68
69    if (in.readBoolean()) {
70      sigma.setValue(in.readDouble());
71    }
72  }
73
74  /** Write sigma to DataOutput */
75  public static void write(Summation sigma, DataOutput out) throws IOException {
76    ArithmeticProgressionWritable.write(sigma.N, out);
77    ArithmeticProgressionWritable.write(sigma.E, out);
78
79    final Double v = sigma.getValue();
80    if (v == null)
81      out.writeBoolean(false);
82    else {
83      out.writeBoolean(true);
84      out.writeDouble(v);
85    }
86  }
87
88  /** {@inheritDoc} */
89  @Override
90  public void write(DataOutput out) throws IOException {
91    write(sigma, out);
92  }
93
94  /** {@inheritDoc} */
95  @Override
96  public int compareTo(SummationWritable that) {
97    return this.sigma.compareTo(that.sigma);
98  }
99
100  /** {@inheritDoc} */
101  @Override
102  public boolean equals(Object obj) {
103    if (this == obj)
104      return true;
105    else if (obj != null && obj instanceof SummationWritable) {
106      final SummationWritable that = (SummationWritable)obj;
107      return this.compareTo(that) == 0;
108    }
109    throw new IllegalArgumentException(obj == null? "obj == null":
110      "obj.getClass()=" + obj.getClass());
111  }
112
113  /** Not supported */
114  @Override
115  public int hashCode() {
116    throw new UnsupportedOperationException();
117  }
118
119  /** A writable class for ArithmeticProgression */
120  private static class ArithmeticProgressionWritable {
121    /** Read ArithmeticProgression from DataInput */
122    private static ArithmeticProgression read(DataInput in) throws IOException {
123      return new ArithmeticProgression(in.readChar(),
124          in.readLong(), in.readLong(), in.readLong());
125    }
126
127    /** Write ArithmeticProgression to DataOutput */
128    private static void write(ArithmeticProgression ap, DataOutput out
129        ) throws IOException {
130      out.writeChar(ap.symbol);
131      out.writeLong(ap.value);
132      out.writeLong(ap.delta);
133      out.writeLong(ap.limit);
134    }
135  }
136}