001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.core.util.resource.locator; 018 019import java.util.Arrays; 020import java.util.Iterator; 021 022/** 023 * Iterate over a set of extensions. If null is provided, hasNext() will 024 * successfully return once with next() returning {@code null}. 025 * 026 * @author Juergen Donnerstag 027 */ 028public class ExtensionResourceNameIterator implements Iterator<String> 029{ 030 private static final Iterable<String> NULL_ITERABLE = Arrays.asList((String)null); 031 032 private final Iterator<String> iterator; 033 034 private String current; 035 036 /** 037 * Construct. 038 * 039 * @param extensions 040 * {@code null} or iterable with extensions 041 */ 042 public ExtensionResourceNameIterator(final Iterable<String> extensions) 043 { 044 // Fail safe: hasNext() needs to return at least once with true 045 if (extensions == null || !extensions.iterator().hasNext()) 046 { 047 this.iterator = NULL_ITERABLE.iterator(); 048 } 049 else 050 { 051 this.iterator = extensions.iterator(); 052 } 053 } 054 055 @Override 056 public boolean hasNext() 057 { 058 return iterator.hasNext(); 059 } 060 061 /** 062 * @return The next filename extension. 063 */ 064 @Override 065 public String next() 066 { 067 current = iterator.next(); 068 return getExtension(); 069 } 070 071 /** 072 * @return Assuming you've called next() already, it'll return the very same value. 073 */ 074 public final String getExtension() 075 { 076 String ext = current; 077 078 if (ext != null) 079 { 080 if (ext.startsWith(".")) 081 { 082 ext = ext.substring(1); 083 } 084 } 085 return ext; 086 } 087 088 @Override 089 public void remove() 090 { 091 iterator.remove(); 092 } 093}