CParseNumber.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.generic.type;

  24. import org.lightjason.agentspeak.language.CRawTerm;
  25. import org.lightjason.agentspeak.language.ITerm;

  26. import javax.annotation.Nonnull;
  27. import java.util.AbstractMap;
  28. import java.util.Map;


  29. /**
  30.  * action for parsing a number from string.
  31.  * Parses each argument to a number value
  32.  * and returns the value, the action fails on
  33.  * parsing errors
  34.  *
  35.  * {@code [X|Y|Z] = generic/type/parsenumber( "1.45", ["8.88", "9"] );}
  36.  */
  37. public final class CParseNumber extends IParse
  38. {
  39.     /**
  40.      * serial id
  41.      */
  42.     private static final long serialVersionUID = 1809811719927824635L;

  43.     /**
  44.      * parses the input string
  45.      *
  46.      * @param p_value string value
  47.      * @return tuple with boolean (for parsing error) and term
  48.      */
  49.     @Nonnull
  50.     protected final Map.Entry<Boolean, ITerm> parse( @Nonnull final String p_value )
  51.     {
  52.         try
  53.         {
  54.             return new AbstractMap.SimpleImmutableEntry<>( true, CRawTerm.from( Double.parseDouble( p_value ) ) );
  55.         }
  56.         catch ( final Exception l_exception )
  57.         {
  58.             return new AbstractMap.SimpleImmutableEntry<>( false, CRawTerm.from( null ) );
  59.         }
  60.     }

  61. }