ss: Bug 475300: Fix inconsistent TreeMapStore due to value comparators
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.segmentstore.core / src / org / eclipse / tracecompass / segmentstore / core / BasicSegment.java
CommitLineData
5a90de55
AM
1/*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
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
10package org.eclipse.tracecompass.segmentstore.core;
11
e5083481
PT
12import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14import java.util.Comparator;
15
16import org.eclipse.jdt.annotation.Nullable;
17
18import com.google.common.collect.Ordering;
19
5a90de55
AM
20/**
21 * Basic implementation of {@link ISegment}.
22 *
23 * @author Alexandre Montplaisir
24 */
25public class BasicSegment implements ISegment {
26
27 private static final long serialVersionUID = -3257452887960883177L;
28
e5083481
PT
29 private static final Comparator<ISegment> COMPARATOR = checkNotNull(Ordering
30 .from(SegmentComparators.INTERVAL_START_COMPARATOR)
31 .compound(SegmentComparators.INTERVAL_END_COMPARATOR));
32
5a90de55
AM
33 private final long fStart;
34 private final long fEnd;
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 BasicSegment(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 long getLength() {
66 return (fEnd - fStart);
67 }
68
e5083481
PT
69 @Override
70 public int compareTo(@Nullable ISegment o) {
71 if (o == null) {
72 throw new IllegalArgumentException();
73 }
74 return COMPARATOR.compare(this, o);
75 }
76
5a90de55
AM
77 @Override
78 public String toString() {
79 return new String('[' + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + ']'); //$NON-NLS-1$
80 }
5a90de55 81}
This page took 0.033363 seconds and 5 git commands to generate.