2016-03-04 03:56:28 +00:00
|
|
|
package com.bytezone.diskbrowser.visicalc;
|
|
|
|
|
|
|
|
import java.security.InvalidParameterException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2017-02-18 09:54:24 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2016-03-04 03:56:28 +00:00
|
|
|
|
|
|
|
class Range implements Iterable<Address>
|
|
|
|
{
|
2017-02-18 09:54:24 +00:00
|
|
|
private static final Pattern rangePattern =
|
|
|
|
Pattern.compile ("([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})");
|
|
|
|
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
|
|
|
|
|
2016-03-04 03:56:28 +00:00
|
|
|
Address from, to;
|
|
|
|
List<Address> range = new ArrayList<Address> ();
|
|
|
|
|
2017-02-18 09:54:24 +00:00
|
|
|
public Range (String rangeText)
|
|
|
|
{
|
|
|
|
setRange (rangeText);
|
|
|
|
}
|
|
|
|
|
2016-03-04 03:56:28 +00:00
|
|
|
public Range (Address from, Address to)
|
|
|
|
{
|
|
|
|
this.from = from;
|
|
|
|
this.to = to;
|
|
|
|
|
2017-02-18 09:54:24 +00:00
|
|
|
addRange ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Range (String[] cells)
|
|
|
|
{
|
|
|
|
for (String s : cells)
|
|
|
|
range.add (new Address (s));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addRange ()
|
|
|
|
{
|
2016-03-04 03:56:28 +00:00
|
|
|
range.add (from);
|
2017-02-18 09:54:24 +00:00
|
|
|
Address tempFrom = from;
|
2016-03-04 03:56:28 +00:00
|
|
|
|
|
|
|
if (from.row == to.row)
|
|
|
|
while (from.compareTo (to) < 0)
|
|
|
|
{
|
|
|
|
from = from.nextColumn ();
|
|
|
|
range.add (from);
|
|
|
|
}
|
|
|
|
else if (from.column == to.column)
|
|
|
|
while (from.compareTo (to) < 0)
|
|
|
|
{
|
|
|
|
from = from.nextRow ();
|
|
|
|
range.add (from);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new InvalidParameterException ();
|
2017-02-18 09:54:24 +00:00
|
|
|
from = tempFrom;
|
2016-03-04 03:56:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 04:37:01 +00:00
|
|
|
boolean isHorizontal ()
|
|
|
|
{
|
|
|
|
Address first = range.get (0);
|
|
|
|
Address last = range.get (range.size () - 1);
|
|
|
|
return first.row == last.row;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isVertical ()
|
|
|
|
{
|
|
|
|
Address first = range.get (0);
|
|
|
|
Address last = range.get (range.size () - 1);
|
|
|
|
return first.column == last.column;
|
|
|
|
}
|
|
|
|
|
2017-02-18 09:54:24 +00:00
|
|
|
@Override
|
|
|
|
public Iterator<Address> iterator ()
|
|
|
|
{
|
|
|
|
return range.iterator ();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setRange (String text)
|
|
|
|
{
|
|
|
|
Matcher m = rangePattern.matcher (text);
|
|
|
|
if (m.find ())
|
|
|
|
{
|
|
|
|
from = new Address (m.group (1), m.group (2));
|
|
|
|
to = new Address (m.group (3), m.group (4));
|
|
|
|
addRange ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = addressList.matcher (text);
|
|
|
|
if (m.find ())
|
|
|
|
{
|
|
|
|
System.out.printf ("Address list:%s%n", text);
|
|
|
|
String[] cells = m.group (1).split (",");
|
|
|
|
for (String s : cells)
|
|
|
|
range.add (new Address (s));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pos = text.indexOf ("...");
|
|
|
|
if (pos > 0)
|
|
|
|
{
|
|
|
|
String fromAddress = text.substring (0, pos);
|
|
|
|
String toAddress = text.substring (pos + 3);
|
|
|
|
from = new Address (fromAddress);
|
|
|
|
to = new Address (toAddress);
|
|
|
|
addRange ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.printf ("null range [%s]%n", text);
|
|
|
|
}
|
|
|
|
|
2016-03-04 03:56:28 +00:00
|
|
|
@Override
|
|
|
|
public String toString ()
|
|
|
|
{
|
|
|
|
if (from == null || to == null)
|
|
|
|
{
|
|
|
|
StringBuilder text = new StringBuilder ();
|
|
|
|
for (Address address : range)
|
|
|
|
text.append (address.text + ",");
|
|
|
|
if (text.length () > 0)
|
|
|
|
text.deleteCharAt (text.length () - 1);
|
|
|
|
return text.toString ();
|
|
|
|
}
|
|
|
|
return String.format (" %s -> %s", from.text, to.text);
|
|
|
|
}
|
|
|
|
}
|