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

186 lines
6.3 KiB
Java
Raw Normal View History

2016-03-04 03:56:28 +00:00
package com.bytezone.diskbrowser.visicalc;
2017-02-18 09:54:24 +00:00
import java.util.Arrays;
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
class Address implements Comparable<Address>
2020-02-10 11:05:40 +00:00
// -----------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
2016-03-04 05:27:14 +00:00
private static final int MAX_ROWS = 255;
private static final int MAX_COLUMNS = 64;
2017-03-03 23:41:08 +00:00
private int row, column;
private int rowKey;
private int columnKey;
private String text;
2016-03-04 03:56:28 +00:00
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
public Address (String column, String row)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
set (column, row);
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
public Address (int column, int row)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
2016-03-04 05:27:14 +00:00
assert column <= MAX_COLUMNS;
assert row <= MAX_ROWS;
2017-03-16 00:27:45 +00:00
2016-03-04 03:56:28 +00:00
this.row = row;
this.column = column;
2016-03-15 20:06:04 +00:00
rowKey = row * MAX_COLUMNS + column;
columnKey = column * MAX_ROWS + row;
2016-03-04 03:56:28 +00:00
int col1 = column / 26;
int col2 = column % 26;
String col =
col1 > 0 ? (char) ('@' + col1) + ('A' + col2) + "" : (char) ('A' + col2) + "";
text = col + (row + 1);
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
public Address (String address)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
2017-03-14 11:28:52 +00:00
assert address.length () >= 2;
2016-03-04 03:56:28 +00:00
if (address.charAt (1) < 'A')
set (address.substring (0, 1), address.substring (1));
else
set (address.substring (0, 2), address.substring (2));
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-15 04:40:18 +00:00
public boolean matches (String addressText)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-15 04:40:18 +00:00
{
Address address = new Address (addressText);
return this.rowMatches (address) && this.columnMatches (address);
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
private void set (String sCol, String sRow)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
if (sCol.length () == 1)
column = sCol.charAt (0) - 'A';
else if (sCol.length () == 2)
column = (sCol.charAt (0) - '@') * 26 + sCol.charAt (1) - 'A';
else
System.out.println ("Bollocks");
try
{
row = Integer.parseInt (sRow) - 1;
2016-03-15 20:06:04 +00:00
rowKey = row * MAX_COLUMNS + column;
columnKey = column * MAX_ROWS + row;
2016-03-04 03:56:28 +00:00
text = sCol + sRow;
}
catch (NumberFormatException e)
{
2017-02-18 09:54:24 +00:00
System.out.printf ("sCol:%s,sRow:%s%n", sCol, sRow);
2016-03-04 03:56:28 +00:00
System.out.printf ("NFE: %s%n", sRow);
2017-02-18 09:54:24 +00:00
System.out.println (Arrays.toString (Thread.currentThread ().getStackTrace ()));
2016-03-04 03:56:28 +00:00
}
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
boolean rowMatches (Address other)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return row == other.row;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
boolean columnMatches (Address other)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return column == other.column;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
int getRow ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return row;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
int getColumn ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return column;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 05:27:14 +00:00
Address nextRow ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
Address next = new Address (column, row + 1);
return next;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 05:27:14 +00:00
Address nextColumn ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
Address next = new Address (column + 1, row);
return next;
}
2016-03-15 20:06:04 +00:00
// copied from Appleworks Cell
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-15 20:06:04 +00:00
static String getCellName (int row, int column)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-15 20:06:04 +00:00
{
char c1 = (char) ('A' + column / 26 - 1);
char c2 = (char) ('A' + column % 26);
return "" + (c1 == '@' ? "" : c1) + c2 + row;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-25 03:56:22 +00:00
public String getText ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-25 03:56:22 +00:00
{
return text;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
int getRowKey ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return rowKey;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
int getColumnKey ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-03-03 23:41:08 +00:00
{
return columnKey;
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-25 03:56:22 +00:00
public String getDetails ()
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2017-02-25 03:56:22 +00:00
{
return String.format ("Row:%3d Col:%3d rKey:%5d cKey:%5d", row, column, rowKey,
columnKey);
}
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
{
2017-02-25 03:56:22 +00:00
return String.format ("%-6s Row:%3d Col:%3d Key:%4d", text, row, column, rowKey);
2016-03-04 03:56:28 +00:00
}
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
@Override
public int compareTo (Address o)
2020-02-10 11:05:40 +00:00
// ---------------------------------------------------------------------------------//
2016-03-04 03:56:28 +00:00
{
2016-03-15 20:06:04 +00:00
return rowKey - o.rowKey;
2016-03-04 03:56:28 +00:00
}
}