Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    private static final Iterable NULL_REPEATABLE = () -> new Iterator() {
        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public Object next() {
            return null;
        }
    };

    /**
     * @param value value to return on each iteration
     * @return iterable returning forever the passed in value
     */
    public static <T> Iterable<T> repeat(T value) {
        return repeat(value, -1);
    }

    /**
     * @param value value to return on each iteration
     * @param n specifies how often the value is repeated
     * @return iterable returning n times the passed in value
     */
    @SuppressWarnings("unchecked")
    public static <T> Iterable<T> repeat(T value, int n) {
        if (value == null) {
            return NULL_REPEATABLE;
        }

        return () -> new Iterator() {
            private int count = 0;

            @Override
            public boolean hasNext() {
                return n < 0 || count < n;
            }

            @Override
            public Object next() {
                count++;
                return value;
            }
        };
    }
}