61b12b9997aaa4470c149798f49b356a706554d3
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.segmentstore.core / src / org / eclipse / tracecompass / segmentstore / core / BasicSegment.java
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
10 package org.eclipse.tracecompass.segmentstore.core;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.Comparator;
15
16 import org.eclipse.jdt.annotation.Nullable;
17
18 import com.google.common.collect.Ordering;
19
20 /**
21 * Basic implementation of {@link ISegment}.
22 *
23 * @author Alexandre Montplaisir
24 */
25 public class BasicSegment implements ISegment {
26
27 private static final long serialVersionUID = -3257452887960883177L;
28
29 private static final Comparator<ISegment> COMPARATOR;
30 static {
31 /* checkNotNull() has to be called separately, or else it breaks the
32 * type inference. */
33 Comparator<ISegment> comp = Ordering
34 .from(SegmentComparators.INTERVAL_START_COMPARATOR)
35 .compound(SegmentComparators.INTERVAL_END_COMPARATOR);
36 COMPARATOR = checkNotNull(comp);
37 }
38
39 private final long fStart;
40 private final long fEnd;
41
42 /**
43 * Create a new segment.
44 *
45 * The end position should be equal to or greater than the start position.
46 *
47 * @param start
48 * Start position of the segment
49 * @param end
50 * End position of the segment
51 */
52 public BasicSegment(long start, long end) {
53 if (end < start) {
54 throw new IllegalArgumentException();
55 }
56 fStart = start;
57 fEnd = end;
58 }
59
60 @Override
61 public long getStart() {
62 return fStart;
63 }
64
65 @Override
66 public long getEnd() {
67 return fEnd;
68 }
69
70 @Override
71 public int compareTo(@Nullable ISegment o) {
72 if (o == null) {
73 throw new IllegalArgumentException();
74 }
75 return COMPARATOR.compare(this, o);
76 }
77
78 @Override
79 public String toString() {
80 return new String('[' + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + ']'); //$NON-NLS-1$
81 }
82 }
This page took 0.054793 seconds and 4 git commands to generate.