• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.util.collections;
6 
7 import java.util.Enumeration;
8 import java.util.Iterator;
9 import java.util.LinkedList;
10 import java.util.List;
11 
12 /**
13  * Utilities for Iterables
14  */
15 public class Iterables {
16 
17     /**
18      * Converts enumeration into iterable
19      */
toIterable(Enumeration<T> in)20     public static <T> Iterable<T> toIterable(Enumeration<T> in) {
21         List<T> out = new LinkedList<T>();
22         while(in.hasMoreElements()) {
23             out.add(in.nextElement());
24         }
25         return out;
26     }
27 
28     /**
29      * Returns first element of provided iterable or fails fast when iterable is empty.
30      *
31      * @param iterable non-empty iterable
32      * @return first element of supplied iterable
33      * @throws IllegalArgumentException when supplied iterable is empty
34      */
firstOf(Iterable<T> iterable)35     public static <T> T firstOf(Iterable<T> iterable) {
36         Iterator<T> iterator = iterable.iterator();
37         if (!iterator.hasNext()) {
38             throw new IllegalArgumentException("Cannot provide 1st element from empty iterable: " + iterable);
39         }
40         return iterator.next();
41     }
42 }
43