br.com.m4rc310.gql.services.MMultiRegitry Maven / Gradle / Ivy
package br.com.m4rc310.gql.services;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import reactor.core.publisher.FluxSink;
/**
*
* MMultiRegitry class.
*
*
* @author marcelo
* @version $Id: $Id
*/
public class MMultiRegitry {
private final ConcurrentMap>> map = new ConcurrentHashMap<>();
/**
*
* add.
*
*
* @param key a K object
* @param value a {@link reactor.core.publisher.FluxSink} object
*/
public synchronized void add(K key, FluxSink> value) {
List> list = map.get(key);
if (list != null) {
list.add(value);
} else {
List> newList = new CopyOnWriteArrayList<>();
newList.add(value);
map.put(key, newList);
}
}
/**
*
* remove.
*
*
* @param key a K object
*/
public synchronized void remove(K key) {
map.remove(key);
}
/**
*
* contains.
*
*
* @param key a K object
* @param value a {@link reactor.core.publisher.FluxSink} object
* @return a boolean
*/
public boolean contains(K key, FluxSink> value) {
List> list = get(key);
return list != null && list.contains(value);
}
/**
*
* contains.
*
*
* @param key a K object
* @return a boolean
*/
public boolean contains(K key) {
return map.containsKey(key);
}
/**
*
* getKeys.
*
*
* @param type a {@link java.lang.Class} object
* @return a {@link java.util.List} object
*/
public List getKeys(Class> type) {
List ret = new ArrayList<>();
final String typeName = type.getSimpleName();
map.forEach((k, v) -> {
String skey = k.toString();
if (skey.startsWith(typeName)) {
ret.add(k);
}
});
return ret;
}
/**
*
* getSizeRegistries.
*
*
* @param type a {@link java.lang.Class} object
* @return a {@link java.lang.Integer} object
*/
public Integer getSizeRegistries(Class> type) {
try {
return getKeys(type).size();
} catch (Exception e) {
return 0;
}
}
/**
*
* get.
*
*
* @param key a K object
* @return a {@link java.util.List} object
*/
public List> get(K key) {
return map.getOrDefault(key, Collections.emptyList());
}
/**
*
* createFluxSinkList.
*
*
* @param value a {@link reactor.core.publisher.FluxSink} object
* @return a {@link java.util.List} object
*/
protected List> createFluxSinkList(FluxSink> value) {
List> list = new CopyOnWriteArrayList<>();
list.add(value);
return list;
}
// Adicionando método para publicar valor para todas as instâncias de FluxSink
// associadas a uma chave
/**
*
* publishValue.
*
*
* @param key a K object
* @param value a T object
*/
@SuppressWarnings("unchecked")
public void publishValue(K key, T value) {
List> sinks = map.get(key);
if (sinks != null) {
sinks.forEach(sub -> ((FluxSink) sub).next(value));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy