LightJason - AgentSpeak(L++)
CMultiStorage.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.beliefbase.storage;
25 
26 import com.google.common.collect.LinkedHashMultimap;
27 import com.google.common.collect.Multimaps;
28 import com.google.common.collect.SetMultimap;
29 
30 import javax.annotation.Nonnull;
31 import java.text.MessageFormat;
32 import java.util.Collection;
33 import java.util.Map;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.stream.Stream;
36 
37 
46 public final class CMultiStorage<N, M> extends IBaseStorage<N, M>
47 {
51  private final SetMultimap<String, N> m_multielements = Multimaps.synchronizedSetMultimap( LinkedHashMultimap.create() );
55  private final Map<String, M> m_singleelements = new ConcurrentHashMap<>();
56 
57 
58  @Nonnull
59  @Override
60  public final Stream<N> streamMultiElements()
61  {
62  return m_multielements.values().stream();
63  }
64 
65  @Nonnull
66  @Override
67  public final Stream<M> streamSingleElements()
68  {
69  return m_singleelements.values().stream();
70  }
71 
72  @Override
73  public final boolean containsMultiElement( @Nonnull final String p_key )
74  {
75  return m_multielements.containsKey( p_key );
76  }
77 
78  @Override
79  public final boolean containsSingleElement( @Nonnull final String p_key )
80  {
81  return m_singleelements.containsKey( p_key );
82  }
83 
84  @Override
85  public final boolean putMultiElement( @Nonnull final String p_key, final N p_value )
86  {
87  return m_multielements.put( p_key, p_value );
88  }
89 
90  @Override
91  public final boolean putSingleElement( @Nonnull final String p_key, final M p_value )
92  {
93  return !p_value.equals( m_singleelements.put( p_key, p_value ) );
94  }
95 
96  @Override
97  public final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value )
98  {
99  return !p_value.equals( m_singleelements.putIfAbsent( p_key, p_value ) );
100  }
101 
102  @Override
103  public final boolean removeMultiElement( @Nonnull final String p_key, final N p_value )
104  {
105  return m_multielements.remove( p_key, p_value );
106  }
107 
108  @Override
109  public final boolean removeSingleElement( @Nonnull final String p_key )
110  {
111  return m_singleelements.remove( p_key ) != null;
112  }
113 
114  @Override
115  public final M getSingleElement( @Nonnull final String p_key )
116  {
117  return m_singleelements.get( p_key );
118  }
119 
120  @Override
121  public final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default )
122  {
123  return m_singleelements.getOrDefault( p_key, p_default );
124  }
125 
126  @Nonnull
127  @Override
128  public final Collection<N> getMultiElement( @Nonnull final String p_key )
129  {
130  return m_multielements.get( p_key );
131  }
132 
133  @Override
134  public final void clear()
135  {
136  m_multielements.clear();
137  m_singleelements.clear();
138  }
139 
140  @Override
141  public final boolean empty()
142  {
143  return m_multielements.isEmpty() && m_singleelements.isEmpty();
144  }
145 
146  @Override
147  public final int size()
148  {
149  return m_multielements.asMap().values().stream().mapToInt( Collection::size ).sum();
150  }
151 
152  @Override
153  public final String toString()
154  {
155  return MessageFormat.format(
156  "{0} {1}",
157  m_multielements.isEmpty() ? "" : m_multielements.values(),
158  m_singleelements.isEmpty() ? "" : m_singleelements.values()
159  ).trim();
160  }
161 }
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
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 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