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