Commit | Line | Data |
---|---|---|
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 | ||
10 | package org.eclipse.tracecompass.segmentstore.core; | |
11 | ||
e5083481 PT |
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 | ||
5a90de55 AM |
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 | ||
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 | ||
e5083481 PT |
64 | @Override |
65 | public int compareTo(@Nullable ISegment o) { | |
66 | if (o == null) { | |
67 | throw new IllegalArgumentException(); | |
68 | } | |
69 | return COMPARATOR.compare(this, o); | |
70 | } | |
71 | ||
5a90de55 AM |
72 | @Override |
73 | public String toString() { | |
74 | return new String('[' + String.valueOf(fStart) + ", " + String.valueOf(fEnd) + ']'); //$NON-NLS-1$ | |
75 | } | |
5a90de55 | 76 | } |