
com.venky.core.math.Range Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common Show documentation
Show all versions of common Show documentation
Commonly used programming tasks in java
The newest version!
package com.venky.core.math;
import java.lang.reflect.ParameterizedType;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.function.Consumer;
public class Range implements Iterable{
AnyNumber start, end , current, step;
Class numberClass ;
public Range(T start, T end){
this(start,end,start);
}
public Range(T start, T end ,T current){
this(start,end,current, 1.0);
}
@SuppressWarnings("unchecked")
public Range(T start, T end ,T current, double step){
this.start = new AnyNumber<>(start);
this.end = new AnyNumber<>(end);
this.current = new AnyNumber<>(current);
this.numberClass = (Class)this.start.value().getClass();
this.step = AnyNumber.valueOf(step,numberClass);
}
private boolean isPresent(double value){
return start != null && end != null && start.doubleValue() <= value && end.doubleValue() >= value;
}
@Override
public Iterator iterator() {
return new Iterator() {
@Override
public boolean hasNext() {
return isPresent( current.doubleValue() );
}
@Override
public T next() {
if (!hasNext()){
throw new NoSuchElementException();
}
T o = current.value();
current = AnyNumber.valueOf(current.doubleValue() + step.doubleValue(), numberClass);
return o;
}
};
}
@Override
public void forEach(Consumer super T> action) {
Iterable.super.forEach(action);
}
@Override
public Spliterator spliterator() {
return Iterable.super.spliterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy