Package discord4j.store.api.mapping
Class MappingStoreService
- java.lang.Object
-
- discord4j.store.api.mapping.MappingStoreService
-
- All Implemented Interfaces:
StoreService
public class MappingStoreService extends Object implements StoreService
Allows you to delegate the store process to a differentStoreServicedepending on the stored value class.Can acquire an instance through
create()and configure it afterwards.Can be used like this to suppress storing certain entities:
DiscordClient client = new DiscordClientBuilder(token) .setStoreService(MappingStoreService.create() .setMapping(new NoOpStoreService(), MessageBean.class) .setFallback(new JdkStoreService())) .build();
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MappingStoreServicecreate()Obtain a newMappingStoreServiceinstance that can be further configured usingsetMapping(StoreService, Class)to set a new delegateStoreServiceandsetFallback(StoreService)to provide a fallback if the value class is not defined elsewhere.Mono<Void>dispose()This is a lifecycle method called to signal that a store should dispose of any resources due to an abrupt close (hard reconnect or disconnect).booleanhasGenericStores()This is used to check if this service can provide generic stores.booleanhasLongObjStores()This is used to check if this service can provide long-object stores.voidinit(StoreContext context)This is a lifecycle method called to signal that a store should allocate any necessary resources.<K extends Comparable<K>,V>
Store<K,V>provideGenericStore(Class<K> keyClass, Class<V> valueClass)This is called to provide a new store instance for the provided configuration.<V> LongObjStore<V>provideLongObjStore(Class<V> valueClass)This is called to provide a new store instance with a long key and object values.MappingStoreServicesetFallback(StoreService storeService)Set the fallbackStoreServiceto use if no mapping was previously defined for a given type.MappingStoreServicesetMapping(StoreService storeService, Class<?> valueClass)Set a new delegateStoreServicefor the given value class.MappingStoreServicesetMappings(StoreService storeService, Class<?>... valueClasses)Set a new delegateStoreServicefor multiple given value classes.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface discord4j.store.api.service.StoreService
order
-
-
-
-
Method Detail
-
create
public static MappingStoreService create()
Obtain a newMappingStoreServiceinstance that can be further configured usingsetMapping(StoreService, Class)to set a new delegateStoreServiceandsetFallback(StoreService)to provide a fallback if the value class is not defined elsewhere.- Returns:
- a new instance backed by
NoOpStoreServiceas fallback for all types, can be configured.
-
setMapping
public MappingStoreService setMapping(StoreService storeService, Class<?> valueClass)
Set a new delegateStoreServicefor the given value class.- Parameters:
storeService- the service to use for the valuesvalueClass- the value class to map to the given service- Returns:
- a new
MappingStoreServicewith updated properties
-
setMappings
public MappingStoreService setMappings(StoreService storeService, Class<?>... valueClasses)
Set a new delegateStoreServicefor multiple given value classes.- Parameters:
storeService- the service to use for the valuesvalueClasses- the list of value classes to map to the given service- Returns:
- a new
MappingStoreServicewith updated properties
-
setFallback
public MappingStoreService setFallback(@Nullable StoreService storeService)
Set the fallbackStoreServiceto use if no mapping was previously defined for a given type.- Parameters:
storeService- the fallback service- Returns:
- a new
MappingStoreServicewith updated properties
-
hasGenericStores
public boolean hasGenericStores()
Description copied from interface:StoreServiceThis is used to check if this service can provide generic stores.- Specified by:
hasGenericStoresin interfaceStoreService- Returns:
- True if possible, else false.
- See Also:
Store
-
provideGenericStore
public <K extends Comparable<K>,V> Store<K,V> provideGenericStore(Class<K> keyClass, Class<V> valueClass)
Description copied from interface:StoreServiceThis is called to provide a new store instance for the provided configuration.- Specified by:
provideGenericStorein interfaceStoreService- Type Parameters:
K- The key type which provides a 1:1 mapping to the value type. This type is also expected to beComparablein order to allow for range operations.V- The value type, these follow JavaBean conventions.- Parameters:
keyClass- The class of the keys.valueClass- The class of the values.- Returns:
- The instance of the store.
-
hasLongObjStores
public boolean hasLongObjStores()
Description copied from interface:StoreServiceThis is used to check if this service can provide long-object stores.- Specified by:
hasLongObjStoresin interfaceStoreService- Returns:
- True if possible, else false.
- See Also:
LongObjStore
-
provideLongObjStore
public <V> LongObjStore<V> provideLongObjStore(Class<V> valueClass)
Description copied from interface:StoreServiceThis is called to provide a new store instance with a long key and object values.- Specified by:
provideLongObjStorein interfaceStoreService- Type Parameters:
V- The value type, these follow JavaBean conventions.- Parameters:
valueClass- The class of the values.- Returns:
- The instance of the store.
-
init
public void init(StoreContext context)
Description copied from interface:StoreServiceThis is a lifecycle method called to signal that a store should allocate any necessary resources.- Specified by:
initin interfaceStoreService- Parameters:
context- Some context about the environment which this service is being utilized in.
-
dispose
public Mono<Void> dispose()
Description copied from interface:StoreServiceThis is a lifecycle method called to signal that a store should dispose of any resources due to an abrupt close (hard reconnect or disconnect).- Specified by:
disposein interfaceStoreService- Returns:
- A mono, whose completion signals that resources have been released successfully.
-
-