001 /* 002 * $RCSfile: ListMap.java,v $ 003 * 004 * Created on March 12, 2002, 4:28 PM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2002-2003 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 package gov.bnl.star.offline.scheduler.policy; 024 025 import java.util.*; 026 027 028 /** 029 * 030 * @author Gabriele Carcassi 031 * @version $Revision: 1.8 $ $Date: 2004/02/20 20:00:14 $ 032 */ 033 public class ListMap { 034 private Map lists = new Hashtable(); 035 private int nValues; 036 037 public ListMap() { 038 } 039 040 /** Adds a value in the list associated to key 041 */ 042 public boolean add(Object key, Object value) { 043 List list = (List) lists.get(key); 044 045 if (list != null) { 046 list.add(value); 047 nValues++; 048 049 return true; 050 } 051 052 list = new ArrayList(); 053 list.add(value); 054 lists.put(key, list); 055 nValues++; 056 057 return true; 058 } 059 060 public boolean addAll(ListMap map) { 061 Set keys = map.getKeys(); 062 Iterator iter = keys.iterator(); 063 064 while (iter.hasNext()) { 065 Object key = iter.next(); 066 List list = getList(key); 067 068 if (list == null) { 069 list = new ArrayList(map.getList(key)); 070 lists.put(key, list); 071 } else { 072 list.addAll(map.getList(key)); 073 } 074 } 075 076 return true; 077 } 078 079 /** Returns the number of values stored in the map. 080 */ 081 public int size() { 082 return nValues; 083 } 084 085 /** Returns the number of keys. 086 */ 087 public int getNLists() { 088 return lists.keySet().size(); 089 } 090 091 /** Returns all the keys. 092 */ 093 public Set getKeys() { 094 return lists.keySet(); 095 } 096 097 /** Get the list of values corresponding to the given key. 098 * @param key the key 099 * @return a list with all the values 100 */ 101 public List getList(Object key) { 102 return (List) lists.get(key); 103 } 104 105 public boolean remove(Object key, Object value) { 106 List list = getList(key); 107 108 if (list == null) { 109 return false; 110 } 111 112 if (list.remove(value)) { 113 nValues--; 114 if (list.size() == 0) { 115 lists.remove(key); 116 } 117 return true; 118 } 119 return false; 120 } 121 122 public boolean remove(Object key) { 123 List list = (List) lists.remove(key); 124 if (list == null) { 125 return false; 126 } 127 128 nValues -= list.size(); 129 return true; 130 } 131 132 public boolean removeValue(Object value) { 133 Set keys = getKeys(); 134 Iterator iter = keys.iterator(); 135 136 while (iter.hasNext()) { 137 Object key = iter.next(); 138 List list = getList(key); 139 140 while (list.contains(value)) { 141 nValues--; 142 remove(key, value); 143 } 144 } 145 146 // TODO Should return false if nothing was deleted 147 return true; 148 } 149 }