Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent assocmap #346

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 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,93 @@
package oap.util;


import com.google.common.base.Preconditions;
import lombok.NoArgsConstructor;

import java.util.Collection;
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.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
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> {

nofateg marked this conversation as resolved.
Show resolved Hide resolved
private ConcurrentMap<K, V> entries = new ConcurrentHashMap<>();
nofateg marked this conversation as resolved.
Show resolved Hide resolved

protected abstract K keyOf( V label );

public ConcurrentAssocMap( ConcurrentMap<K, V> entries ) {
this.entries = entries;
nofateg marked this conversation as resolved.
Show resolved Hide resolved
}

public ConcurrentAssocMap( Collection<V> values ) {
entries = values.stream().collect( Collectors.toConcurrentMap( this::keyOf, Function.identity() ) );
nofateg marked this conversation as resolved.
Show resolved Hide resolved
}

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();
}
}
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.5.6</oap.project.version>
<oap.project.version>22.5.7</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