package com.nickww.finitefield;

import static com.nickww.finitefield.FiniteByteField.*;

final class XorChecksumVector extends ChecksumVector
{
	@Override
	public byte[] withChecksums(byte[] data)
	{
		if(data.length < 1)
			throw new IllegalArgumentException("Data array must have at least one element");
		
		byte[] checksummed = new byte[data.length + 1];
		byte checksum = 0;
		for(int i = 0; i < data.length; i++)
		{
			checksum = add(checksum, data[i]);
			checksummed[i] = data[i];
		}
		checksummed[data.length] = checksum;
		return checksummed;
	}
	
	@Override
	public byte[] solveMissingValues(Byte[] dataWithChecksums)
	{
		if(dataWithChecksums.length < 2)
			throw new IllegalArgumentException("Array too small to include both data and checksums.");
		
		byte[] data = new byte[dataWithChecksums.length - 1];
		byte checksum = 0;
		int indexOfNull = -1;
		for(int i = 0; i < dataWithChecksums.length; i++)
		{
			if(dataWithChecksums[i] == null)
			{
				if(indexOfNull != -1)
					throw new IllegalArgumentException("Too many nulls - can only handle 1");
				indexOfNull = i;
			}
			else
			{
				checksum = add(checksum, dataWithChecksums[i]);
				if(i < data.length)
					data[i] = dataWithChecksums[i];
			}
		}
		
		if(indexOfNull >= data.length)
			return data;
		data[indexOfNull] = checksum;
		return data;
	}
}

@@ -372,113 +372,65 @@
 %0D%0A%09%09
-byte checksum = 0;%0D%0A%09%09for(int i = 0; i %3C data.length; i++)%0D%0A%09%09%7B%0D%0A%09%09%09checksum = add(checksum, data%5Bi%5D
+System.arraycopy(data, 0, checksummed, 0, data.length
 );%0D%0A
-%09
 %09%09ch
@@ -443,30 +443,8 @@
 med%5B
-i%5D = data%5Bi%5D;%0D%0A%09%09%7D%0D%0A%09%09
 chec
@@ -454,32 +454,30 @@
 mmed
-%5Bdata
 .length
+-1
 %5D = 
-checksum
+add(data)
 ;%0D%0A%09

Refactor #withChecksums logic to be simpler