Skip to content

Commit

Permalink
Concurrent assocmap (#346)
Browse files Browse the repository at this point in the history
* Concurrent AssocMap to support concurrency  with inner collections

* Concurrent AssocMap to support concurrency  with inner collections

* Concurrent AssocMap to support concurrency  with inner collections

* Concurrent AssocMap comments on PR

* Concurrent AssocMap comments on PR
  • Loading branch information
JuliaGalabut authored Dec 4, 2024
1 parent 2e91e0a commit 21d6424
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
98 changes: 98 additions & 0 deletions oap-stdlib/src/main/java/oap/util/ConcurrentAssocMap.java
Original file line number Diff line number Diff line change
@@ -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<K,V>
*
* @param <K>
* @param <V>
*/
@SuppressWarnings( "AbstractClassName" )
@NoArgsConstructor
public abstract class ConcurrentAssocMap<K, V> implements Iterable<V> {

private final ConcurrentMap<K, V> entries = new ConcurrentHashMap<>();

protected abstract K keyOf( V label );

public ConcurrentAssocMap( ConcurrentMap<K, V> entries ) {
this.entries.putAll( entries );
}

public ConcurrentAssocMap( Collection<V> values ) {
values.forEach( v -> this.entries.put( this.keyOf( v ), v ) );
}

public Stream<V> stream() {
return entries.values().stream();
}

public Collection<V> values() {
return entries.values();
}

public void forEach( Consumer<? super V> action ) {
entries.values().forEach( action );
}

public int size() {
return entries.size();
}

public Optional<V> 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<V> getAll( Collection<K> keys ) {
LinkedHashSet<V> 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<V> 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<? extends V> c ) {
for( V v : c ) add( v );
return !c.isEmpty();
}

@Override
public Iterator<V> iterator() {
return entries.values().iterator();
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</distributionManagement>

<properties>
<oap.project.version>22.6.0</oap.project.version>
<oap.project.version>22.6.1</oap.project.version>

<oap.deps.config.version>21.0.0</oap.deps.config.version>
<oap.deps.oap-teamcity.version>21.0.1</oap.deps.oap-teamcity.version>
Expand Down

0 comments on commit 21d6424

Please sign in to comment.