Add byte offset variable to fake data segments.

The value is automatically set at creation and allows simpler access to the
faked data position within the source.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162218095
This commit is contained in:
tonihei 2017-07-17 08:44:57 -07:00 committed by Oliver Woodman
parent 91341df7b0
commit 81048ee7af

View File

@ -80,33 +80,37 @@ public class FakeDataSet {
public final IOException exception; public final IOException exception;
public final byte[] data; public final byte[] data;
public final int length; public final int length;
public final long byteOffset;
public final Runnable action; public final Runnable action;
public boolean exceptionThrown; public boolean exceptionThrown;
public boolean exceptionCleared; public boolean exceptionCleared;
public int bytesRead; public int bytesRead;
private Segment(byte[] data) { private Segment(byte[] data, Segment previousSegment) {
this(data, data.length, null, null); this(data, data.length, null, null, previousSegment);
} }
private Segment(int length) { private Segment(int length, Segment previousSegment) {
this(null, length, null, null); this(null, length, null, null, previousSegment);
} }
private Segment(IOException exception) { private Segment(IOException exception, Segment previousSegment) {
this(null, 0, exception, null); this(null, 0, exception, null, previousSegment);
} }
private Segment(Runnable action) { private Segment(Runnable action, Segment previousSegment) {
this(null, 0, null, action); this(null, 0, null, action, previousSegment);
} }
private Segment(byte[] data, int length, IOException exception, Runnable action) { private Segment(byte[] data, int length, IOException exception, Runnable action,
Segment previousSegment) {
this.exception = exception; this.exception = exception;
this.action = action; this.action = action;
this.data = data; this.data = data;
this.length = length; this.length = length;
this.byteOffset = previousSegment == null ? 0
: previousSegment.byteOffset + previousSegment.length;
} }
public boolean isErrorSegment() { public boolean isErrorSegment() {
@ -152,7 +156,7 @@ public class FakeDataSet {
*/ */
public FakeData appendReadData(byte[] data) { public FakeData appendReadData(byte[] data) {
Assertions.checkState(data != null && data.length > 0); Assertions.checkState(data != null && data.length > 0);
segments.add(new Segment(data)); segments.add(new Segment(data, getLastSegment()));
return this; return this;
} }
@ -162,7 +166,7 @@ public class FakeDataSet {
*/ */
public FakeData appendReadData(int length) { public FakeData appendReadData(int length) {
Assertions.checkState(length > 0); Assertions.checkState(length > 0);
segments.add(new Segment(length)); segments.add(new Segment(length, getLastSegment()));
return this; return this;
} }
@ -170,7 +174,7 @@ public class FakeDataSet {
* Appends an error in the underlying data. * Appends an error in the underlying data.
*/ */
public FakeData appendReadError(IOException exception) { public FakeData appendReadError(IOException exception) {
segments.add(new Segment(exception)); segments.add(new Segment(exception, getLastSegment()));
return this; return this;
} }
@ -178,7 +182,7 @@ public class FakeDataSet {
* Appends an action. * Appends an action.
*/ */
public FakeData appendReadAction(Runnable action) { public FakeData appendReadAction(Runnable action) {
segments.add(new Segment(action)); segments.add(new Segment(action, getLastSegment()));
return this; return this;
} }
@ -206,6 +210,12 @@ public class FakeDataSet {
public boolean isSimulatingUnknownLength() { public boolean isSimulatingUnknownLength() {
return simulateUnknownLength; return simulateUnknownLength;
} }
private Segment getLastSegment() {
int count = segments.size();
return count > 0 ? segments.get(count - 1) : null;
}
} }
private final HashMap<String, FakeData> dataMap; private final HashMap<String, FakeData> dataMap;