1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-08 21:31:08 +01:00

Kotlin conversion: MultiMap

Warning: This commit changes the semantics of MultiMap.put()
put() now replaces values, while plus() adds them.
This commit is contained in:
Paul Schaub 2023-09-28 14:36:04 +02:00
parent b324742a62
commit 841b386226
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 147 additions and 177 deletions

View file

@ -1,137 +0,0 @@
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.util;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
public class MultiMap<K, V> {
private final Map<K, Set<V>> map;
public MultiMap() {
map = new HashMap<>();
}
public MultiMap(@Nonnull MultiMap<K, V> other) {
this.map = new HashMap<>();
for (K k : other.map.keySet()) {
map.put(k, new LinkedHashSet<>(other.map.get(k)));
}
}
public MultiMap(@Nonnull Map<K, Set<V>> content) {
this.map = new HashMap<>(content);
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(K o) {
return map.containsKey(o);
}
public boolean containsValue(V o) {
for (Set<V> values : map.values()) {
if (values.contains(o)) return true;
}
return false;
}
public Set<V> get(K o) {
return map.get(o);
}
public void put(K k, V v) {
Set<V> values = map.get(k);
if (values == null) {
values = new LinkedHashSet<>();
map.put(k, values);
}
values.add(v);
}
public void put(K k, Set<V> vs) {
for (V v : vs) {
put(k, v);
}
}
public void removeAll(K o) {
map.remove(o);
}
public void remove(K o, V v) {
Set<V> vs = map.get(o);
if (vs == null) return;
vs.remove(v);
}
public void putAll(MultiMap<K, V> other) {
for (K key : other.keySet()) {
put(key, other.get(key));
}
}
public void clear() {
map.clear();
}
public Set<K> keySet() {
return map.keySet();
}
public Collection<Set<V>> values() {
return map.values();
}
public Set<Map.Entry<K, Set<V>>> entrySet() {
return map.entrySet();
}
/**
* Return all values of the {@link MultiMap} in a single {@link LinkedHashSet}.
*
* @return set of all values
*/
public Set<V> flatten() {
LinkedHashSet<V> flattened = new LinkedHashSet<>();
for (Set<V> items : map.values()) {
flattened.addAll(items);
}
return flattened;
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof MultiMap)) {
return false;
}
if (this == o) {
return true;
}
return map.equals(((MultiMap<?, ?>) o).map);
}
@Override
public int hashCode() {
return map.hashCode();
}
}

View file

@ -7,6 +7,7 @@ package org.pgpainless.util.selection.keyring;
import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
@ -33,9 +34,9 @@ public abstract class PublicKeyRingSelectionStrategy<O> implements KeyRingSelect
@Override
public MultiMap<O, PGPPublicKeyRing> selectKeyRingsFromCollections(@Nonnull MultiMap<O, PGPPublicKeyRingCollection> keyRingCollections) {
MultiMap<O, PGPPublicKeyRing> keyRings = new MultiMap<>();
for (O identifier : keyRingCollections.keySet()) {
for (PGPPublicKeyRingCollection collection : keyRingCollections.get(identifier)) {
keyRings.put(identifier, selectKeyRingsFromCollection(identifier, collection));
for (Map.Entry<O, Set<PGPPublicKeyRingCollection>> entry : keyRingCollections.entrySet()) {
for (PGPPublicKeyRingCollection collection : entry.getValue()) {
keyRings.plus(entry.getKey(), selectKeyRingsFromCollection(entry.getKey(), collection));
}
}
return keyRings;

View file

@ -6,6 +6,7 @@ package org.pgpainless.util.selection.keyring;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
@ -32,9 +33,9 @@ public abstract class SecretKeyRingSelectionStrategy<O> implements KeyRingSelect
@Override
public MultiMap<O, PGPSecretKeyRing> selectKeyRingsFromCollections(@Nonnull MultiMap<O, PGPSecretKeyRingCollection> keyRingCollections) {
MultiMap<O, PGPSecretKeyRing> keyRings = new MultiMap<>();
for (O identifier : keyRingCollections.keySet()) {
for (PGPSecretKeyRingCollection collection : keyRingCollections.get(identifier)) {
keyRings.put(identifier, selectKeyRingsFromCollection(identifier, collection));
for (Map.Entry<O, Set<PGPSecretKeyRingCollection>> entry : keyRingCollections.entrySet()) {
for (PGPSecretKeyRingCollection collection : entry.getValue()) {
keyRings.plus(entry.getKey(), selectKeyRingsFromCollection(entry.getKey(), collection));
}
}
return keyRings;

View file

@ -0,0 +1,75 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.util
class MultiMap<K, V> : Iterable<Map.Entry<K, Set<V>>> {
private val map: Map<K, MutableSet<V>>
constructor(): this(mutableMapOf())
constructor(other: MultiMap<K, V>): this(other.map)
constructor(content: Map<K, Set<V>>) {
map = mutableMapOf()
content.forEach {
map[it.key] = it.value.toMutableSet()
}
}
override fun iterator(): Iterator<Map.Entry<K, Set<V>>> {
return map.iterator()
}
val size: Int
get() = map.size
fun size() = size
val keys: Set<K>
get() = map.keys
fun keySet() = keys
val values: Collection<Set<V>>
get() = map.values
fun values() = values
val entries: Set<Map.Entry<K, Set<V>>>
get() = map.entries
fun entrySet() = entries
fun isEmpty(): Boolean = map.isEmpty()
fun containsKey(key: K): Boolean = map.containsKey(key)
fun containsValue(value: V): Boolean = map.values.any { it.contains(value) }
fun contains(key: K, value: V): Boolean = map[key]?.contains(value) ?: false
operator fun get(key: K): Set<V>? = map[key]
fun put(key: K, value: V) =
(map as MutableMap).put(key, mutableSetOf(value))
fun plus(key: K, value: V) =
(map as MutableMap).getOrPut(key) { mutableSetOf() }.add(value)
fun put(key: K, values: Set<V>) =
(map as MutableMap).put(key, values.toMutableSet())
fun plus(key: K, values: Set<V>) =
(map as MutableMap).getOrPut(key) { mutableSetOf() }.addAll(values)
fun putAll(other: MultiMap<K, V>) = other.map.entries.forEach {
put(it.key, it.value)
}
fun plusAll(other: MultiMap<K, V>) = other.map.entries.forEach {
plus(it.key, it.value)
}
fun removeAll(key: K) = (map as MutableMap).remove(key)
fun remove(key: K, value: V) = (map as MutableMap)[key]?.remove(value)
fun clear() = (map as MutableMap).clear()
fun flatten() = map.flatMap { it.value }.toSet()
override fun equals(other: Any?): Boolean {
return if (other == null)
false
else if (other !is MultiMap<*, *>)
false
else if (this === other) {
true
} else {
map == other.map
}
}
override fun hashCode(): Int {
return map.hashCode()
}
}