24 package org.lightjason.agentspeak.beliefbase.storage;
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;
39 import java.util.Objects;
40 import java.util.function.Function;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
70 this( p_instance, i -> i );
79 public CClassStorage( @Nonnull
final Object p_instance, @Nonnull
final Function<String, String> p_fieldnameformater )
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 )
97 return m_fields.entrySet().stream()
98 .map( i -> this.
literal( i.getKey(), i.getValue() ) )
99 .filter( Objects::nonNull );
106 return Stream.empty();
112 return m_fields.containsKey( p_key );
124 final Field l_field = m_fields.get( p_key );
125 if ( ( Objects.isNull( l_field ) ) || ( p_value.
emptyValues() ) || ( Modifier.isFinal( l_field.getModifiers() ) ) )
130 l_field.set( m_instance, p_value.
values().findFirst().orElseGet( () ->
CRawTerm.
from( null ) ).raw() );
133 catch (
final IllegalAccessException l_exception )
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() );
191 return m_fields.isEmpty();
197 return m_fields.size();
208 private ILiteral literal( @Nonnull
final String p_name, @Nonnull
final Field p_field )
212 final Object l_value = p_field.get( m_instance );
215 catch (
final IllegalAccessException l_exception )
final Stream< M > streamSingleElements()
final Object m_instance
object instance
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)
default structure of a storage
final boolean putSingleElementIfAbsent( @Nonnull final String p_key, final M p_value)
final Stream< ILiteral > streamMultiElements()
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
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 ...
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
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