LightJason - AgentSpeak(L++)
CSingleStorage.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 javax.annotation.Nonnull;
27 import java.text.MessageFormat;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.LinkedHashMap;
31 import java.util.Map;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35 
36 
47 public final class CSingleStorage<N, M> extends IBaseStorage<N, M>
48 {
52  private final Map<String, N> m_multielements = Collections.synchronizedMap( new LinkedHashMap<>() );
56  private final Map<String, M> m_singleelements = new ConcurrentHashMap<>();
57 
61  public CSingleStorage()
62  {
63  super();
64  }
65 
66  @Nonnull
67  @Override
68  public final Stream<N> streamMultiElements()
69  {
70  return m_multielements.values().stream();
71  }
72 
73  @Nonnull
74  @Override
75  public final Stream<M> streamSingleElements()
76  {
77  return m_singleelements.values().stream();
78  }
79 
80  @Override
81  public final boolean containsMultiElement( @Nonnull final String p_key )
82  {
83  return m_multielements.containsKey( p_key );
84  }
85 
86  @Override
87  public final boolean containsSingleElement( @Nonnull final String p_key )
88  {
89  return m_singleelements.containsKey( p_key );
90  }
91 
92  @Override
93  public final boolean putMultiElement( @Nonnull final String p_key, final N p_value )
94  {
95  return !p_value.equals( m_multielements.put( p_key, p_value ) );
96  }
97 
98  @Override
99  public final boolean putSingleElement( @Nonnull final String p_key, final M p_value )
100  {
101  return !p_value.equals( m_singleelements.put( p_key, p_value ) );
102  }
103 
104  @Override
105  public final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value )
106  {
107  return !p_value.equals( m_singleelements.putIfAbsent( p_key, p_value ) );
108  }
109 
110  @Override
111  public final boolean removeMultiElement( @Nonnull final String p_key, final N p_value )
112  {
113  return p_value.equals( m_multielements.remove( p_key ) );
114  }
115 
116  @Override
117  public final boolean removeSingleElement( @Nonnull final String p_key )
118  {
119  return m_singleelements.remove( p_key ) != null;
120  }
121 
122  @Override
123  public final M getSingleElement( @Nonnull final String p_key )
124  {
125  return m_singleelements.get( p_key );
126  }
127 
128  @Override
129  public final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default )
130  {
131  return m_singleelements.getOrDefault( p_key, p_default );
132  }
133 
134  @Nonnull
135  @Override
136  public final Collection<N> getMultiElement( @Nonnull final String p_key )
137  {
138  return Stream.of( m_multielements.get( p_key ) ).collect( Collectors.toSet() );
139  }
140 
141  @Override
142  public final void clear()
143  {
144  m_multielements.clear();
145  m_singleelements.clear();
146  }
147 
148  @Override
149  public final boolean empty()
150  {
151  return m_multielements.isEmpty() && m_singleelements.isEmpty();
152  }
153 
154  @Override
155  public final int size()
156  {
157  return m_multielements.size();
158  }
159 
160 
161  @Override
162  public final String toString()
163  {
164  return MessageFormat.format( "[multi elements: {0}, single elements: {1}]", m_multielements, m_singleelements );
165  }
166 
167 }
final Map< String, N > m_multielements
map with elements
final M getSingleElement( @Nonnull final String p_key)
returns a single-element by the name
final boolean putMultiElement( @Nonnull final String p_key, final N p_value)
puts a multi-element into the storage
final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default)
returns a single-element by the name
final boolean containsMultiElement( @Nonnull final String p_key)
contains a multi-element
final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value)
puts a single-element if it is absent
final Stream< M > streamSingleElements()
returns a stream over all single-elements
final Collection< N > getMultiElement( @Nonnull final String p_key)
returns a collection of multi-elementy by name
final boolean containsSingleElement( @Nonnull final String p_key)
contains a single-element
final Stream< N > streamMultiElements()
returns a stream over all multi-elements
final boolean removeMultiElement( @Nonnull final String p_key, final N p_value)
removes a multi-element from the storage
final Map< String, M > m_singleelements
map with single elements
thread-safe storage of the data of single- and multi-elements, a multi-element can be stored once and...
final boolean removeSingleElement( @Nonnull final String p_key)
removes a single-element from the storage
final boolean empty()
checks if a storage is empty
final boolean putSingleElement( @Nonnull final String p_key, final M p_value)
puts a single-element into the storage