LightJason - AgentSpeak(L++)
CClassStorage.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 
29 
30 import javax.annotation.Nonnull;
31 import javax.annotation.Nullable;
32 import java.lang.reflect.Field;
33 import java.lang.reflect.Modifier;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.Locale;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.function.Function;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43 
44 
52 public final class CClassStorage<M> extends IBaseStorage<ILiteral, M>
53 {
57  private final Object m_instance;
61  private final Map<String, Field> m_fields;
62 
68  public CClassStorage( @Nonnull final Object p_instance )
69  {
70  this( p_instance, i -> i );
71  }
72 
79  public CClassStorage( @Nonnull final Object p_instance, @Nonnull final Function<String, String> p_fieldnameformater )
80  {
81  m_instance = p_instance;
82  m_fields = Collections.unmodifiableMap(
83  Arrays.stream( m_instance.getClass().getDeclaredFields() )
84  .peek( i -> i.setAccessible( true ) )
85  .filter( i -> !Modifier.isTransient( i.getModifiers() ) )
86  .filter( i -> !Modifier.isStatic( i.getModifiers() ) )
87  .collect( Collectors.toMap(
88  i -> p_fieldnameformater.apply( i.getName() ).toLowerCase( Locale.ROOT ).replace( "\\s+", "" ), i -> i )
89  )
90  );
91  }
92 
93  @Nonnull
94  @Override
95  public final Stream<ILiteral> streamMultiElements()
96  {
97  return m_fields.entrySet().stream()
98  .map( i -> this.literal( i.getKey(), i.getValue() ) )
99  .filter( Objects::nonNull );
100  }
101 
102  @Nonnull
103  @Override
104  public final Stream<M> streamSingleElements()
105  {
106  return Stream.empty();
107  }
108 
109  @Override
110  public final boolean containsMultiElement( @Nonnull final String p_key )
111  {
112  return m_fields.containsKey( p_key );
113  }
114 
115  @Override
116  public final boolean containsSingleElement( @Nonnull final String p_key )
117  {
118  return false;
119  }
120 
121  @Override
122  public final boolean putMultiElement( @Nonnull final String p_key, final ILiteral p_value )
123  {
124  final Field l_field = m_fields.get( p_key );
125  if ( ( Objects.isNull( l_field ) ) || ( p_value.emptyValues() ) || ( Modifier.isFinal( l_field.getModifiers() ) ) )
126  return false;
127 
128  try
129  {
130  l_field.set( m_instance, p_value.values().findFirst().orElseGet( () -> CRawTerm.from( null ) ).raw() );
131  return true;
132  }
133  catch ( final IllegalAccessException l_exception )
134  {
135  return false;
136  }
137  }
138 
139  @Override
140  public final boolean putSingleElement( @Nonnull final String p_key, final M p_value )
141  {
142  return false;
143  }
144 
145  @Override
146  public final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value )
147  {
148  return false;
149  }
150 
151  @Override
152  public final boolean removeMultiElement( @Nonnull final String p_key, final ILiteral p_value )
153  {
154  return false;
155  }
156 
157  @Override
158  public final boolean removeSingleElement( @Nonnull final String p_key )
159  {
160  return false;
161  }
162 
163  @Override
164  public final M getSingleElement( @Nonnull final String p_key )
165  {
166  return null;
167  }
168 
169  @Override
170  public final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default )
171  {
172  return p_default;
173  }
174 
175  @Nonnull
176  @Override
177  public Collection<ILiteral> getMultiElement( @Nonnull final String p_key )
178  {
179  final Field l_field = m_fields.get( p_key );
180  return Objects.isNull( l_field ) ? Collections.emptySet() : Stream.of( this.literal( p_key, l_field ) ).collect( Collectors.toSet() );
181  }
182 
183  @Override
184  public final void clear()
185  {
186  }
187 
188  @Override
189  public final boolean empty()
190  {
191  return m_fields.isEmpty();
192  }
193 
194  @Override
195  public final int size()
196  {
197  return m_fields.size();
198  }
199 
207  @Nullable
208  private ILiteral literal( @Nonnull final String p_name, @Nonnull final Field p_field )
209  {
210  try
211  {
212  final Object l_value = p_field.get( m_instance );
213  return Objects.isNull( l_value ) ? CLiteral.from( p_name, CRawTerm.EMPTY ) : CLiteral.from( p_name, CRawTerm.from( l_value ) );
214  }
215  catch ( final IllegalAccessException l_exception )
216  {
217  return null;
218  }
219  }
220 
221 }
final boolean removeMultiElement( @Nonnull final String p_key, final ILiteral p_value)
Collection< ILiteral > getMultiElement( @Nonnull final String p_key)
final M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default)
final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value)
CClassStorage( @Nonnull final Object p_instance, @Nonnull final Function< String, String > p_fieldnameformater)
ctor
CClassStorage( @Nonnull final Object p_instance)
ctor
Stream< ITerm > values(final IPath... p_path)
returns a stream over value items
static final ITerm EMPTY
empty raw term
Definition: CRawTerm.java:50
ILiteral literal( @Nonnull final String p_name, @Nonnull final Field p_field)
returns a literal definition of the a class field
final boolean putSingleElement( @Nonnull final String p_key, final M p_value)
final boolean putMultiElement( @Nonnull final String p_key, final ILiteral p_value)
final Map< String, Field > m_fields
map with functor and field reference
boolean emptyValues()
check for empty values
final boolean containsMultiElement( @Nonnull final String p_key)
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
final boolean containsSingleElement( @Nonnull final String p_key)
belief storage to get access to all class attributes
final M getSingleElement( @Nonnull final String p_key)
final boolean removeSingleElement( @Nonnull final String p_key)
term structure for simple datatypes
Definition: CRawTerm.java:45