LightJason - AgentSpeak(L++)
CSingleOnlyStorage.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.util.Collection;
28 import java.util.Collections;
29 import java.util.Map;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.stream.Stream;
32 
33 
37 public final class CSingleOnlyStorage<N, M> extends IBaseStorage<N, M>
38 {
42  private final Map<String, M> m_elements = new ConcurrentHashMap<>();
43 
44  @Nonnull
45  @Override
46  public final Stream<N> streamMultiElements()
47  {
48  return Stream.empty();
49  }
50 
51  @Nonnull
52  @Override
53  public Stream<M> streamSingleElements()
54  {
55  return m_elements.values().stream();
56  }
57 
58  @Override
59  public final boolean containsMultiElement( @Nonnull final String p_key )
60  {
61  return false;
62  }
63 
64  @Override
65  public final boolean containsSingleElement( @Nonnull final String p_key )
66  {
67  return m_elements.containsKey( p_key );
68  }
69 
70  @Override
71  public final boolean putMultiElement( @Nonnull final String p_key, final N p_value )
72  {
73  return false;
74  }
75 
76  @Override
77  public final boolean putSingleElement( @Nonnull final String p_key, final M p_value )
78  {
79  return !p_value.equals( m_elements.put( p_key, p_value ) );
80  }
81 
82  @Override
83  public final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value )
84  {
85  return !p_value.equals( m_elements.putIfAbsent( p_key, p_value ) );
86  }
87 
88  @Override
89  public final boolean removeMultiElement( @Nonnull final String p_key, final N p_value )
90  {
91  return false;
92  }
93 
94  @Override
95  public final boolean removeSingleElement( @Nonnull final String p_key )
96  {
97  return m_elements.remove( p_key ) != null;
98  }
99 
100  @Override
101  public final M getSingleElement( @Nonnull final String p_key )
102  {
103  return m_elements.get( p_key );
104  }
105 
106  @Override
107  public final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default )
108  {
109  return m_elements.getOrDefault( p_key, p_default );
110  }
111 
112  @Nonnull
113  @Override
114  public final Collection<N> getMultiElement( @Nonnull final String p_key )
115  {
116  return Collections.emptySet();
117  }
118 
119  @Override
120  public final void clear()
121  {
122  m_elements.clear();
123  }
124 
125  @Override
126  public final boolean empty()
127  {
128  return m_elements.isEmpty();
129  }
130 
131  @Override
132  public final int size()
133  {
134  return 0;
135  }
136 }
final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value)
puts a single-element if it is absent
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 putMultiElement( @Nonnull final String p_key, final N p_value)
puts a multi-element into the storage
final M getSingleElement( @Nonnull final String p_key)
returns a single-element by the name
final boolean putSingleElement( @Nonnull final String p_key, final M p_value)
puts a single-element into the storage
final boolean removeMultiElement( @Nonnull final String p_key, final N p_value)
removes a multi-element from the storage
final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default)
returns a single-element by the name
final boolean removeSingleElement( @Nonnull final String p_key)
removes a single-element from the storage
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 Stream< N > streamMultiElements()
returns a stream over all multi-elements
final Map< String, M > m_elements
map with single elements