diff --git a/oap-stdlib/src/main/java/oap/util/ConcurrentAssocMap.java b/oap-stdlib/src/main/java/oap/util/ConcurrentAssocMap.java new file mode 100644 index 0000000000..d7785540ef --- /dev/null +++ b/oap-stdlib/src/main/java/oap/util/ConcurrentAssocMap.java @@ -0,0 +1,98 @@ +package oap.util; + + +import com.google.common.base.Preconditions; +import lombok.NoArgsConstructor; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Consumer; +import java.util.function.Supplier; +import java.util.stream.Stream; + +/** + * This class is a concurrent version of OAP AssocList + * + * @param + * @param + */ +@SuppressWarnings( "AbstractClassName" ) +@NoArgsConstructor +public abstract class ConcurrentAssocMap implements Iterable { + + private final ConcurrentMap entries = new ConcurrentHashMap<>(); + + protected abstract K keyOf( V label ); + + public ConcurrentAssocMap( ConcurrentMap entries ) { + this.entries.putAll( entries ); + } + + public ConcurrentAssocMap( Collection values ) { + values.forEach( v -> this.entries.put( this.keyOf( v ), v ) ); + } + + public Stream stream() { + return entries.values().stream(); + } + + public Collection values() { + return entries.values(); + } + + public void forEach( Consumer action ) { + entries.values().forEach( action ); + } + + public int size() { + return entries.size(); + } + + public Optional get( K key ) { + return Optional.ofNullable( entries.get( key ) ); + } + + public V getOrDefault( K key, V def ) { + return get( key ).orElse( def ); + } + + public boolean removeKey( K key ) { + return entries.remove( key ) != null; + } + + public Set getAll( Collection keys ) { + LinkedHashSet result = new LinkedHashSet<>(); + for( K key : keys ) get( key ).ifPresent( result::add ); + return result; + } + + public boolean add( V v ) { + return entries.put( keyOf( v ), v ) == null; + } + + public V computeIfAbsent( K key, Supplier supplier ) { + V v = supplier.get(); + Preconditions.checkArgument( Objects.equals( key, keyOf( v ) ) ); + return entries.computeIfAbsent( key, k -> supplier.get() ); + } + + public boolean containsKey( K key ) { + return this.entries.containsKey( key ); + } + + public boolean addAll( Collection c ) { + for( V v : c ) add( v ); + return !c.isEmpty(); + } + + @Override + public Iterator iterator() { + return entries.values().iterator(); + } +} diff --git a/pom.xml b/pom.xml index 96920af4ad..f37c4fb632 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,7 @@ - 22.6.0 + 22.6.1 21.0.0 21.0.1