/*\
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
* _____________________________________________________________________________________________________________________
*
* This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
* under the terms of the BSD License. The license text is appended to the end of this file.
\*/
package de.klaro.base.util;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/**
* Wrapper fr Maps, mit dem fehlende Schlssel mit Hilfe einer Factory automatisch erzeugt werden.
*
* Diese Implementation erweitert existierende Map-Instanzen, indem alle Aufrufe an die die im Konstruktor
* angegebene Map delegiert werden. Zustzlich gibt es eine neue Methode obtain, die wie die Methode get arbeitet,
* zustzlich aber die genannten Eigenschaften hat.
*
* @param <K> Der Typ der Schlssel
* @param <V> Der Typ der Werte
*/
public class AutoValueMap<K extends Object, V extends Object> extends Object implements Serializable, Map<K,V>
{
/* ______________________________________________________________________________________________________________ *\
\* Konstanten */
/* ______________________________________________________________________________________________________________ *\
\* Klassenvariablen */
/* ______________________________________________________________________________________________________________ *\
\* Instanzvariablen */
private Map<K,V> map$;
private AutoValueFactory<V> factory$;
/* ______________________________________________________________________________________________________________ *\
\* Konstruktoren */
/**
* Erzeugt eine Instanz dieser Klasse.
*
* Dieser Konstruktor erzeugt intern eine Factory,
*
* @param map Die Map, an die die Methodenaufrufe delegiert werden
* @param clazz
*/
public AutoValueMap(Map <K,V> map,Class<? extends V> clazz)
{
this(map,new SimpleAutoHashMapFactory<V>(clazz));
}
/**
* Erzeugt eine Instanz dieser Klasse.
*
* @param map Die Map, an die die Methodenaufrufe delegiert werden
* @param factory Die Factory fr Werte fr den Fall, dass ein Wert nicht vorhanden ist
*/
public AutoValueMap(Map <K,V> map,AutoValueFactory<V> factory)
{
super();
map$ = map;
factory$ = factory;
}
/* ______________________________________________________________________________________________________________ *\
\* Instanzmethoden */
/**
* Liefert die Anzahl der Eintrge.
*
* @return Die Anzahl der Eintrge
*/
public int size()
{
return map$.size();
}
/**
* Prft, ob die Map leer ist.
*
* @return Die Map ist leer
*/
public boolean isEmpty()
{
return map$.isEmpty();
}
/**
* Prft, ob der Eintrag mit dem angegebenen Schlssel existiert.
*
* @param key Der Schlssel
* @return Der Eintrag existiert
*/
public boolean containsKey(Object key)
{
return map$.containsKey(key);
}
/**
* Prft, ob der Eintrag mit dem angegebenen Wert existiert.
*
* @param value Der Wert
* @return Der Eintrag existiert
*/
public boolean containsValue(Object value)
{
return map$.containsValue(value);
}
/**
* Liefert den Eintrag mit dem angegebenen Schlssel.
*
* @param key Der Schlssel
* @return Der Eintrag, oder 'null' wenn der Eintrag nicht existiert
*/
public V get(Object key)
{
return map$.get(key);
}
/**
* Liefert den Wert eines Schlssels.
*
* Es kann bestimmt werden, ob ein Wert mit Hilfe der Factory erzeugt werden soll, wenn fr den Schlssel noch
* kein Eintrag vorhanden ist.
*
* @param key Der Schlssel
* @param autoCreate Der Eintrag wird erzeugt, wenn noch nicht vorhanden
* @return Der Wert
*/
public V get(K key, boolean autoCreate)
{
V value = map$.get(key);
if (autoCreate && value == null)
{
try
{
value = factory$.newInstance(key);
}
catch (IllegalAccessException e)
{
// Keine Aktion ntig, der Wert soll 'null' bleiben
}
catch (InstantiationException e)
{
// Keine Aktion ntig, der Wert soll 'null' bleiben
}
map$.put(key,value);
}
return value;
}
/**
* Fgt einen neuen Eintrag hinzu.
*
* @param key Der Schlssel
* @param value Der Wert
* @return Der Wert des zuvor unter dem Schlssel gespeicherten Wertes, oder 'null'
*/
public V put(K key, V value)
{
return map$.put(key,value);
}
/**
* Lscht den Eintrag mit dem angegebenen Schlssel.
*
* @param key Der Schlssel
* @return Der Wert des zuvor unter dem Schlssel gespeicherten Wertes, oder 'null'
*/
public V remove(Object key)
{
return map$.remove(key);
}
/**
* Kopiert die Eintrge der angegebenen Map.
*
* @param map Die Map
*/
public void putAll(Map<? extends K, ? extends V> map)
{
map$.putAll(map);
}
/**
* Lscht alle Eintrge.
*/
public void clear()
{
map$.clear();
}
/**
* Liefert die Schlssel aller Eintrge.
*
* @return Die Schlssel
*/
public Set<K> keySet()
{
return map$.keySet();
}
/**
* Liefert die Werte aller Eintrge.
*
* @return Die Werte
*/
public Collection<V> values()
{
return map$.values();
}
/**
* Liefert alle Eintrge.
*
* @return Die Eintrge
*/
public Set<Map.Entry<K, V>> entrySet()
{
return map$.entrySet();
}
/* ______________________________________________________________________________________________________________ *\
\* Klassenmethoden */
/* ______________________________________________________________________________________________________________ *\
\* Klassen */
/**
* Factory, die Objekte per Standard-Konstruktor erzeugt.
*
* @param <V1> Der Typ des Wertes, die die Factory erzeugt
*/
private static class SimpleAutoHashMapFactory<V1 extends Object> extends Object implements AutoValueFactory<V1>
{
private Class<? extends V1> clazz$;
/**
* Erzeugt eine Factory.
*
* @param clazz Die Klasse, fr die Objekte erzeugt werden sollen
*/
public SimpleAutoHashMapFactory(Class<? extends V1> clazz)
{
super();
clazz$ = clazz;
}
/**
* Erzeugt eine Instanz, indem der Standard-Konstruktor benutzt wird.
*
* @param key Der Schlssel
* @return Der Wert
* @throws IllegalAccessException Keine Zugriffrechte auf die Klasse
* @throws InstantiationException Fehler whrend des Konstruktoraufrufs
*/
public V1 newInstance(Object key) throws IllegalAccessException, InstantiationException
{
return clazz$.newInstance();
}
}
}
/*\
* _____________________________________________________________________________________________________________________
*
* This software is distributed under the terms of the BSD License:
*
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* _____________________________________________________________________________________________________________________
\*/