Integer Sequence Generator : Range « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Range 




Integer Sequence Generator
      
//package org.hh.jga.util;

import java.util.Iterator;


/**
 * Integer Sequence Generator
 
 @author Hong Hong
 *
 */
public abstract class Range<T extends Number> implements Iterable<T> {
  public static Range<Integer> range(int from, int to, int step) {
    return new IntRange(from, to, step);
  }
  public static Range<Integer> range(int from, int to) {
    return new IntRange(from, to, to >= from : -1);
  }
  public static Range<Integer> range(int to) {
    return new IntRange(0, to, 1);
  }
  public static Range<Short> range(short from, short to, short step) {
    return new ShortRange(from, to, step);
  }
  public static Range<Short> range(short from, short to) {
    return new ShortRange(from, to, (short)(to >= from : -1));
  }
  public static Range<Short> range(short to) {
    return new ShortRange((short)0, to, (short)1);
  }
  public static Range<Long> range(long from, long to, long end) {
    return new LongRange(from, to, end);
  }
  public static Range<Long> range(long from, long to) {
    return new LongRange(from, to, to >= from 1L : -1L);
  }
  public static Range<Long> range(Long to) {
    return new LongRange(0L, to, 1L);
  }
  protected static class IntRange extends Range<Integer> implements Iterator<Integer> {
    protected int m_cur, m_step, m_end;
    public IntRange(int from, int to, int step) {
      m_cur = from;
      m_step = step;
      m_end = to;
    }
    public boolean hasNext() {
      return m_step > ? m_cur < m_end : m_end < m_cur;
    }

    public Integer next() {
      int res = m_cur;
      m_cur += m_step;
      return res;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }

    public Iterator<Integer> iterator() {
      return this;
    }
    
  }
  protected static class LongRange extends Range<Long> implements Iterator<Long> {
    protected long m_cur, m_step, m_end;
    public LongRange(long from, long to, long step) {
      m_cur = from;
      m_step = step;
      m_end = to;
    }
    public boolean hasNext() {
      return m_step > ? m_cur < m_end : m_end < m_cur;
    }

    public Long next() {
      long res = m_cur;
      m_cur += m_step;
      return res;
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }

    public Iterator<Long> iterator() {
      return this;
    }
  }

  protected static class ShortRange extends Range<Short> implements Iterator<Short> {
    protected short m_cur, m_step, m_end;
    public ShortRange(short from, short to, short step) {
      m_cur = from;
      m_step = step;
      m_end = to;
    }
    public boolean hasNext() {
      return m_step > ? m_cur < m_end : m_end < m_cur;
    }

    public Short next() {
      short res = m_cur;
      m_cur += m_step;
      return res;
    }
    public void remove() {
      throw new UnsupportedOperationException();
    }

    public Iterator<Short> iterator() {
      return this;
    }
  }
}

   
    
    
    
    
    
  














Related examples in the same category
1.Represents a sequence of integer values, either ascending or descending.
2.This constructs an Iterator over each day in a date range defined by a focus date and range style.
3.Finds the value in the range (start,limit) of the largest element (rank) where the count of all smaller elements in that range is less than or equals target.
4.IntRange represents an inclusive range of ints.
5.LongRange represents an inclusive range of longs.
6.Byte Range
7.NumberRange represents an inclusive range of java.lang.Number objects of the same type.
8.Represents a range of Number objects.
9.A numeric range has a high, low, mean, root-mean-square, standard deviation, and the count of how many samples it contains.
10.A range of integers.
11.Value Range generic structure
12.Long Range
13.Class for storing start and end integer offsets.
14.A numerical interval
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.