Java Iterable Count count(final Iterable iterable, final int step)

Here you can find the source of count(final Iterable iterable, final int step)

Description

count

License

Open Source License

Declaration

public static <T> Iterable<Integer> count(final Iterable<T> iterable, final int step) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    public static Iterable<Integer> count(final int min, final int max, final int step) {
        Iterable<Integer> generator = new Iterable<Integer>() {
            public Iterator<Integer> iterator() {
                return new Iterator<Integer>() {
                    int currentValue = min - step;

                    @Override//from w  ww .  j  a  va2 s. c o  m
                    public boolean hasNext() {
                        if (currentValue < max - 1) {
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public Integer next() {
                        currentValue += step;
                        return currentValue;
                    }

                    @Override
                    public void remove() {

                    }

                };
            }
        };
        return generator;
    }

    public static <T> Iterable<Integer> count(final Iterable<T> iterable, final int step) {
        Iterable<Integer> generator = new Iterable<Integer>() {
            public Iterator<Integer> iterator() {
                return new Iterator<Integer>() {
                    Iterator<T> iter = iterable.iterator();
                    int currentValue = 0 - step;

                    @Override
                    public boolean hasNext() {
                        return iter.hasNext();
                    }

                    @Override
                    public Integer next() {
                        currentValue += step;
                        for (Integer i : countTo(step)) {
                            currentValue += i;
                            iter.next();
                        }
                        return currentValue;
                    }

                    @Override
                    public void remove() {

                    }

                };
            }
        };
        return generator;
    }

    public static Iterable<Integer> countTo(int max) {
        return count(0, max, 1);
    }
}

Related

  1. count(Iterable iterable)
  2. count(Iterable i)
  3. count(Iterable iterable)
  4. count(Iterable iterable)