CRemove.java

  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. package org.lightjason.agentspeak.action.builtin.storage;

  24. import org.lightjason.agentspeak.agent.IAgent;
  25. import org.lightjason.agentspeak.language.CCommon;
  26. import org.lightjason.agentspeak.language.CRawTerm;
  27. import org.lightjason.agentspeak.language.ITerm;
  28. import org.lightjason.agentspeak.language.execution.IContext;
  29. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  30. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  31. import javax.annotation.Nonnegative;
  32. import javax.annotation.Nonnull;
  33. import javax.annotation.Nullable;
  34. import java.util.List;
  35. import java.util.function.Function;
  36. import java.util.stream.Stream;


  37. /**
  38.  * removes an element by name from the storage.
  39.  * The actions removes any value from the storage
  40.  * which is referenced by the key and returns the
  41.  * value, the action never fails
  42.  *
  43.  * {@code [A|B] = storage/remove("foo", "bar");}
  44.  */
  45. public final class CRemove extends IStorage
  46. {
  47.     /**
  48.      * serial id
  49.      */
  50.     private static final long serialVersionUID = 7237340367513736766L;

  51.     /**
  52.      * ctor
  53.      */
  54.     public CRemove()
  55.     {
  56.         super();
  57.     }

  58.     /**
  59.      * ctor
  60.      *
  61.      * @param p_resolver resolver function
  62.      */
  63.     public CRemove( @Nonnull final Function<String, Boolean> p_resolver )
  64.     {
  65.         super( p_resolver );
  66.     }

  67.     /**
  68.      * ctor
  69.      *
  70.      * @param p_forbidden forbidden keys
  71.      */
  72.     public CRemove( @Nullable final String... p_forbidden )
  73.     {
  74.         super( p_forbidden );
  75.     }

  76.     /**
  77.      * ctor
  78.      *
  79.      * @param p_fordbidden stream with borbidden keys
  80.      */
  81.     public CRemove( @Nonnull final Stream<String> p_fordbidden )
  82.     {
  83.         super( p_fordbidden );
  84.     }

  85.     @Nonnegative
  86.     @Override
  87.     public final int minimalArgumentNumber()
  88.     {
  89.         return 1;
  90.     }

  91.     @Nonnull
  92.     @Override
  93.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  94.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  95.     {
  96.         CCommon.flatten( p_argument )
  97.                .map( ITerm::<String>raw )
  98.                .forEach( i -> this.remove( p_context.agent(), i, p_return ) );

  99.         return CFuzzyValue.from( true );
  100.     }


  101.     /**
  102.      * removes a value from the storage
  103.      *
  104.      * @param p_agent agent
  105.      * @param p_key key
  106.      * @param p_return return arguments
  107.      */
  108.     private void remove( @Nonnull final IAgent<?> p_agent, @Nonnull final String p_key, @Nonnull final List<ITerm> p_return )
  109.     {
  110.         if ( ( m_resolver.apply( p_key ) ) || ( !p_agent.storage().containsKey( p_key ) ) )
  111.             return;

  112.         p_return.add(
  113.             CRawTerm.from(
  114.                 p_agent.storage().remove( p_key )
  115.             )
  116.         );
  117.     }

  118. }