1 package org.apache.onami.configuration; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.Iterator; 23 import java.util.Map; 24 import java.util.Map.Entry; 25 26 27 /** 28 * Simple iterator of a {@code Map<K, V>} entries, with the option of prefixing keys 29 * with the given prefix. 30 */ 31 final class PropertiesIterator<K, V> 32 implements Iterator<Entry<String, String>> 33 { 34 35 /** 36 * Creates a new iterator over a map configuration with prefixing the keys with the given prefix. 37 * 38 * @param <K> The map entry key type 39 * @param <V> The map entry value type 40 * @param properties The map configuration has to be read 41 * @return A map configuration iterator 42 */ 43 public static final <K, V> PropertiesIterator<K, V> newPropertiesIterator( Map<K, V> properties ) 44 { 45 return new PropertiesIterator<K, V>( null, properties ); 46 } 47 48 /** 49 * Creates a new iterator over a map configuration with prefixing the keys with the given prefix. 50 * 51 * @param <K> the map entry key type. 52 * @param <V> the map entry value type. 53 * @param keyPrefix the prefix for key entries. 54 * @param properties the map configuration has to be read. 55 * @return a map configuration iterator. 56 */ 57 public static final <K, V> PropertiesIterator<K, V> newPropertiesIterator( String keyPrefix, Map<K, V> properties ) 58 { 59 return new PropertiesIterator<K, V>( keyPrefix, properties ); 60 } 61 62 /** 63 * The key prefix. 64 */ 65 private final String keyPrefix; 66 67 /** 68 * The wrapped configuration iterator. 69 */ 70 private final Iterator<Entry<K, V>> propertiesIterator; 71 72 /** 73 * Creates a new properties iterator. 74 * 75 * @param keyPrefix the key prefix. It can be {@code null}. 76 * @param properties the wrapped configuration. 77 */ 78 private PropertiesIterator( String keyPrefix, Map<K, V> properties ) 79 { 80 this.keyPrefix = keyPrefix; 81 this.propertiesIterator = properties.entrySet().iterator(); 82 } 83 84 /** 85 * {@inheritDoc} 86 */ 87 public boolean hasNext() 88 { 89 return propertiesIterator.hasNext(); 90 } 91 92 /** 93 * {@inheritDoc} 94 */ 95 public Entry<String, String> next() 96 { 97 Entry<K, V> next = propertiesIterator.next(); 98 String key = String.valueOf( next.getKey() ); 99 if ( keyPrefix != null && keyPrefix.length() > 0 ) 100 { 101 key = keyPrefix + key; 102 } 103 return new KeyValue( key, String.valueOf( next.getValue() ) ); 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 public void remove() { 110 // not needed in this version 111 } 112 113 }