/**
* Copyright 2012 by dueni.ch
*
* 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.
*/
package ch.dueni.util.collections;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* CreateOnWriteMap
is intended to be used for most likely empty maps within objects
* that are intended to keep in session and therefore may occupy memory for long time.
* CreateOnWriteMap
in best usage case will use 0 bytes of memory and still provides a
* fully operable Map implementation using a call-back method to create the real map before first
* {@link Map#put(Object, Object)} operation is executed.
*
* Memory analysis for different types of maps show: *
* ** Nr Test case retained shallow * == ================================================== ========= ======= * 1 HashMap (default size) 120 40 * 2 HashMap (size 0) 56 40 * 3 LinkedHashMap (empty) 160 48 * 4 CreatOnWriteMap (empty, assigned to variable) 32 16 * 5 CreatOnWriteMap (return new from getList() method) 0 0 ** *
* public class MyOwner { * private Map<String, String> map; * * public MyOwner() { * map = new CreateOnWriteMap<String, String>() { * @Override * public Map<String, String> newMap() { * map = new HashMap<String, String>(2); // size will grow to 2 on first put anyway * return map; * } * }; * } * * public Map<String, String> getMap() { * return map; * } * } ** *
* public class MyOwner { * private Map<String, String> map; * * public MyOwner() { * } * * public Map<String, String> getMap() { * if (map == null) { * return new CreateOnWriteMap<String, String>() { * @Override * public Map<String, String> newMap() { * map = new HashMap<String, String>(2); // size will grow to 2 on first put anyway * return map; * } * }; * } * return map; * } * } ** *
* It is recommended to return new CreateOnWriteMap within the get-method as shown in * "example code for test nr 5" unless you have very frequent access to empty maps without putting * entries. *
* * @author Hanspeter Dünnenberger */ public abstract class CreateOnWriteMap* public class OwningType { * private Map<String, String> map; * * public Map<String, String> getMap() { * if (map == null) { * return new CreateOnWriteMap<String, String>() { * * @Override * public Map<String, String> newMap() { * map = new HashMap<String>(2); // init size 2, would grow to 2 on first put anyway * return map; * } * }; * } * return map; * } * } ** * @return the just created real Map after assigning it to the owning object's member variable. */ public abstract Map