LightJason - AgentSpeak(L++)
CPrint.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.action.builtin.generic;
25 
33 
34 import javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
36 import java.io.ObjectInputStream;
37 import java.io.PrintStream;
38 import java.io.Serializable;
39 import java.text.MessageFormat;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Objects;
46 import java.util.Set;
47 import java.util.stream.Collectors;
48 
49 
57 public final class CPrint extends IBuiltinAction
58 {
62  private static final long serialVersionUID = -4271829260928469828L;
66  private transient PrintStream m_stream;
74  private final String m_seperator;
78  private final Set<IFormatter<?>> m_formatter;
79 
80 
87  public CPrint() throws Exception
88  {
89  this( () -> System.out, " ", new CFormat2D(), new CFormat1D() );
90  }
91 
98  public CPrint( @Nonnull final ISupplier<PrintStream> p_streamsupplier ) throws Exception
99  {
100  this( p_streamsupplier, " ", new CFormat2D(), new CFormat1D() );
101  }
102 
111  public CPrint( @Nonnull final ISupplier<PrintStream> p_streamsupplier, @Nonnull final String p_seperator, @Nullable final IFormatter<?>... p_formatter )
112  throws Exception
113  {
114  super( 2 );
115  m_streamsupplier = p_streamsupplier;
116  m_stream = m_streamsupplier.get();
117  m_seperator = p_seperator;
118  m_formatter = p_formatter != null ? new HashSet<>( Arrays.asList( p_formatter ) ) : Collections.emptySet();
119  }
120 
128  private void readObject( final ObjectInputStream p_stream ) throws Exception
129  {
130  p_stream.defaultReadObject();
131  m_stream = m_streamsupplier.get();
132  }
133 
139  public final Set<IFormatter<?>> formatter()
140  {
141  return m_formatter;
142  }
143 
144  @Nonnull
145  @Override
146  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
147  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
148  )
149  {
150  m_stream.println( MessageFormat.format( "{0}", this.format( p_argument ) ) );
151  return CFuzzyValue.from( true );
152  }
153 
160  @Nonnull
161  private String format( @Nonnull final Collection<ITerm> p_argument )
162  {
163  return p_argument.stream()
164  .map( ITerm::raw )
165  .map( i ->
166  {
167  if ( Objects.isNull( i ) )
168  return "";
169 
170  final IFormatter<?> l_formatter = m_formatter.parallelStream()
171  .filter( j -> j.isAssignableTo( i.getClass() ) )
172  .limit( 1 )
173  .findFirst()
174  .orElse( null );
175 
176  return Objects.isNull( l_formatter ) ? i.toString() : l_formatter.toString( i );
177  } )
178  .collect( Collectors.joining( m_seperator ) );
179  }
180 
185  @FunctionalInterface
186  public interface ISupplier<T> extends Serializable
187  {
194  T get() throws Exception;
195  }
196 
197 
203  public abstract static class IFormatter<T> implements Serializable
204  {
208  private static final long serialVersionUID = -4997526550642055213L;
209 
216  public final boolean isAssignableTo( final Class<?> p_class )
217  {
218  return this.getType().isAssignableFrom( p_class );
219  }
220 
221  @Override
222  public final int hashCode()
223  {
224  return this.getType().hashCode();
225  }
226 
227  @Override
228  public final boolean equals( final Object p_object )
229  {
230  return ( p_object instanceof IFormatter<?> ) && ( this.hashCode() == p_object.hashCode() );
231  }
232 
239  @SuppressWarnings( "unchecked" )
240  public final String toString( final Object p_data )
241  {
242  return this.format( (T) p_data );
243  }
244 
250  protected abstract Class<?> getType();
251 
258  protected abstract String format( final T p_data );
259  }
260 
261 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
final boolean isAssignableTo(final Class<?> p_class)
checks if a type is assigneable
Definition: CPrint.java:216
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CPrint.java:146
void readObject(final ObjectInputStream p_stream)
deserializable call
Definition: CPrint.java:128
transient PrintStream m_stream
output stream
Definition: CPrint.java:66
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
final Set< IFormatter<?> > m_formatter
list mit individual format calls
Definition: CPrint.java:78
execution context with local data
Definition: IContext.java:42
final Set< IFormatter<?> > formatter()
returns the formatter list
Definition: CPrint.java:139
final ISupplier< PrintStream > m_streamsupplier
supplier of print stream field
Definition: CPrint.java:70
result for an immutable fuzzy value
final String m_seperator
argument seperator
Definition: CPrint.java:74
< T > T raw()
cast to any raw value type
String format( @Nonnull final Collection< ITerm > p_argument)
format definition
Definition: CPrint.java:161
final String toString(final Object p_data)
to string
Definition: CPrint.java:240
CPrint( @Nonnull final ISupplier< PrintStream > p_streamsupplier)
ctor
Definition: CPrint.java:98
CPrint( @Nonnull final ISupplier< PrintStream > p_streamsupplier, @Nonnull final String p_seperator, @Nullable final IFormatter<?>... p_formatter)
ctor
Definition: CPrint.java:111