private boolean isInvalid() { for (int i = 0; i < position.length; i++) if (position[i] < parentSpace.min(i) || position[i] > parentSpace.max(i)) { return true; } return false; }
/** * Moves a given dimension of the {@link Position} by a given delta. Throws an exception if delta * would move {@link Position} outside its parent {@link Extents}. */ @Override public void move(final long delta, final int dim) { if (this.isInvalid) { throw new IllegalArgumentException("Cannot move position : it is uninitialized"); } final long newValue = position[dim] + delta; if (newValue < parentSpace.min(dim) || newValue > parentSpace.max(dim)) { throw new IllegalArgumentException( "specified move would take position outside defined extents"); } position[dim] = newValue; }
/** * Moves the {@link Position} forward one step in specified dimension. Throws an exception if * specified move would take position outside parent {@link Extents}. */ @Override public void fwd(final int d) { if (this.isInvalid) { throw new IllegalArgumentException("Cannot move position : it is uninitialized"); } final long newValue = position[d] + 1; if (newValue > parentSpace.max(d)) { throw new IllegalArgumentException( "cannot move specified dimension forward -" + " it would take position outside defined extents"); } position[d]++; }
/** * Moves the {@link Position} backward by one step. Decrements the dimension positions from left * to right. * * @throws IllegalStateException if called from first position. */ public void bck() { if (this.isInvalid) { last(); return; } for (int i = 0; i < position.length; i++) { position[i]--; if (position[i] >= parentSpace.min(i)) return; position[i] = parentSpace.max(i); } first(); // reset position to where it was throw new IllegalStateException("cannot move first position backward"); }
/** * Moves the {@link Position} forward by one step. Increments the dimension positions from left to * right. * * @throws IllegalStateException if called from last position. */ @Override public void fwd() { if (this.isInvalid) { first(); return; } for (int i = 0; i < position.length; i++) { position[i]++; if (position[i] <= parentSpace.max(i)) return; position[i] = parentSpace.min(i); } last(); // reset position to where it was throw new IllegalStateException("cannot move last position forward"); }
/** * Sets the value of the {@link Position} for a given dimension. Throws an exception if the given * value is outside the bounds of the parent {@link Extents}. */ @Override public void setPosition(final long value, final int dim) { final long min = parentSpace.min(dim); if (value < min) { throw new IllegalArgumentException( "invalid position for dimension #" + dim + ": " + value + " < " + min); } final long max = parentSpace.max(dim); if (value > max) { throw new IllegalArgumentException( "invalid position for dimension #" + dim + ": " + value + " > " + max); } position[dim] = value; if (this.isInvalid) this.isInvalid = isInvalid(); }
/** Returns true if position can be moved forward (i.e. position is not in the last position). */ @Override public boolean hasNext() { if (this.isInvalid && position.length > 0) return true; for (int i = 0; i < position.length; i++) if (position[i] < parentSpace.max(i)) return true; return false; }
/** Sets the {@link Position} to its last state (all dimension positions to max-1) */ public void last() { for (int i = 0; i < position.length; i++) position[i] = parentSpace.max(i); this.isInvalid = false; }