dcdd89e44c6ed20ed73b32fd2f7d7faef4900ba1
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.datastore.core / src / org / eclipse / tracecompass / internal / datastore / core / condition / ContinuousRangeCondition.java
1 /*******************************************************************************
2 * Copyright (c) 2017 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 ******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.datastore.core.condition;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.tracecompass.internal.provisional.datastore.core.condition.RangeCondition;
14
15 /**
16 * Generic Range Condition. This condition will verify that a value are within a
17 * range limited by a lower and upper bound.
18 *
19 * @author Loïc Prieur-Drevon
20 * @param <E>
21 * Comparable type, for instance Long for timestamps
22 */
23 public class ContinuousRangeCondition<E extends Comparable<E>> implements RangeCondition<E> {
24
25 private final E fMin;
26 private final E fMax;
27
28 /**
29 * Build a continuous range condition from two bounds.
30 *
31 * @param low
32 * The range's lower bound
33 * @param high
34 * The range's higher bound
35 */
36 public ContinuousRangeCondition(@NonNull E low, @NonNull E high) {
37 if (low.compareTo(high) > 0) {
38 throw new IllegalArgumentException(low.toString() + " is greater than " + high.toString()); //$NON-NLS-1$
39 }
40 fMin = low;
41 fMax = high;
42 }
43
44 @Override
45 public E min() {
46 return fMin;
47 }
48
49 @Override
50 public E max() {
51 return fMax;
52 }
53
54 @Override
55 public boolean test(E element) {
56 return fMin.compareTo(element) <= 0 && element.compareTo(fMax) <= 0;
57 }
58
59 @Override
60 public boolean intersects(E low, E high) {
61 return fMin.compareTo(high) <= 0 && fMax.compareTo(low) >= 0;
62 }
63
64 private static <E extends Comparable<E>> E min(E n1, E n2) {
65 if (n1.compareTo(n2) <= 0) {
66 return n1;
67 }
68 return n2;
69 }
70
71 private static <E extends Comparable<E>> E max(E n1, E n2) {
72 if (n1.compareTo(n2) >= 0) {
73 return n1;
74 }
75 return n2;
76 }
77
78 @Override
79 public @NonNull RangeCondition<E> subCondition(E from, E to) {
80 return new ContinuousRangeCondition<>(max(from, fMin), min(fMax, to));
81 }
82
83 @Override
84 public String toString() {
85 return "Continuous condition: " + fMin.toString() + '-' + fMax.toString(); //$NON-NLS-1$
86 }
87
88 }
This page took 0.033631 seconds and 4 git commands to generate.