2017-03-14 11:28:52 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
2017-03-16 00:27:45 +00:00
|
|
|
import java.util.ArrayList;
|
2017-03-14 11:28:52 +00:00
|
|
|
import java.util.Iterator;
|
2017-03-16 00:27:45 +00:00
|
|
|
import java.util.List;
|
2017-03-15 00:41:45 +00:00
|
|
|
import java.util.regex.Pattern;
|
2017-03-14 11:28:52 +00:00
|
|
|
|
2017-03-16 00:27:45 +00:00
|
|
|
public class ValueList implements Iterable<Value>
|
2017-03-14 11:28:52 +00:00
|
|
|
{
|
2017-03-15 00:41:45 +00:00
|
|
|
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
|
|
|
|
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
|
|
|
|
|
2017-03-14 11:28:52 +00:00
|
|
|
private final Sheet parent;
|
2017-03-16 00:27:45 +00:00
|
|
|
protected List<Value> values = new ArrayList<Value> ();
|
2017-03-14 11:28:52 +00:00
|
|
|
|
2017-03-16 00:27:45 +00:00
|
|
|
public ValueList (Sheet parent, Cell cell, String text)
|
2017-03-14 11:28:52 +00:00
|
|
|
{
|
|
|
|
this.parent = parent;
|
2017-03-15 00:41:45 +00:00
|
|
|
|
2017-03-15 07:07:36 +00:00
|
|
|
int ptr = 0;
|
|
|
|
while (ptr < text.length ())
|
2017-03-15 00:41:45 +00:00
|
|
|
{
|
2017-03-16 00:27:45 +00:00
|
|
|
if (text.charAt (ptr) == '@') // function
|
2017-03-15 07:07:36 +00:00
|
|
|
{
|
|
|
|
String functionText = Expression.getBalancedText (text.substring (ptr));
|
|
|
|
Value v = new Expression (parent, cell, functionText).reduce ();
|
|
|
|
values.add (v);
|
|
|
|
ptr += functionText.length ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String item = getNextItem (text, ptr);
|
|
|
|
int pos = item.indexOf ("...");
|
2017-03-16 00:27:45 +00:00
|
|
|
if (pos > 0) // range
|
2017-03-15 07:07:36 +00:00
|
|
|
{
|
2017-03-16 00:27:45 +00:00
|
|
|
Address from = new Address (item.substring (0, pos));
|
|
|
|
Address to = new Address (item.substring (pos + 3));
|
2017-03-15 07:07:36 +00:00
|
|
|
Range range = new Range (parent, from, to);
|
2017-03-15 00:41:45 +00:00
|
|
|
|
2017-03-15 07:07:36 +00:00
|
|
|
for (Address address : range)
|
|
|
|
values.add (parent.getCell (address));
|
|
|
|
}
|
2017-03-16 00:27:45 +00:00
|
|
|
else // cell/number/expression
|
2017-03-15 07:07:36 +00:00
|
|
|
{
|
|
|
|
Value v = new Expression (parent, cell, item).reduce ();
|
|
|
|
values.add (v);
|
|
|
|
}
|
|
|
|
ptr += item.length ();
|
|
|
|
}
|
2017-03-15 00:41:45 +00:00
|
|
|
|
2017-03-15 07:07:36 +00:00
|
|
|
if (ptr < text.length () && text.charAt (ptr) == ',')
|
|
|
|
ptr++;
|
|
|
|
if (ptr < text.length () && text.charAt (ptr) == ')')
|
2017-03-16 00:27:45 +00:00
|
|
|
break;
|
2017-03-15 00:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:27:45 +00:00
|
|
|
// return substring of text from ptr up to the next comma
|
2017-03-15 07:07:36 +00:00
|
|
|
private String getNextItem (String text, int ptr)
|
|
|
|
{
|
|
|
|
int p = ptr;
|
|
|
|
while (++p < text.length () && text.charAt (p) != ',')
|
|
|
|
;
|
|
|
|
return text.substring (ptr, p);
|
|
|
|
}
|
|
|
|
|
2017-03-15 00:41:45 +00:00
|
|
|
public int size ()
|
|
|
|
{
|
|
|
|
return values.size ();
|
2017-03-14 11:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-15 00:41:45 +00:00
|
|
|
public Iterator<Value> iterator ()
|
2017-03-14 11:28:52 +00:00
|
|
|
{
|
2017-03-15 00:41:45 +00:00
|
|
|
return values.iterator ();
|
2017-03-14 11:28:52 +00:00
|
|
|
}
|
|
|
|
}
|