SCViewer/com.minres.scviewer.database/src/com/minres/scviewer/database/EventTime.java

80 lines
2.0 KiB
Java
Raw Normal View History

2012-06-17 20:34:50 +02:00
/*******************************************************************************
* Copyright (c) 2012 IT Just working.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IT Just working - initial API and implementation
*******************************************************************************/
package com.minres.scviewer.database;
2012-06-17 19:53:05 +02:00
public class EventTime implements Comparable<EventTime>{
2015-01-09 14:17:29 +01:00
public enum Unit {
FS("fs"), PS("ps"), NS("ns"), US("us"), MS("ms"), SEC("s");
private String alternative;
private Unit(String alternative){
this.alternative=alternative;
}
public static Unit fromString(String text) {
if (text != null)
for (Unit b : Unit.values()) {
if (text.equalsIgnoreCase(b.name()) || text.equalsIgnoreCase(b.alternative)) return b;
}
return null;
}
}
2015-01-01 23:17:32 +01:00
static final double[] scales = {
1,
1000.0,
1000000.0,
1000000000.0,
1000000000000.0,
1000000000000000.0};
2015-01-01 23:17:32 +01:00
2015-01-09 14:17:29 +01:00
public static final EventTime ZERO = new EventTime(0L);
2015-01-06 17:14:16 +01:00
2012-06-17 19:53:05 +02:00
private long value; // unit is femto seconds
2015-01-09 14:17:29 +01:00
public EventTime(Long value){
this(value, Unit.FS);
}
public EventTime(Long value, Unit scale){
setValue(value, scale);
}
2015-01-06 17:14:16 +01:00
2015-01-09 14:17:29 +01:00
public static double getScalingFactor(Unit scale){
return scales[scale.ordinal()];
2012-06-17 19:53:05 +02:00
}
public long getValue(){
return(value);
}
2015-01-09 14:17:29 +01:00
public double getScaledValue(Unit scale){
return value/scales[scale.ordinal()];
2012-06-17 19:53:05 +02:00
}
public void setValue(long value){
this.value=value;
}
2015-01-09 14:17:29 +01:00
public void setValue(long value, Unit scale){
this.value=(long) (value*scales[scale.ordinal()]);
2012-06-17 19:53:05 +02:00
}
public String toString(){
2015-01-09 14:17:29 +01:00
return value/scales[Unit.NS.ordinal()] +"ns";
2012-06-17 19:53:05 +02:00
}
@Override
public int compareTo(EventTime other) {
return this.value<other.value? -1 : this.value==other.value? 0 : 1;
}
}