Java Array to Iterable toIterable(final boolean[] values)

Here you can find the source of toIterable(final boolean[] values)

Description

Converts an array of boolean values into an Iterable of Boolean objects.

License

Open Source License

Parameter

Parameter Description
values the array of <code>boolean</code>s

Return

an Iterable of Booleans

Declaration

public static Iterable<Boolean> toIterable(final boolean[] values) 

Method Source Code

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

import java.util.Iterator;

public class Main {
    /**/*from w ww  .ja va2s . c  o m*/
     * Converts an array of <code>boolean</code> values into an
     * <code>Iterable</code> of <code>Boolean</code> objects.
     *
     * @param values the array of <code>boolean</code>s
     * @return an <code>Iterable</code> of <code>Boolean</code>s
     */
    public static Iterable<Boolean> toIterable(final boolean[] values) {
        return new Iterable<Boolean>() {
            @Override
            public Iterator<Boolean> iterator() {
                return new Iterator<Boolean>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Boolean next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>byte</code> values into an
     * <code>Iterable</code> of <code>Byte</code> objects.
     *
     * @param values the array of <code>byte</code>s
     * @return an <code>Iterable</code> of <code>Byte</code>s
     */
    public static Iterable<Byte> toIterable(final byte[] values) {
        return new Iterable<Byte>() {
            @Override
            public Iterator<Byte> iterator() {
                return new Iterator<Byte>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Byte next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>char</code> values into an
     * <code>Iterable</code> of <code>Character</code> objects.
     *
     * @param values the array of <code>char</code>s
     * @return an <code>Iterable</code> of <code>Character</code>s
     */
    public static Iterable<Character> toIterable(final char[] values) {
        return new Iterable<Character>() {
            @Override
            public Iterator<Character> iterator() {
                return new Iterator<Character>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Character next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>short</code> values into an
     * <code>Iterable</code> of <code>Short</code> objects.
     *
     * @param values the array of <code>short</code>s
     * @return an <code>Iterable</code> of <code>Short</code>s
     */
    public static Iterable<Short> toIterable(final short[] values) {
        return new Iterable<Short>() {
            @Override
            public Iterator<Short> iterator() {
                return new Iterator<Short>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Short next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>int</code> values into an
     * <code>Iterable</code> of <code>Integer</code> objects.
     *
     * @param values the array of <code>int</code>s
     * @return an <code>Iterable</code> of <code>Integer</code>s
     */
    public static Iterable<Integer> toIterable(final int[] values) {
        return new Iterable<Integer>() {
            @Override
            public Iterator<Integer> iterator() {
                return new Iterator<Integer>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Integer next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>long</code> values into an
     * <code>Iterable</code> of <code>Long</code> objects.
     *
     * @param values the array of <code>long</code>s
     * @return an <code>Iterable</code> of <code>Long</code>s
     */
    public static Iterable<Long> toIterable(final long[] values) {
        return new Iterable<Long>() {
            @Override
            public Iterator<Long> iterator() {
                return new Iterator<Long>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Long next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>float</code> values into an
     * <code>Iterable</code> of <code>Float</code> objects.
     *
     * @param values the array of <code>float</code>s
     * @return an <code>Iterable</code> of <code>Float</code>s
     */
    public static Iterable<Float> toIterable(final float[] values) {
        return new Iterable<Float>() {
            @Override
            public Iterator<Float> iterator() {
                return new Iterator<Float>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Float next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }

    /**
     * Converts an array of <code>double</code> values into an
     * <code>Iterable</code> of <code>Double</code> objects.
     *
     * @param values the array of <code>double</code>s
     * @return an <code>Iterable</code> of <code>Double</code>s
     */
    public static Iterable<Double> toIterable(final double[] values) {
        return new Iterable<Double>() {
            @Override
            public Iterator<Double> iterator() {
                return new Iterator<Double>() {
                    private int pos = 0;

                    @Override
                    public boolean hasNext() {
                        return pos < values.length;
                    }

                    @Override
                    public Double next() {
                        return values[pos++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("cannot remove an element from an array");
                    }
                };
            }
        };
    }
}

Related

  1. toIterable(final T[] arr)
  2. toIterable(final T[] array)