001 /* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015 package org.apache.commons.functor.generator.util; 016 017 import java.util.Arrays; 018 import java.util.Collection; 019 import java.util.Iterator; 020 import java.util.Map; 021 022 import org.apache.commons.functor.generator.Generator; 023 import org.apache.commons.functor.generator.IteratorToGeneratorAdapter; 024 025 /** 026 * Generator factory for each element of a "collection". 027 * 028 * @since 1.0 029 * @version $Revision: 1160769 $ $Date: 2011-08-23 18:11:06 +0200 (Tue, 23 Aug 2011) $ 030 * @author Jason Horman (jason@jhorman.org) 031 */ 032 public final class EachElement { 033 /** 034 * <p>{@code EachElement} instances should NOT be constructed in 035 * standard programming. Instead, the methods of the class should be invoked 036 * statically.</p> 037 * 038 * <p>This constructor is public to permit tools that require a JavaBean 039 * instance to operate.</p> 040 */ 041 public EachElement() { 042 } 043 044 /** 045 * Get a Generator for each element of a Collection. 046 * @param <E> the type of elements held in the input collection. 047 * @param collection to iterate 048 * @return Generator<E> 049 */ 050 public static <E> Generator<E> from(Collection<? extends E> collection) { 051 return collection == null ? null : EachElement.from(collection.iterator()); 052 } 053 054 /** 055 * Get a Generator for each entry of a Map. 056 * @param <K> the type of keys maintained by the input map. 057 * @param <V> the type of mapped values in the input map. 058 * @param map to iterate 059 * @return Generator 060 */ 061 @SuppressWarnings("unchecked") 062 public static <K, V> Generator<Map.Entry<K, V>> from(Map<? extends K, ? extends V> map) { 063 return map == null ? null : EachElement.from(((Map<K, V>) map).entrySet().iterator()); 064 } 065 066 /** 067 * Get a Generator for each element of an Object[]. 068 * @param <E> the type of elements held in the input array. 069 * @param array to iterate 070 * @return Generator 071 */ 072 public static <E> Generator<E> from(E[] array) { 073 return array == null ? null : EachElement.from(Arrays.asList(array).iterator()); 074 } 075 076 /** 077 * Get a Generator for each element of an Iterator. 078 * @param <E> the type of elements held in the input iterator. 079 * @param iter to iterate 080 * @return Generator 081 */ 082 public static <E> Generator<E> from(Iterator<? extends E> iter) { 083 return iter == null ? null : new IteratorToGeneratorAdapter<E>(iter); 084 } 085 }