24 package org.lightjason.agentspeak.beliefbase.storage;
26 import com.google.common.collect.LinkedHashMultimap;
27 import com.google.common.collect.Multimaps;
28 import com.google.common.collect.SetMultimap;
30 import javax.annotation.Nonnull;
31 import java.text.MessageFormat;
32 import java.util.Collection;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.stream.Stream;
51 private final SetMultimap<String, N>
m_multielements = Multimaps.synchronizedSetMultimap( LinkedHashMultimap.create() );
62 return m_multielements.values().stream();
69 return m_singleelements.values().stream();
75 return m_multielements.containsKey( p_key );
81 return m_singleelements.containsKey( p_key );
85 public final boolean putMultiElement( @Nonnull
final String p_key,
final N p_value )
87 return m_multielements.put( p_key, p_value );
93 return !p_value.equals( m_singleelements.put( p_key, p_value ) );
99 return !p_value.equals( m_singleelements.putIfAbsent( p_key, p_value ) );
105 return m_multielements.remove( p_key, p_value );
111 return m_singleelements.remove( p_key ) != null;
117 return m_singleelements.get( p_key );
123 return m_singleelements.getOrDefault( p_key, p_default );
130 return m_multielements.get( p_key );
136 m_multielements.clear();
137 m_singleelements.clear();
143 return m_multielements.isEmpty() && m_singleelements.isEmpty();
149 return m_multielements.asMap().values().stream().mapToInt( Collection::size ).sum();
155 return MessageFormat.format(
157 m_multielements.isEmpty() ?
"" : m_multielements.values(),
158 m_singleelements.isEmpty() ?
"" : m_singleelements.values()
final boolean containsSingleElement( @Nonnull final String p_key)
contains a single-element
final boolean containsMultiElement( @Nonnull final String p_key)
contains a multi-element
final boolean empty()
checks if a storage is empty
final Stream< N > streamMultiElements()
returns a stream over all multi-elements
final Collection< N > getMultiElement( @Nonnull final String p_key)
returns a collection of multi-elementy by name
final SetMultimap< String, N > m_multielements
map with elements
default structure of a storage
final boolean removeSingleElement( @Nonnull final String p_key)
removes a single-element from the storage
final boolean removeMultiElement( @Nonnull final String p_key, final N p_value)
removes a multi-element from the storage
final M getSingleElement( @Nonnull final String p_key)
returns a single-element by the name
thread-safe storage of the data of single- and multi-elements
final void clear()
clears all elements
final boolean putMultiElement( @Nonnull final String p_key, final N p_value)
puts a multi-element into the storage
final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value)
puts a single-element if it is absent
final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default)
returns a single-element by the name
final Stream< M > streamSingleElements()
returns a stream over all single-elements
final Map< String, M > m_singleelements
map with single elements
final boolean putSingleElement( @Nonnull final String p_key, final M p_value)
puts a single-element into the storage
final int size()
number of multi elements