Created
August 23, 2019 14:11
-
-
Save kool79/0dff6c471620948670022f81bc20efcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package webdav.support; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import com.google.common.base.Preconditions; | |
import org.apache.commons.lang3.tuple.Pair; | |
public class RangeSpecification { | |
private List<Pair<Long, Long>> ranges = new ArrayList<>(); | |
// default dimension | |
private String dimension = "bytes"; | |
public RangeSpecification() {} | |
public RangeSpecification(long position) { | |
if (position < 0) { | |
ranges.add(Pair.of(null, -position)); | |
} else { | |
ranges.add(Pair.of(position, null)); | |
} | |
} | |
public RangeSpecification(long start, long end) { | |
Preconditions.checkArgument(start >= 0, "start must be >=0"); | |
Preconditions.checkArgument(end >= 0, "end must be >=0"); | |
ranges.add(Pair.of(start, end)); | |
} | |
public RangeSpecification addRange(long start, long end) { | |
Preconditions.checkArgument(start >= 0, "start must be >=0"); | |
Preconditions.checkArgument(end >= 0, "end must be >=0"); | |
ranges.add(Pair.of(start, end)); | |
return this; | |
} | |
/** same as {@code addRange(0, toPosition)} */ | |
public RangeSpecification to(long toPosition) { | |
Preconditions.checkArgument(toPosition >= 0, "toPosition must be >=0"); | |
ranges.add(Pair.of(0L, toPosition)); | |
return this; | |
} | |
public RangeSpecification after(long startPosition) { | |
Preconditions.checkArgument(startPosition >= 0, "startPosition must be >=0"); | |
ranges.add(Pair.of(startPosition, null)); | |
return this; | |
} | |
public RangeSpecification last(long lastCount) { | |
Preconditions.checkArgument(lastCount >= 0, "lastCount must be >=0"); | |
ranges.add(Pair.of(null, lastCount)); | |
return this; | |
} | |
/** Example: | |
* <pre>{@code | |
* if (rangeSpecification.hasAnyRange() | |
* headers.add("Range, rangeSpecification.toString()); | |
* } | |
* </pre> | |
* | |
* @return | |
*/ | |
public boolean hasAnyRange() { | |
return !ranges.isEmpty(); | |
} | |
public RangeSpecification setDimension(String dimension) { | |
this.dimension = dimension; | |
return this; | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(dimension).append("="); | |
if (ranges.isEmpty()) { | |
sb.append("0-"); // if no range defined then use 'full range' from 0 to last byte. | |
} | |
else { | |
Iterator<Pair<Long, Long>> iterator = ranges.iterator(); | |
while (iterator.hasNext()) { | |
Pair<Long, Long> range = iterator.next(); | |
if (range.getLeft() != null) sb.append(range.getLeft()); | |
sb.append("-"); | |
if (range.getRight() != null) sb.append(range.getRight()); | |
if (iterator.hasNext()) sb.append(", "); | |
} | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment