| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 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 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | * Patrick Tasse - Modified from TmfSimpleTimestamp to use nanosecond scale |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.tmf.core.timestamp; |
| 15 | |
| 16 | /** |
| 17 | * A simplified timestamp where scale is nanoseconds and precision is set to 0. |
| 18 | */ |
| 19 | public final class TmfNanoTimestamp extends TmfTimestamp { |
| 20 | |
| 21 | private final long fValue; |
| 22 | |
| 23 | // ------------------------------------------------------------------------ |
| 24 | // Constructors |
| 25 | // ------------------------------------------------------------------------ |
| 26 | |
| 27 | /** |
| 28 | * Default constructor (value = 0) |
| 29 | */ |
| 30 | public TmfNanoTimestamp() { |
| 31 | this(0); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Full constructor |
| 36 | * |
| 37 | * @param value |
| 38 | * the timestamp value |
| 39 | */ |
| 40 | public TmfNanoTimestamp(final long value) { |
| 41 | fValue = value; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public long getValue() { |
| 46 | return fValue; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | @Override |
| 51 | public int getScale() { |
| 52 | return ITmfTimestamp.NANOSECOND_SCALE; |
| 53 | } |
| 54 | |
| 55 | // ------------------------------------------------------------------------ |
| 56 | // ITmfTimestamp |
| 57 | // ------------------------------------------------------------------------ |
| 58 | |
| 59 | @Override |
| 60 | public ITmfTimestamp normalize(final long offset, final int scale) { |
| 61 | if (scale == ITmfTimestamp.NANOSECOND_SCALE) { |
| 62 | return new TmfNanoTimestamp(saturatedAdd(getValue(), offset)); |
| 63 | } |
| 64 | return super.normalize(offset, scale); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int compareTo(final ITmfTimestamp ts) { |
| 69 | if (ts instanceof TmfNanoTimestamp) { |
| 70 | final long delta = getValue() - ts.getValue(); |
| 71 | return (delta == 0) ? 0 : (delta > 0) ? 1 : -1; |
| 72 | } |
| 73 | return super.compareTo(ts); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public ITmfTimestamp getDelta(final ITmfTimestamp ts) { |
| 78 | if (ts instanceof TmfNanoTimestamp) { |
| 79 | return new TmfTimestampDelta(getValue() - ts.getValue(), ITmfTimestamp.NANOSECOND_SCALE); |
| 80 | } |
| 81 | return super.getDelta(ts); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 2.0 |
| 86 | */ |
| 87 | @Override |
| 88 | public long toNanos() { |
| 89 | return getValue(); |
| 90 | } |
| 91 | |
| 92 | // ------------------------------------------------------------------------ |
| 93 | // Object |
| 94 | // ------------------------------------------------------------------------ |
| 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
| 98 | return super.hashCode(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(final Object other) { |
| 103 | if (this == other) { |
| 104 | return true; |
| 105 | } |
| 106 | if (other == null) { |
| 107 | return false; |
| 108 | } |
| 109 | if (!(other instanceof TmfNanoTimestamp)) { |
| 110 | return super.equals(other); |
| 111 | } |
| 112 | final TmfNanoTimestamp ts = (TmfNanoTimestamp) other; |
| 113 | |
| 114 | return compareTo(ts) == 0; |
| 115 | } |
| 116 | |
| 117 | } |