dmolony-DiskBrowser/src/com/bytezone/diskbrowser/visicalc/If.java

66 lines
1.5 KiB
Java
Raw Normal View History

2016-03-09 10:38:53 +00:00
package com.bytezone.diskbrowser.visicalc;
2016-03-12 22:38:03 +00:00
class If extends Function
2016-03-09 10:38:53 +00:00
{
2016-03-12 22:33:18 +00:00
private final Condition condition;
2016-03-09 10:38:53 +00:00
private final String textTrue;
private final String textFalse;
2016-03-10 02:39:23 +00:00
private Expression expTrue;
private Expression expFalse;
2016-03-09 10:38:53 +00:00
public If (Sheet parent, String text)
{
super (parent, text);
2016-03-12 22:33:18 +00:00
int pos1 = functionText.indexOf (',');
int pos2 = functionText.indexOf (',', pos1 + 1);
2016-03-16 06:15:39 +00:00
2016-03-12 22:33:18 +00:00
condition = new Condition (parent, functionText.substring (0, pos1));
2016-03-16 06:15:39 +00:00
2016-03-12 22:33:18 +00:00
textTrue = functionText.substring (pos1 + 1, pos2);
textFalse = functionText.substring (pos2 + 1);
2016-03-09 10:38:53 +00:00
}
@Override
2016-03-17 04:40:43 +00:00
public Value calculate ()
2016-03-09 10:38:53 +00:00
{
2016-03-16 19:32:25 +00:00
valueType = ValueType.VALUE;
2016-03-19 05:58:52 +00:00
// System.out.println (functionText);
2016-03-12 22:33:18 +00:00
if (condition.getResult ())
2016-03-10 02:39:23 +00:00
{
2016-03-19 05:31:30 +00:00
// System.out.println ("true");
2016-03-10 02:39:23 +00:00
if (expTrue == null)
expTrue = new Expression (parent, textTrue);
2016-03-19 05:31:30 +00:00
expTrue.calculate ();
2016-08-01 01:22:34 +00:00
if (expTrue.is (ValueType.ERROR) || expTrue.is (ValueType.NA))
2016-03-16 19:32:25 +00:00
valueType = expTrue.getValueType ();
else
value = expTrue.getValue ();
2016-03-10 02:39:23 +00:00
}
else
{
2016-03-19 05:31:30 +00:00
// System.out.println ("false");
2016-03-10 02:39:23 +00:00
if (expFalse == null)
expFalse = new Expression (parent, textFalse);
2016-03-19 05:31:30 +00:00
expFalse.calculate ();
2016-08-01 01:22:34 +00:00
if (expFalse.is (ValueType.ERROR) || expFalse.is (ValueType.NA))
2016-03-16 19:32:25 +00:00
valueType = expFalse.getValueType ();
else
value = expFalse.getValue ();
2016-03-10 02:39:23 +00:00
}
2016-03-19 05:31:30 +00:00
2016-03-17 04:40:43 +00:00
return this;
2016-03-09 10:38:53 +00:00
}
2016-03-14 08:58:54 +00:00
@Override
public String toString ()
{
return String.format ("[IF:%s, True:%s, False:%s]", condition, textTrue, textFalse);
}
2016-03-09 10:38:53 +00:00
}