Skip to content

Commit e32f923

Browse files
authored
chore: introduce ByteRangeSpec#endOffsetInclusive (#1832)
There are some cases where endOffset and endOffsetInclusive are both necessary and externally being able to differentiate is needed. ### Test Refactor As part of this change, I've update the declaration of ByteRangeSpec.RangeScenario to be more verbose and easier to read, as well as have a more direct description for the test name. This does add a fair number of lines of code, but I think it brings some clarity compared to a method with 4 different long arguments. It also reduces (and possible errors in declaration) between related scenarios. In general the new pattern is: define the four properties of any range (beginOffset, endOffset, endOffsetInclusive, length) then specify those ByteRangeSpecs for which it should be applicable.
1 parent 8720f05 commit e32f923

File tree

2 files changed

+310
-152
lines changed

2 files changed

+310
-152
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/ByteRangeSpec.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ private ByteRangeSpec() {}
5050

5151
abstract long endOffset() throws ArithmeticException;
5252

53+
abstract long endOffsetInclusive() throws ArithmeticException;
54+
5355
abstract long length() throws ArithmeticException;
5456

5557
// TODO: add validation to this if it ever becomes public
@@ -97,9 +99,7 @@ public boolean equals(Object o) {
9799

98100
@Override
99101
public String toString() {
100-
return append(MoreObjects.toStringHelper(""))
101-
.add("httpRangeHeader", getHttpRangeHeader())
102-
.toString();
102+
return append(MoreObjects.toStringHelper("ByteRangeSpec")).toString();
103103
}
104104

105105
protected abstract MoreObjects.ToStringHelper append(MoreObjects.ToStringHelper tsh);
@@ -162,6 +162,11 @@ long beginOffset() {
162162

163163
@Override
164164
long endOffset() throws ArithmeticException {
165+
return Math.addExact(beginOffset, length);
166+
}
167+
168+
@Override
169+
long endOffsetInclusive() throws ArithmeticException {
165170
return Math.addExact(beginOffset, length) - 1;
166171
}
167172

@@ -209,7 +214,7 @@ ByteRangeSpec withNewRelativeLength(long relativeLength) {
209214

210215
@Override
211216
protected String fmtAsHttpRangeHeader() throws ArithmeticException {
212-
return String.format("bytes=%d-%d", beginOffset, endOffset());
217+
return String.format("bytes=%d-%d", beginOffset, endOffsetInclusive());
213218
}
214219

215220
@Override
@@ -239,9 +244,14 @@ long endOffset() throws ArithmeticException {
239244
return endOffsetExclusive;
240245
}
241246

247+
@Override
248+
long endOffsetInclusive() throws ArithmeticException {
249+
return Math.subtractExact(endOffsetExclusive, 1);
250+
}
251+
242252
@Override
243253
long length() throws ArithmeticException {
244-
return endOffsetExclusive - beginOffset;
254+
return Math.subtractExact(endOffsetExclusive, beginOffset);
245255
}
246256

247257
@Override
@@ -284,7 +294,7 @@ ByteRangeSpec withNewRelativeLength(long relativeLength) {
284294

285295
@Override
286296
protected String fmtAsHttpRangeHeader() throws ArithmeticException {
287-
return String.format("bytes=%d-%d", beginOffset, endOffsetExclusive - 1);
297+
return String.format("bytes=%d-%d", beginOffset, endOffsetInclusive());
288298
}
289299

290300
@Override
@@ -311,12 +321,17 @@ long beginOffset() {
311321

312322
@Override
313323
long endOffset() throws ArithmeticException {
324+
return Math.addExact(endOffsetInclusive, 1L);
325+
}
326+
327+
@Override
328+
long endOffsetInclusive() throws ArithmeticException {
314329
return endOffsetInclusive;
315330
}
316331

317332
@Override
318333
long length() throws ArithmeticException {
319-
return endOffsetInclusive - beginOffset;
334+
return Math.addExact(Math.subtractExact(endOffsetInclusive, beginOffset), 1);
320335
}
321336

322337
@Override
@@ -346,7 +361,7 @@ ByteRangeSpec withNewEndOffset(long endOffsetExclusive) {
346361
@Override
347362
ByteRangeSpec withNewEndOffsetClosed(long endOffsetInclusive) {
348363
if (endOffsetInclusive != this.endOffsetInclusive) {
349-
return new LeftClosedRightOpenByteRangeSpec(beginOffset, endOffsetInclusive);
364+
return new LeftClosedRightClosedByteRangeSpec(beginOffset, endOffsetInclusive);
350365
} else {
351366
return this;
352367
}
@@ -387,6 +402,11 @@ long endOffset() throws ArithmeticException {
387402
return EFFECTIVE_INFINITY;
388403
}
389404

405+
@Override
406+
long endOffsetInclusive() throws ArithmeticException {
407+
return EFFECTIVE_INFINITY;
408+
}
409+
390410
@Override
391411
long length() throws ArithmeticException {
392412
return EFFECTIVE_INFINITY;
@@ -455,6 +475,11 @@ long endOffset() throws ArithmeticException {
455475
return EFFECTIVE_INFINITY;
456476
}
457477

478+
@Override
479+
long endOffsetInclusive() throws ArithmeticException {
480+
return EFFECTIVE_INFINITY;
481+
}
482+
458483
@Override
459484
long length() throws ArithmeticException {
460485
return EFFECTIVE_INFINITY;

0 commit comments

Comments
 (0)