1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.archivepatcher.shared;
16
17/**
18 * A range, annotated with metadata, that is represented as an offset and a length. Comparison is
19 * performed based on the natural ordering of the offset field.
20 * @param <T> the type of the metadata
21 */
22public class TypedRange<T> implements Comparable<TypedRange<T>> {
23  /**
24   * The offset at which the range starts.
25   */
26  private final long offset;
27
28  /**
29   * The length of the range.
30   */
31  private final long length;
32
33  /**
34   * Optional metadata associated with this range.
35   */
36  private final T metadata;
37
38  /**
39   * Constructs a new range with the specified parameters.
40   * @param offset the offset at which the range starts
41   * @param length the length of the range
42   * @param metadata optional metadata associated with this range
43   */
44  public TypedRange(long offset, long length, T metadata) {
45    this.offset = offset;
46    this.length = length;
47    this.metadata = metadata;
48  }
49
50  @Override
51  public String toString() {
52    return "offset " + offset + ", length " + length + ", metadata " + metadata;
53  }
54
55  /**
56   * Returns the offset at which the range starts.
57   * @return as described
58   */
59  public long getOffset() {
60    return offset;
61  }
62
63  /**
64   * Returns the length of the range.
65   * @return as described
66   */
67  public long getLength() {
68    return length;
69  }
70
71  /**
72   * Returns the metadata associated with the range, or null if no metadata has been set.
73   * @return as described
74   */
75  public T getMetadata() {
76    return metadata;
77  }
78
79  @Override
80  public int hashCode() {
81    final int prime = 31;
82    int result = 1;
83    result = prime * result + (int) (length ^ (length >>> 32));
84    result = prime * result + ((metadata == null) ? 0 : metadata.hashCode());
85    result = prime * result + (int) (offset ^ (offset >>> 32));
86    return result;
87  }
88
89  @Override
90  public boolean equals(Object obj) {
91    if (this == obj) return true;
92    if (obj == null) return false;
93    if (getClass() != obj.getClass()) return false;
94    TypedRange<?> other = (TypedRange<?>) obj;
95    if (length != other.length) return false;
96    if (metadata == null) {
97      if (other.metadata != null) return false;
98    } else if (!metadata.equals(other.metadata)) return false;
99    if (offset != other.offset) return false;
100    return true;
101  }
102
103  @Override
104  public int compareTo(TypedRange<T> other) {
105    if (getOffset() < other.getOffset()) {
106      return -1;
107    } else if (getOffset() > other.getOffset()) {
108      return 1;
109    }
110    return 0;
111  }
112}
113