[Japi-cvs] SF.net SVN: japi:[1192] libs/util/trunk/src/prj/net/sf/japi/util/ HashMapWithDefault.jav
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2009-02-26 14:29:09
|
Revision: 1192 http://japi.svn.sourceforge.net/japi/?rev=1192&view=rev Author: christianhujer Date: 2009-02-26 14:29:00 +0000 (Thu, 26 Feb 2009) Log Message: ----------- Added class HashMapWithDefault. Added Paths: ----------- libs/util/trunk/src/prj/net/sf/japi/util/HashMapWithDefault.java Added: libs/util/trunk/src/prj/net/sf/japi/util/HashMapWithDefault.java =================================================================== --- libs/util/trunk/src/prj/net/sf/japi/util/HashMapWithDefault.java (rev 0) +++ libs/util/trunk/src/prj/net/sf/japi/util/HashMapWithDefault.java 2009-02-26 14:29:00 UTC (rev 1192) @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2009 Christian Hujer. + * + * 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; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.sf.japi.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * HashMap implementation which provides default values for unmapped keys which can be distinct from <code>null</code>. + * @warning This map implementation violates the contract of {@link Map#get(Object)} regarding the return value. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @param <K> Type for the map keys. + * @param <V> Type for the map values. + */ +@SuppressWarnings({"ClassExtendsConcreteCollection"}) +public class HashMapWithDefault<K, V> extends HashMap<K, V> { + + /** Serial Version. */ + private static final long serialVersionUID = 1L; + + /** The default value to return if a key is not mapped. + * @serial include + */ + // It is the responsibility of the user to choose a serializable V for a map that is serialized. + @SuppressWarnings({"NonSerializableFieldInSerializableClass"}) + private V defaultValue; + + /** Creates a HashMapWithDefault. + * @param defaultValue Value to be returned by {@link Map#get(Object)} for unmapped keys. + */ + public HashMapWithDefault(final V defaultValue) { + this.defaultValue = defaultValue; + } + + /** {@inheritDoc} */ + @Override + public V get(final Object key) { + return containsKey(key) ? super.get(key) : defaultValue; + } +} Property changes on: libs/util/trunk/src/prj/net/sf/japi/util/HashMapWithDefault.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |