All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.util.KeyMap Maven / Gradle / Ivy

/*
 * KeyMap.java May 2007
 *
 * Copyright (C) 2007, Niall Gallagher 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 */

package org.simpleframework.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * The KeyMap object is used to represent a map of values
 * keyed using a known string. This also ensures that the keys and
 * the values added to this hash map can be acquired in an independent
 * list of values, ensuring that modifications to the map do not have
 * an impact on the lists provided, and vice versa. The key map can
 * also be used in a fore each look using the string keys.
 * 
 * @author Niall Gallagher
 */
public class KeyMap extends HashMap implements Iterable {

   /**
    * Constructor for the KeyMap object. This creates
    * a hash map that can expose the keys and values of the map as
    * an independent List containing the values. This
    * can also be used within a for loop for convenience.
    */
   public KeyMap() {
     super();            
   }          
   
   /**
    * This is used to produce an Iterator of values 
    * that can be used to acquire the contents of the key map within
    * a for each loop. The key map can be modified while it is been
    * iterated as the iterator is an independent list of values.
    * 
    * @return this returns an iterator of the keys in the map
    */
   public Iterator iterator() {
      return getKeys().iterator();
   }

   /**
    * This is used to produce a List of the keys in
    * the map. The list produced is a copy of the internal keys and
    * so can be modified and used without affecting this map object.
    * 
    * @return this returns an independent list of the key values
    */
   public List getKeys() {
     Set keys = keySet();
     
     if(keys == null) {
        return new ArrayList();
     }
     return new ArrayList(keys);      
   }
   
   /**
    * This is used to produce a List of the values in
    * the map. The list produced is a copy of the internal values and
    * so can be modified and used without affecting this map object.
    * 
    * @return this returns an independent list of the values
    */   
   public List getValues() {
      Collection values = values();
      
      if(values == null) {
         return new ArrayList();          
      }
      return new ArrayList(values);
   }
 }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy