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

130 lines
4.3 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
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
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
class Range implements Iterable<Address>
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
2017-03-26 09:16:36 +00:00
// private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
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})");
2017-03-26 09:16:36 +00:00
// private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
2017-02-18 09:54:24 +00:00
2017-03-03 23:41:08 +00:00
private Address from, to;
2020-02-02 10:17:49 +00:00
private final List<Address> range = new ArrayList<> ();
private final Cell cell;
2016-03-04 03:56:28 +00:00
2017-03-15 00:41:45 +00:00
private boolean isHorizontal;
2017-02-18 09:54:24 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
Range (Cell cell, String rangeText)
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
this.cell = cell;
2016-03-04 03:56:28 +00:00
2017-03-15 00:41:45 +00:00
Matcher m = rangePattern.matcher (rangeText);
if (m.find ())
{
from = new Address (m.group (1), m.group (2));
to = new Address (m.group (3), m.group (4));
isHorizontal = from.rowMatches (to);
populateRange ();
}
else
2017-03-15 07:07:36 +00:00
throw new IllegalArgumentException (rangeText);
2017-02-18 09:54:24 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-15 00:41:45 +00:00
private void populateRange ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-18 09:54:24 +00:00
{
2016-03-04 03:56:28 +00:00
range.add (from);
cell.getCell (from);
2017-02-18 09:54:24 +00:00
Address tempFrom = from;
2016-03-04 03:56:28 +00:00
2017-03-03 23:41:08 +00:00
if (from.rowMatches (to))
2016-03-04 03:56:28 +00:00
while (from.compareTo (to) < 0)
{
from = from.nextColumn ();
range.add (from);
cell.getCell (from);
2016-03-04 03:56:28 +00:00
}
2017-03-03 23:41:08 +00:00
else if (from.columnMatches (to))
2016-03-04 03:56:28 +00:00
while (from.compareTo (to) < 0)
{
from = from.nextRow ();
range.add (from);
cell.getCell (from);
2016-03-04 03:56:28 +00:00
}
else
2017-03-26 09:16:36 +00:00
throw new IllegalArgumentException (
"Cannot create range " + from.getText () + ", " + to.getText ());
2017-03-03 10:24:23 +00:00
2017-02-18 09:54:24 +00:00
from = tempFrom;
2016-03-04 03:56:28 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-18 02:22:49 +00:00
static boolean isRange (String text)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-18 02:22:49 +00:00
{
return rangePattern.matcher (text).matches ();
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-07 04:37:01 +00:00
boolean isHorizontal ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-07 04:37:01 +00:00
{
2017-03-15 00:41:45 +00:00
return isHorizontal;
2016-03-07 04:37:01 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-07 04:37:01 +00:00
boolean isVertical ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-07 04:37:01 +00:00
{
2017-03-15 00:41:45 +00:00
return !isHorizontal;
2016-03-07 04:37:01 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-18 09:54:24 +00:00
@Override
public Iterator<Address> iterator ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-18 09:54:24 +00:00
{
return range.iterator ();
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 10:24:23 +00:00
public int size ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 10:24:23 +00:00
{
2017-03-15 00:41:45 +00:00
return range.size ();
2017-03-03 10:24:23 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-16 00:27:45 +00:00
public Address get (int index)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-16 00:27:45 +00:00
{
2017-03-16 01:44:26 +00:00
return index < 0 || index >= range.size () ? null : range.get (index);
2017-03-16 00:27:45 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
@Override
public String toString ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
if (from == null || to == null)
{
StringBuilder text = new StringBuilder ();
for (Address address : range)
2017-03-03 23:41:08 +00:00
text.append (address.getText () + ",");
2016-03-04 03:56:28 +00:00
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
2017-03-03 23:41:08 +00:00
return String.format (" %s -> %s", from.getText (), to.getText ());
2016-03-04 03:56:28 +00:00
}
}