bt.BufferingMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bt-core Show documentation
Show all versions of bt-core Show documentation
BitTorrent Client Library (Core)
The newest version!
/*
* Copyright (c) 2016—2021 Andrei Tomashpolskiy and individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bt;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
/**
* Convenient utility class to create maps, that act as temporary buffers
* for storing values between their initial collection and retrieval for further processing.
*
* Thread-safe.
*
* @param Key type
* @param Collection element type
* @since 1.6
*/
public class BufferingMap {
private final ConcurrentMap> m;
private final Supplier extends Collection> valueSupplier;
/**
* @param valueSupplier Factory of new containers for elements
* @since 1.6
*/
public BufferingMap(Supplier extends Collection> valueSupplier) {
this.m = new ConcurrentHashMap<>();
this.valueSupplier = valueSupplier;
}
/**
* @since 1.6
*/
public synchronized boolean containsKey(K key) {
Collection values = m.get(key);
return values != null && values.size() > 0;
}
/**
* Add a new element into the corresponding bucket
* (a new bucket will be created, if mapping for this key does not exist yet).
*
* @since 1.6
*/
public synchronized void add(K key, E element) {
Collection values = m.computeIfAbsent(key, it -> valueSupplier.get());
values.add(element);
}
/**
* Remove and return all elements for a given key.
*
* @since 1.6
*/
public synchronized Collection removeCopy(K key) {
Collection copy;
Collection values = m.get(key);
if (values == null) {
copy = Collections.emptySet();
} else {
copy = valueSupplier.get();
if (values.size() > 0) {
copy.addAll(values);
values.clear();
}
}
return copy;
}
}