multimap.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 185 行
SVN-BASE
185 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program 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 * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.utils;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;/** * This class represents data structure for one to many relations. * It stores list of the elements for each key. The map is HashMap for * quick access. */public class MultiMap<Key, Value> { private boolean isCopy; private boolean isSynchronized; private boolean isSyncOptimized; private HashMap<Key, List<Value>> map = new HashMap<Key, List<Value>>(); private boolean isUniq; /** * Creates the instance with the given properties. * @param isCopy if this parameter is true, then the copy of the * List always returned, otherwise the internal list is returned * and modification of the returned list cause modification of the MultiMap. * Note that isSynchronized and isSyncOptimized are ignored if this * parameter is true. * @param isSyncOptimized if this parameter is true, than adding and * removing of the values will cause creation of the new List. It allows do * not synchronize returned list. * Note that isSynchronized parameter is ignored of this parameter is true. * @param isSynchronized If the value is true, then the synchronized is * returned. * @param isUniq If the parameter is true, then the List will contain only * unique values. */ public MultiMap(boolean isCopy, boolean isSyncOptimized, boolean isSynchronized, boolean isUniq) { this.isCopy = isCopy; // there are no needs in the the creation of the synchronized list // if it is cloned during update. this.isSynchronized = isSynchronized && (!isSyncOptimized); // there are no needs in the creation of the copy during map update // if clone is returned by get operations this.isSyncOptimized = isSyncOptimized && (!isCopy); this.isUniq = isUniq; } /** * creates the default MultiMap, where all properties are false. * */ public MultiMap() { this(false, false, false, false); } /** * Returns values mapped with the given key as Iterator. * Note that this method never returns null. */ public Iterator<Value> iterator(Key key) { return getList(key).iterator(); } /** * Returns values mapped with the given key as List. * Note that this method never returns null. */ public List<Value> getList(Key key) { List<Value> retVal; synchronized (map) { retVal = map.get(key); if ((retVal == null) && (!isCopy)) { retVal = new ArrayList<Value>(); map.put(key, retVal); } } if (retVal == null) { return new ArrayList<Value>(); } else if (isCopy) { ArrayList<Value> copy = new ArrayList<Value>(); copy.addAll(retVal); return copy; } else { return retVal; } } /** * Adds value to the list associated with the given key. */ public void addValue(Key key, Value value) { List<Value> list; synchronized (map) { List<Value> old = map.get(key); if (isSyncOptimized || (old == null)) { list = new ArrayList<Value>(); if (isSynchronized) { list = Collections.synchronizedList(list); } map.put(key, list); if (old != null) { list.addAll(old); } } else { list = old; } } synchronized (list) { if (!list.contains(value) || this.isUniq) { list.add(value); } } } /** * Removes the value from the list associated with the given key. * @param key given key * @param value value to be removed. * @return true if the list contained this element. */ public boolean removeValue(Key key, Value value) { List<Value> list; synchronized (map) { List<Value> old = map.get(key); if (old == null) { return false; } if (isSyncOptimized) { list = new ArrayList<Value>(); map.put(key, list); list.addAll(old); } else { list = old; } } return list.remove(value); } /** * Removes all values associated with the given key. */ public List<Value> clearValues(Object key) { synchronized (map) { List<Value> retVal = map.remove(key); return (retVal == null) ? new ArrayList<Value>() : retVal; } } /** * removes all elements. */ public void clear() { synchronized (map) { map.clear(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?