Coverage Report - org.apache.onami.configuration.PropertiesIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesIterator
92%
13/14
75%
3/4
1.333
 
 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  476
 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  24
         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  4
         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  28
     {
 80  28
         this.keyPrefix = keyPrefix;
 81  28
         this.propertiesIterator = properties.entrySet().iterator();
 82  28
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public boolean hasNext()
 88  
     {
 89  504
         return propertiesIterator.hasNext();
 90  
     }
 91  
 
 92  
     /**
 93  
      * {@inheritDoc}
 94  
      */
 95  
     public Entry<String, String> next()
 96  
     {
 97  476
         Entry<K, V> next = propertiesIterator.next();
 98  476
         String key = String.valueOf( next.getKey() );
 99  476
         if ( keyPrefix != null && keyPrefix.length() > 0 )
 100  
         {
 101  152
             key = keyPrefix + key;
 102  
         }
 103  476
         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  0
     }
 112  
 
 113  
 }