001// --- BEGIN LICENSE BLOCK ---
002/* 
003 * Copyright (c) 2009, Mikio L. Braun
004 * All rights reserved.
005 * 
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are
008 * met:
009 * 
010 *     * Redistributions of source code must retain the above copyright
011 *       notice, this list of conditions and the following disclaimer.
012 * 
013 *     * Redistributions in binary form must reproduce the above
014 *       copyright notice, this list of conditions and the following
015 *       disclaimer in the documentation and/or other materials provided
016 *       with the distribution.
017 * 
018 *     * Neither the name of the Technische Universität Berlin nor the
019 *       names of its contributors may be used to endorse or promote
020 *       products derived from this software without specific prior
021 *       written permission.
022 * 
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034 */
035// --- END LICENSE BLOCK ---
036
037/*
038 * To change this template, choose Tools | Templates
039 * and open the template in the editor.
040 */
041package org.jblas.ranges;
042
043/**
044 * Range which varies from a given interval. Endpoint is exclusive!
045 * <p/>
046 * "new IntervalRange(0, 3)" enumerates 0, 1, 2.
047 */
048public class IntervalRange implements Range {
049  private int start;
050  private int end;
051  private int value;
052
053  /**
054   * Construct new interval range. Endpoints are inclusive.
055   */
056  public IntervalRange(int a, int b) {
057    start = a;
058    end = b;
059  }
060
061  public void init(int lower, int upper) {
062    value = start;
063    if (start < lower || end > upper + 1) {
064      throw new IllegalArgumentException("Bounds " + lower + " to " + upper + " are beyond range interval " + start + " to " + end + ".");
065    }
066  }
067
068  public int length() {
069    return end - start;
070  }
071
072  public void next() {
073    value++;
074  }
075
076  public int index() {
077    return value - start;
078  }
079
080  public int value() {
081    return value;
082  }
083
084  public boolean hasMore() {
085    return value < end;
086  }
087
088  @Override
089  public String toString() {
090    return String.format("<Interval Range from %d to %d, length %d index=%d value=%d>", start, end, length(), index(), value());
091  }
092}