+/EE360P

distributed systems

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

vijaygarg1init07ca65d

master
4.2 KiB126 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.math;
19
20import org.apache.hadoop.examples.pi.Util;
21
22/** An arithmetic progression */
23public class ArithmeticProgression implements Comparable<ArithmeticProgression> {
24  /** A symbol */
25  public final char symbol;
26  /** Starting value */
27  public final long value;
28  /** Difference between terms */
29  public final long delta;
30  /** Ending value */
31  public final long limit;
32
33  /** Constructor */
34  public ArithmeticProgression(char symbol, long value, long delta, long limit) {
35    if (delta == 0)
36      throw new IllegalArgumentException("delta == 0");
37
38    this.symbol = symbol;
39    this.value = value;
40    this.delta = delta;
41    this.limit = limit;
42  }
43
44  /** {@inheritDoc} */
45  @Override
46  public boolean equals(Object obj) {
47    if (this == obj)
48      return true;
49    else if (obj != null && obj instanceof ArithmeticProgression) {
50      final ArithmeticProgression that = (ArithmeticProgression)obj;
51      if (this.symbol != that.symbol)
52        throw new IllegalArgumentException("this.symbol != that.symbol, this="
53            + this + ", that=" + that);
54      return this.value == that.value
55          && this.delta == that.delta
56          && this.limit == that.limit;
57    }
58    throw new IllegalArgumentException(obj == null? "obj == null":
59      "obj.getClass()=" + obj.getClass());
60  }
61
62  /** Not supported */
63  public int hashCode() {
64    throw new UnsupportedOperationException();
65  }
66
67  /** {@inheritDoc} */
68  @Override
69  public int compareTo(ArithmeticProgression that) {
70    if (this.symbol != that.symbol)
71      throw new IllegalArgumentException("this.symbol != that.symbol, this="
72          + this + ", that=" + that);
73    if (this.delta != that.delta)
74      throw new IllegalArgumentException("this.delta != that.delta, this="
75          + this + ", that=" + that);
76    final long d = this.limit - that.limit;
77    return d > 0? 1: d == 0? 0: -1;
78  }
79
80  /** Does this contain that? */
81  boolean contains(ArithmeticProgression that) {
82    if (this.symbol != that.symbol)
83      throw new IllegalArgumentException("this.symbol != that.symbol, this="
84          + this + ", that=" + that);
85    if (this.delta == that.delta) {
86      if (this.value == that.value)
87        return this.getSteps() >= that.getSteps();
88      else if (this.delta < 0)
89        return this.value > that.value && this.limit <= that.limit;
90      else if (this.delta > 0)
91        return this.value < that.value && this.limit >= that.limit;
92    }
93    return false;    
94  }
95
96  /** Skip some steps */
97  long skip(long steps) {
98    if (steps < 0)
99      throw new IllegalArgumentException("steps < 0, steps=" + steps);
100    return value + steps*delta; 
101  }
102
103  /** Get the number of steps */
104  public long getSteps() {
105    return (limit - value)/delta;
106  }
107
108  /** {@inheritDoc} */
109  @Override
110  public String toString() {
111    return symbol + ":value=" + value + ",delta=" + delta + ",limit=" + limit;
112  }
113
114  /** Convert a String to an ArithmeticProgression. */
115  static ArithmeticProgression valueOf(final String s) {
116    int i = 2;
117    int j = s.indexOf(",delta=");
118    final long value = Util.parseLongVariable("value", s.substring(2, j));
119    i = j + 1;
120    j = s.indexOf(",limit=");
121    final long delta = Util.parseLongVariable("delta", s.substring(i, j));
122    i = j + 1;
123    final long limit = Util.parseLongVariable("limit", s.substring(i));
124    return new ArithmeticProgression(s.charAt(0), value, delta, limit);
125  }
126}