Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/lambda/DefaultMethods/ArrayIterator.java
38829 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031import java.util.Iterator;32import java.util.NoSuchElementException;3334/**35* The code sample illustrates the usage of default methods in the JDK 8. Most36* implementations of {@link Iterator} don't provide a useful37* {@link Iterator#remove()} method, however,38* they still have to implement this method to throw39* an UnsupportedOperationException. With the default method, the same40* default behavior in interface Iterator itself can be provided.41*/42public class ArrayIterator {4344/** Close the constructor because ArrayIterator is part of the utility45* class.46*/47protected ArrayIterator() {48throw new UnsupportedOperationException();49}5051/**52* Returns an iterator that goes over the elements in the array.53*54* @param <E> type of an array element55* @param array source array to iterate over it56* @return an iterator that goes over the elements in the array57*/58public static <E> Iterator<E> iterator(final E[] array) {59return new Iterator<E>() {60/**61* Index of the current position62*63*/64private int index = 0;6566/**67* Returns the next element in the iteration68*69* @return the next element in the iteration70* @throws NoSuchElementException if the iteration has no more71* elements72*/73@Override74public boolean hasNext() {75return (index < array.length);76}7778/**79* Returns {@code true} if the iteration has more elements. (In80* other words, returns {@code true} if {@link #next} returns81* an element, rather than throwing an exception.)82*83* @return {@code true} if the iteration has more elements84*/85@Override86public E next() {87if (!hasNext()) {88throw new NoSuchElementException();89}90return array[index++];91}9293/**94* This method does not need to be overwritten in JDK 8.95*/96//@Override97//public void remove() {98// throw UnsupportedOperationException(99// "Arrays don't support remove.")100//}101};102}103104/**105* Sample usage of the ArrayIterator106*107* @param args command-line arguments108*/109public static void main(final String[] args) {110Iterator<String> it = ArrayIterator.iterator(111new String[]{"one", "two", "three"});112113while (it.hasNext()) {114System.out.println(it.next());115}116}117}118119120