CSubList.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.collection.list;

  24. import com.codepoetics.protonpack.StreamUtils;
  25. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  26. import org.lightjason.agentspeak.language.CCommon;
  27. import org.lightjason.agentspeak.language.CRawTerm;
  28. import org.lightjason.agentspeak.language.ITerm;
  29. import org.lightjason.agentspeak.language.execution.IContext;
  30. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  31. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  32. import javax.annotation.Nonnegative;
  33. import javax.annotation.Nonnull;
  34. import java.util.Collections;
  35. import java.util.List;
  36. import java.util.stream.Collectors;
  37. import java.util.stream.Stream;


  38. /**
  39.  * returns a sublist within the index range.
  40.  * Creates a sublist of an existing list by an index range,
  41.  * first argument is the list object, all other arguments
  42.  * are tuples of ranges \f$ [ \text{lower-bound}, \text{upper-bound} ) \f$,
  43.  * the action fails on an wrong number of arguments
  44.  *
  45.  * {@code [L1|L2] = collection/list/get( L, 2, 5, [4, 6] );}
  46.  */
  47. public final class CSubList extends IBuiltinAction
  48. {
  49.     /**
  50.      * serial id
  51.      */
  52.     private static final long serialVersionUID = 5029899221891965636L;

  53.     /**
  54.      * ctor
  55.      */
  56.     public CSubList()
  57.     {
  58.         super( 3 );
  59.     }

  60.     @Nonnegative
  61.     @Override
  62.     public final int minimalArgumentNumber()
  63.     {
  64.         return 1;
  65.     }

  66.     @Nonnull
  67.     @Override
  68.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  69.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  70.     {
  71.         final List<ITerm> l_arguments = Stream.concat( Stream.of( p_argument.get( 0 ) ), CCommon.flatten( p_argument.stream().skip( 1 ) ) )
  72.                                               .collect( Collectors.toList() );

  73.         if ( ( l_arguments.size() % 2 == 0 ) || ( l_arguments.size() < 3 ) )
  74.             return CFuzzyValue.from( false );

  75.         StreamUtils.windowed(
  76.             l_arguments.stream()
  77.                        .skip( 1 )
  78.                        .map( ITerm::<Number>raw )
  79.                        .map( Number::intValue ),
  80.             2,
  81.             2
  82.         )
  83.             .map( i -> l_arguments.get( 0 ).<List<?>>raw().subList( i.get( 0 ), i.get( 1 ) ) )
  84.             .map( i -> p_parallel ? Collections.synchronizedList( i ) : i )
  85.             .map( CRawTerm::from )
  86.             .forEach( p_return::add );

  87.         return CFuzzyValue.from( true );
  88.     }

  89. }