org.arp.javautil.map.ImnuMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javautil Show documentation
Show all versions of javautil Show documentation
JavaUtil is a utility library to speed the development of
Java software. We developed it over multiple years during
our internal development efforts, and it has reached a
point of stability where we have decided to make it
available to the outside world. The JavaUtil package aims
to fill in the gaps of the Apache Commons and similar
utility libraries out there. Features include convenience
classes for string, collections, arrays, dates, iterators,
colors, logging, unit testing, a little bit of basic
statistics, database queries, caching and more.
The newest version!
/*
* #%L
* JavaUtil
* %%
* Copyright (C) 2012 - 2013 Emory University
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.arp.javautil.map;
import java.util.HashMap;
/**
* ImnuMap is a {@link java.util.HashMap} that specifies a default value for
* keys that are not in the map. The default value is specified through an
* interface called DefaultValue that defines one method: defaultValue.
* defaultValue accepts the key as an argument, so an implementing
* class can decide what default value should be returned based on the supplied
* key.
*
* While there are many ways of providing a default value for missing keys, the
* name of this class enshrines the fact that the design of this particular
* implementation was suggested by Himanshu Rathod. Specifically, {IMNU} = {HIMANSHU} -
* {HASH}. In other words, ImnuMap is the difference between Himanshu's
* suggestion and a plain ol' HashMap.
*
* @author Michel Mansour
*
* @param
* the key type
* @param
* the value type
*/
public class ImnuMap extends HashMap {
private static final long serialVersionUID = -2120654167887510727L;
private final DefaultValue dv;
public ImnuMap(DefaultValue defaultValue) {
super();
this.dv = defaultValue;
}
/**
* Creates a new ImnuMap instance that returns null for all missing keys.
*/
public ImnuMap() {
this(new NullDefaultValue());
}
public V get(Object key) {
if (super.containsKey(key)) {
return super.get(key);
} else {
return dv.defaultValue(key);
}
}
public interface DefaultValue {
public V defaultValue(Object key);
}
/**
* A built-in implementation of DefaultValue that returns null for
* any missing key.
*/
private static class NullDefaultValue implements DefaultValue {
@Override
public U defaultValue(Object key) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy