refactoring

This commit is contained in:
Denis Molony 2016-03-07 10:52:46 +11:00
parent 319fe41e74
commit bc27149106
3 changed files with 18 additions and 13 deletions

View File

@ -18,7 +18,7 @@ public class Count
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell.hasValue () && cell.getValue () != 0.0)
if (cell != null && cell.hasValue () && cell.getValue () != 0.0)
result += 1;
}

View File

@ -18,7 +18,6 @@ public class Sheet implements Iterable<Cell>
private final Map<Integer, Cell> sheet = new TreeMap<Integer, Cell> ();
private final Map<String, Double> functions = new HashMap<String, Double> ();
final List<String> lines = new ArrayList<String> ();
Cell currentCell = null;
char defaultFormat;
@ -138,11 +137,14 @@ public class Sheet implements Iterable<Cell>
;
int ptr = 0;
while (ptr <= last)
final List<String> lines = new ArrayList<String> ();
while (ptr < last)
{
int endPtr = findEndPtr (buffer, ptr);
add (HexFormatter.getString (buffer, ptr, endPtr - ptr));
ptr = endPtr + 1;
int length = getLineLength (buffer, ptr);
String line = HexFormatter.getString (buffer, ptr, length);
lines.add (line);
processLine (line);
ptr += length + 1; // +1 for end-of-line token
}
if (true)
@ -165,14 +167,15 @@ public class Sheet implements Iterable<Cell>
}
}
private int findEndPtr (byte[] buffer, int ptr)
private int getLineLength (byte[] buffer, int offset)
{
while (buffer[ptr] != (byte) 0x8D)
int ptr = offset;
while (buffer[ptr] != (byte) 0x8D) // end-of-line token
ptr++;
return ptr;
return ptr - offset;
}
private void add (String command)
private void processLine (String command)
{
// NB no closing bracket: [>K11:@SUM(J11...F11]
@ -182,8 +185,6 @@ public class Sheet implements Iterable<Cell>
return;
}
lines.add (command);
if (command.startsWith (">")) // GOTO cell
{
Matcher m = addressPattern.matcher (command);

View File

@ -16,7 +16,11 @@ public class Sum
double result = 0;
for (Address address : range)
result += parent.getCell (address).getValue ();
{
Cell cell = parent.getCell (address);
if (cell != null && cell.hasValue ())
result += cell.getValue ();
}
return result;
}