-
Notifications
You must be signed in to change notification settings - Fork 587
/
AbstractImmutableMultimap.java
144 lines (121 loc) · 4.06 KB
/
AbstractImmutableMultimap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.multimap;
import java.io.InvalidClassException;
import java.io.ObjectStreamException;
import java.util.Collection;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.collection.ImmutableCollection;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.ImmutableMultimap;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.impl.set.mutable.UnmodifiableMutableSet;
import org.eclipse.collections.impl.utility.Iterate;
public abstract class AbstractImmutableMultimap<K, V, C extends ImmutableCollection<V>>
extends AbstractMultimap<K, V, C>
implements ImmutableMultimap<K, V>
{
protected final ImmutableMap<K, C> map;
/**
* Creates a new multimap that clones the provided map into an ImmutableMap.
*
* @param map place to store the mapping from each key to its corresponding values
*/
protected AbstractImmutableMultimap(MutableMap<K, C> map)
{
this(map.toImmutable());
}
/**
* Creates a new multimap that uses the provided immutableMap.
*
* @param immutableMap place to store the mapping from each key to its corresponding values
*/
protected AbstractImmutableMultimap(ImmutableMap<K, C> immutableMap)
{
this.map = immutableMap;
}
@Override
protected ImmutableMap<K, C> getMap()
{
return this.map;
}
// Query Operations
@Override
public int size()
{
class CountProcedure implements Procedure<C>
{
private static final long serialVersionUID = 1L;
private int totalSize;
@Override
public void value(C collection)
{
this.totalSize += collection.size();
}
public int getTotalSize()
{
return this.totalSize;
}
}
CountProcedure procedure = new CountProcedure();
this.map.forEachValue(procedure);
return procedure.getTotalSize();
}
@Override
public int sizeDistinct()
{
return this.map.size();
}
@Override
public boolean isEmpty()
{
return this.map.isEmpty();
}
// Views
@Override
public SetIterable<K> keySet()
{
return UnmodifiableMutableSet.of(this.getMap().castToMap().keySet());
}
@Override
public C get(K key)
{
return this.map.getIfAbsentWith(key, this.createCollectionBlock(), this);
}
@Override
public MutableMap<K, RichIterable<V>> toMap()
{
return (MutableMap<K, RichIterable<V>>) (MutableMap<?, ?>) this.map.toMap();
}
@Override
public <R extends Collection<V>> MutableMap<K, R> toMap(Function0<R> collectionFactory)
{
MutableMap<K, R> result = Maps.mutable.empty();
this.map.forEachKeyValue((key, iterable) -> {
R newCollection = collectionFactory.value();
Iterate.addAllTo(iterable, newCollection);
result.put(key, newCollection);
});
return result;
}
@Override
public ImmutableMultimap<K, V> toImmutable()
{
return this;
}
protected Object readResolve() throws ObjectStreamException
{
throw new InvalidClassException("You should be using the proxy for serialization of ImmutableMultimaps");
}
}