fixed NPE on double-remove of last element
[fedora-idea.git] / platform / util / src / com / intellij / openapi / util / MultiValuesMap.java
blobee0b7d8a4baaab3a52acf36240e1c215b9e08dbb
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.util;
18 import org.jetbrains.annotations.Nullable;
20 import java.util.*;
22 public class MultiValuesMap<K, V>{
23 private final Map<K, Collection<V>> myBaseMap;
24 private final boolean myOrdered;
26 public MultiValuesMap() {
27 this(false);
30 public MultiValuesMap(boolean ordered) {
31 myOrdered = ordered;
32 myBaseMap = ordered ? new LinkedHashMap<K, Collection<V>>() : new HashMap<K, Collection<V>>();
35 public void putAll(K key, Collection<V> values) {
36 for (V value : values) {
37 put(key, value);
41 public void putAll(K key, V... values) {
42 for (V value : values) {
43 put(key, value);
47 public void put(K key, V value) {
48 if (!myBaseMap.containsKey(key)) {
49 myBaseMap.put(key, myOrdered ? new LinkedHashSet<V>() : new HashSet<V>());
52 myBaseMap.get(key).add(value);
55 @Nullable
56 public Collection<V> get(K key){
57 return myBaseMap.get(key);
60 public Set<K> keySet() {
61 return myBaseMap.keySet();
64 public Collection<V> values() {
65 Set<V> result = myOrdered ? new LinkedHashSet<V>() : new HashSet<V>();
66 for (final Collection<V> values : myBaseMap.values()) {
67 result.addAll(values);
70 return result;
73 public void remove(K key, V value) {
74 if (myBaseMap.get(key) == null) return;
75 final Collection<V> values = myBaseMap.get(key);
76 values.remove(value);
77 if (values.isEmpty()) {
78 myBaseMap.remove(key);
82 public void clear() {
83 myBaseMap.clear();
86 @Nullable
87 public Collection<V> removeAll(final K key) {
88 return myBaseMap.remove(key);
91 public Set<Map.Entry<K, Collection<V>>> entrySet() {
92 return myBaseMap.entrySet();
95 public boolean isEmpty() {
96 return myBaseMap.isEmpty();
99 public boolean containsKey(final K key) {
100 return myBaseMap.containsKey(key);
103 public Collection<V> collectValues() {
104 Collection<V> result = new HashSet<V>();
105 for (Collection<V> v : myBaseMap.values()) {
106 result.addAll(v);
109 return result;
112 @Nullable
113 public V getFirst(final K key) {
114 Collection<V> values = myBaseMap.get(key);
115 return values == null || values.isEmpty() ? null : values.iterator().next();