2018-09-02 13:36:49 +02:00
package it.cavallium.warppi.math.functions ;
2017-05-16 22:02:44 +02:00
2018-09-02 13:36:49 +02:00
import it.cavallium.warppi.gui.expression.blocks.Block ;
import it.cavallium.warppi.gui.expression.blocks.BlockChar ;
import it.cavallium.warppi.math.Function ;
import it.cavallium.warppi.math.MathContext ;
import it.cavallium.warppi.math.rules.Rule ;
2018-09-12 22:16:33 +02:00
import it.cavallium.warppi.util.Error ;
2017-05-16 22:02:44 +02:00
import it.unimi.dsi.fastutil.objects.ObjectArrayList ;
2016-11-25 22:40:43 +01:00
2019-10-25 22:48:49 +02:00
import java.util.Objects ;
2016-11-25 22:40:43 +01:00
public class Variable implements Function {
protected char var ;
2017-02-14 20:02:03 +01:00
protected final MathContext root ;
2017-07-24 23:11:29 +02:00
protected V_TYPE type = V_TYPE . CONSTANT ;
2018-10-12 23:20:12 +02:00
private boolean simplified ;
2017-01-31 22:29:49 +01:00
2018-09-22 11:17:30 +02:00
public Variable ( final MathContext root , final char val , final V_TYPE type ) {
2017-01-17 22:32:40 +01:00
this . root = root ;
2016-11-25 22:40:43 +01:00
var = val ;
2017-02-06 21:38:17 +01:00
this . type = type ;
2016-11-25 22:40:43 +01:00
}
2018-09-22 11:17:30 +02:00
public Variable ( final MathContext root , final String s , final V_TYPE type ) throws Error {
2017-02-06 21:38:17 +01:00
this ( root , s . charAt ( 0 ) , type ) ;
2016-11-25 22:40:43 +01:00
}
2018-10-04 23:39:19 +02:00
/ * *
* Copy
* @param old
* @param root
* /
public Variable ( Variable old , MathContext root ) {
this . root = root ;
this . type = old . type ;
this . var = old . var ;
}
2016-11-25 22:40:43 +01:00
public char getChar ( ) {
return var ;
}
2018-09-22 11:17:30 +02:00
public Variable setChar ( final char val ) {
2017-02-06 21:38:17 +01:00
return new Variable ( root , val , type ) ;
}
public V_TYPE getType ( ) {
return type ;
}
2018-09-22 11:17:30 +02:00
public Variable setType ( final V_TYPE typ ) {
2017-02-06 21:38:17 +01:00
return new Variable ( root , var , typ ) ;
2016-11-25 22:40:43 +01:00
}
@Override
public String toString ( ) {
2017-01-31 22:29:49 +01:00
return " " + getChar ( ) ;
2016-11-25 22:40:43 +01:00
}
2017-04-10 22:50:43 +02:00
2018-10-04 23:39:19 +02:00
public static class VariableValue {
2017-01-16 17:57:09 +01:00
public final Variable v ;
public final Number n ;
2017-01-31 22:29:49 +01:00
2018-09-22 11:17:30 +02:00
public VariableValue ( final Variable v , final Number n ) {
2017-01-16 17:57:09 +01:00
this . v = v ;
this . n = n ;
}
2018-10-04 23:39:19 +02:00
/ * *
* Copy
* @param old
* @param newContext
* /
public VariableValue ( VariableValue old , MathContext newContext ) {
this . v = new Variable ( old . v , newContext ) ;
this . n = new Number ( old . n , newContext ) ;
}
2017-01-16 17:57:09 +01:00
}
2018-05-12 21:18:29 +02:00
2016-11-25 22:40:43 +01:00
@Override
2018-09-22 11:17:30 +02:00
public ObjectArrayList < Function > simplify ( final Rule rule ) throws Error , InterruptedException {
2017-12-22 22:39:58 +01:00
return rule . execute ( this ) ;
2016-11-25 22:40:43 +01:00
}
2017-01-31 22:29:49 +01:00
2016-11-25 22:40:43 +01:00
@Override
public int hashCode ( ) {
2019-10-25 22:48:49 +02:00
return Objects . hash ( var , type ) ;
2016-11-25 22:40:43 +01:00
}
2017-01-31 22:29:49 +01:00
2016-11-25 22:40:43 +01:00
@Override
2018-09-22 11:17:30 +02:00
public boolean equals ( final Object o ) {
2018-09-28 11:39:28 +02:00
if ( o instanceof Variable ) {
2017-02-06 21:38:17 +01:00
return ( ( Variable ) o ) . getChar ( ) = = var & & ( ( Variable ) o ) . getType ( ) = = type ;
2018-09-28 11:39:28 +02:00
}
2016-11-25 22:40:43 +01:00
return false ;
}
@Override
2017-02-14 20:02:03 +01:00
public MathContext getMathContext ( ) {
2017-01-17 22:32:40 +01:00
return root ;
2016-11-25 22:40:43 +01:00
}
2017-04-10 22:50:43 +02:00
2017-02-14 20:02:03 +01:00
@Override
public Variable clone ( ) {
return new Variable ( root , var , type ) ;
}
2017-04-10 22:50:43 +02:00
2018-10-04 23:39:19 +02:00
@Override
public Variable clone ( MathContext c ) {
return new Variable ( c , var , type ) ;
}
2017-02-06 21:38:17 +01:00
public static enum V_TYPE {
2017-07-24 23:11:29 +02:00
CONSTANT , VARIABLE , SOLUTION
2017-02-06 21:38:17 +01:00
}
2017-02-14 20:02:03 +01:00
@Override
2018-09-22 11:17:30 +02:00
public Function setParameter ( final int index , final Function var ) throws IndexOutOfBoundsException {
2017-02-14 20:02:03 +01:00
throw new IndexOutOfBoundsException ( ) ;
}
@Override
2018-09-22 11:17:30 +02:00
public Function getParameter ( final int index ) throws IndexOutOfBoundsException {
2017-02-14 20:02:03 +01:00
throw new IndexOutOfBoundsException ( ) ;
}
2017-05-26 22:37:18 +02:00
@Override
2018-09-22 11:17:30 +02:00
public ObjectArrayList < Block > toBlock ( final MathContext context ) {
2018-05-12 21:18:29 +02:00
final ObjectArrayList < Block > result = new ObjectArrayList < > ( ) ;
2017-05-26 22:37:18 +02:00
//TODO: Temporary solution. In near future Variables will be distint objects and they will have a color. So they will be no longer a BlockChar/FeatureChar
result . add ( new BlockChar ( getChar ( ) ) ) ;
return result ;
}
2018-10-06 10:31:34 +02:00
@Override
2019-08-10 20:51:29 +02:00
public < Argument , Result > Result accept ( final Function . Visitor < Argument , Result > visitor , final Argument argument ) {
return visitor . visit ( this , argument ) ;
2018-10-06 10:31:34 +02:00
}
2016-11-25 22:40:43 +01:00
}