summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/filetransfer/FileRange.java
blob: e8a7d1214ffc880905b024b6e48a14d9a7c1087c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.openslx.filetransfer;

public class FileRange
{

	/**
	 * Offset of first byte of range in file, inclusive
	 */
	public final long startOffset;
	/**
	 * Offset of last byte of range in file, exclusive
	 */
	public final long endOffset;

	/**
	 * Create a FileRange instance
	 * 
	 * @param startOffset Offset of first byte of range in file, inclusive
	 * @param endOffset Offset of last byte of range in file, exclusive
	 */
	public FileRange( long startOffset, long endOffset )
	{
		this.startOffset = startOffset;
		this.endOffset = endOffset;
	}

	/**
	 * Get length of range
	 * 
	 * @return length of range, in bytes
	 */
	public int getLength()
	{
		return (int) ( endOffset - startOffset );
	}

	/**
	 * Check if the given range lies within this chunk's range.
	 * 
	 * @param startOffset Start offset to compare to
	 * @param endOffset End offset to compare to
	 * @return true iff the given offsets are a subset or equal to the offsets represented by this
	 *         class
	 */
	public boolean contains( long startOffset, long endOffset )
	{
		return this.startOffset <= startOffset && this.endOffset >= endOffset;
	}

	@Override
	public boolean equals( Object other )
	{
		if ( other == this )
			return true;
		if ( other == null || ! ( other instanceof FileRange ) )
			return false;
		FileRange o = (FileRange)other;
		return o.startOffset == this.startOffset && o.endOffset == this.endOffset;
	}

	@Override
	public int hashCode()
	{
		return (int)startOffset ^ Integer.rotateLeft( (int)endOffset, 16 ) ^ (int)(startOffset >> 32);
	}

}