LightJason - AgentSpeak(L++)
inary/EOperator.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.language.execution.expressionbinary;
25 
29 
30 import javax.annotation.Nonnull;
31 import java.util.function.BiFunction;
32 
33 
37 public enum EOperator implements BiFunction<Number, Number, Number>
38 {
39  ASSIGNINCREMENT( "+=" ),
40  ASSIGNDECREMENT( "-=" ),
41  ASSIGNMULTIPLY( "*=" ),
42  ASSIGNDIVIDE( "/=" ),
43  ASSIGNMODULO( "%=" ),
44  ASSIGNPOW( "^=" );
45 
49  private final String m_operator;
50 
56  EOperator( final String p_operator )
57  {
58  m_operator = p_operator;
59  }
60 
61  @Override
62  public final Number apply( final Number p_lhs, final Number p_rhs )
63  {
64  switch ( this )
65  {
66  case ASSIGNINCREMENT:
67  return p_lhs.doubleValue() + p_rhs.doubleValue();
68 
69  case ASSIGNDECREMENT:
70  return p_lhs.doubleValue() - p_rhs.doubleValue();
71 
72  case ASSIGNMULTIPLY:
73  return p_lhs.doubleValue() * p_rhs.doubleValue();
74 
75  case ASSIGNDIVIDE:
76  return p_lhs.doubleValue() * p_rhs.doubleValue();
77 
78  case ASSIGNMODULO:
79  return p_lhs.longValue() % p_rhs.longValue();
80 
81  case ASSIGNPOW:
82  return Math.pow( p_lhs.doubleValue(), p_rhs.doubleValue() );
83 
84  default:
85  throw new CSyntaxErrorException( CCommon.languagestring( this, "operatorunknown", this ) );
86  }
87  }
88 
89  @Override
90  public final String toString()
91  {
92  return m_operator;
93  }
94 
101  public static EOperator from( @Nonnull final String p_value )
102  {
103  switch ( p_value )
104  {
105  case "+=":
106  return ASSIGNINCREMENT;
107 
108  case "-=":
109  return ASSIGNDECREMENT;
110 
111  case "*=":
112  return ASSIGNMULTIPLY;
113 
114  case "/=":
115  return ASSIGNDIVIDE;
116 
117  case "%=":
118  return ASSIGNMODULO;
119 
120  case "^=":
121  return ASSIGNPOW;
122 
123  default:
124  throw new CIllegalArgumentException( CCommon.languagestring( EOperator.class, "notexist", p_value ) );
125  }
126  }
127 }
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
final Number apply(final Number p_lhs, final Number p_rhs)
static EOperator from( @Nonnull final String p_value)
returns operator from string