6fe493eddcadb409d1ea7d64b785ec486ad07eca
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.datastore.core / src / org / eclipse / tracecompass / internal / provisional / datastore / core / interval / HTInterval.java
1 /*******************************************************************************
2 * Copyright (c) 2017 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.provisional.datastore.core.interval;
11
12 import java.util.Objects;
13 import java.util.StringJoiner;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
18
19 /**
20 * Basic implementation of {@link IHTInterval}.
21 *
22 * @author Geneviève Bastien
23 */
24 public class HTInterval implements IHTInterval {
25
26 private final long fStart;
27 private final long fEnd;
28
29 /**
30 * The object to use to read a BaseHtObject from the disk
31 */
32 public static final IHTIntervalReader<HTInterval> INTERVAL_READER =
33 (buffer) -> new HTInterval(buffer.getLong(), buffer.getLong());
34
35 /**
36 * Create a new segment.
37 *
38 * The end position should be equal to or greater than the start position.
39 *
40 * @param start
41 * Start position of the segment
42 * @param end
43 * End position of the segment
44 */
45 public HTInterval(long start, long end) {
46 if (end < start) {
47 throw new IllegalArgumentException();
48 }
49 fStart = start;
50 fEnd = end;
51 }
52
53 @Override
54 public long getStart() {
55 return fStart;
56 }
57
58 @Override
59 public long getEnd() {
60 return fEnd;
61 }
62
63 @Override
64 public String toString() {
65 return (new StringJoiner(", ", "[", "]")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
66 .add(String.valueOf(fStart))
67 .add(String.valueOf(fEnd))
68 .toString();
69 }
70
71 @Override
72 public int getSizeOnDisk() {
73 return 2 * Long.BYTES;
74 }
75
76 @Override
77 public void writeSegment(@NonNull ISafeByteBufferWriter buffer) {
78 buffer.putLong(fStart);
79 buffer.putLong(fEnd);
80 }
81
82 @Override
83 public int hashCode() {
84 return Objects.hash(fStart, fEnd);
85 }
86
87 @Override
88 public boolean equals(@Nullable Object obj) {
89 if (obj == null) {
90 return false;
91 }
92 if (obj.getClass() != getClass()) {
93 return false;
94 }
95 HTInterval other = (HTInterval) obj;
96 return (fStart == other.fStart
97 && fEnd == other.fEnd);
98 }
99
100 }
This page took 0.032527 seconds and 4 git commands to generate.