datastore: Make the serialization package public
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.datastore.core / src / org / eclipse / tracecompass / 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.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.datastore.core.serialization.ISafeByteBufferWriter;
18
19 /**
20 * Basic implementation of {@link IHTInterval}.
21 *
22 * @author Geneviève Bastien
23 * @since 1.1
24 */
25 public class HTInterval implements IHTInterval {
26
27 private final long fStart;
28 private final long fEnd;
29
30 /**
31 * The object to use to read a BaseHtObject from the disk
32 */
33 public static final IHTIntervalReader<HTInterval> INTERVAL_READER =
34 (buffer) -> new HTInterval(buffer.getLong(), buffer.getLong());
35
36 /**
37 * Create a new segment.
38 *
39 * The end position should be equal to or greater than the start position.
40 *
41 * @param start
42 * Start position of the segment
43 * @param end
44 * End position of the segment
45 */
46 public HTInterval(long start, long end) {
47 if (end < start) {
48 throw new IllegalArgumentException();
49 }
50 fStart = start;
51 fEnd = end;
52 }
53
54 @Override
55 public long getStart() {
56 return fStart;
57 }
58
59 @Override
60 public long getEnd() {
61 return fEnd;
62 }
63
64 @Override
65 public String toString() {
66 return (new StringJoiner(", ", "[", "]")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
67 .add(String.valueOf(fStart))
68 .add(String.valueOf(fEnd))
69 .toString();
70 }
71
72 @Override
73 public int getSizeOnDisk() {
74 return 2 * Long.BYTES;
75 }
76
77 @Override
78 public void writeSegment(@NonNull ISafeByteBufferWriter buffer) {
79 buffer.putLong(fStart);
80 buffer.putLong(fEnd);
81 }
82
83 @Override
84 public int hashCode() {
85 return Objects.hash(fStart, fEnd);
86 }
87
88 @Override
89 public boolean equals(@Nullable Object obj) {
90 if (obj == null) {
91 return false;
92 }
93 if (obj.getClass() != getClass()) {
94 return false;
95 }
96 HTInterval other = (HTInterval) obj;
97 return (fStart == other.fStart
98 && fEnd == other.fEnd);
99 }
100
101 }
This page took 0.032135 seconds and 5 git commands to generate.