
net.vectorpublish.desktop.vp.api.Moment Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.api;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* A Moment on a timeline as ReadOnly-Singleton.
*/
public final class Moment implements Serializable {
private static final long serialVersionUID = -2411777818941452741L;
private static final Set GLOBAL_MOMENTS = new LinkedHashSet<>();
public static final Moment NULL_MOMENT = constructMoment(0);
private static final int MINUTE = 1000 * 60;
private static final int HOUR = MINUTE * 60;
private static final int DAY = HOUR * 24;
private static final int YEAR = DAY * 356;
private static synchronized Moment constructMoment(int time) {
for (final Moment moment : GLOBAL_MOMENTS) {
if (moment.ms == time) {
return moment;
}
}
Moment moment = new Moment(time);
GLOBAL_MOMENTS.add(moment);
return moment;
}
public static Moment newMoment(int time) {
for (final Moment moment : GLOBAL_MOMENTS) {
if (moment.ms == time) {
return moment;
}
}
return constructMoment(time);
}
/**
* Parses a Moment from a Time.
*
* @param time
* The time human-readable.
* @return The moment parsed or null
if the time could not be
* parsed.
*/
public static Moment parse(String time) {
try {
return newMoment((int) new PlainDateFormat("ss:SSS").parse(time).getTime());
} catch (final ParseException e) {
try {
return newMoment((int) new PlainDateFormat("mm:ss:SSS").parse(time).getTime());
} catch (final ParseException e1) {
try {
return newMoment((int) new PlainDateFormat("HH:mm:ss:SSS").parse(time).getTime());
} catch (final ParseException e2) {
try {
return newMoment((int) new PlainDateFormat("DD HH:mm:ss:SSS").parse(time).getTime());
} catch (final ParseException e3) {
return null;
}
}
}
}
}
private final int ms;
private String formatted;
/**
* Initializes a new Moment using a time.
*
* @param ms
* The absolute Miliseconds.
*/
private Moment(int ms) {
this.ms = ms;
}
/**
* Checks if a moment is before this moment.
*
* @param later
* The may later moment.
* @return true
if the moment is later, false
if
* not.
*/
public boolean before(Moment later) {
return later.ms > ms;
}
/**
* Formats the time to present it to a user.
*
* @return The user-readable time.
*/
public String format() {
if (formatted == null) {
synchronized (this) {
if (formatted == null) {
final SimpleDateFormat sdf;
if (ms < MINUTE) {
sdf = new SimpleDateFormat("ss:SSS");
} else if (ms < HOUR) {
sdf = new SimpleDateFormat("mm:ss:SSS");
} else if (ms < DAY) {
sdf = new SimpleDateFormat("HH:mm:ss:SSS");
} else if (ms < YEAR) {
sdf = new SimpleDateFormat("DD HH:mm:ss:SSS");
} else {
sdf = new SimpleDateFormat("DD HH:mm:ss:SSS");
}
formatted = sdf.format(new Date(ms));
}
}
}
return formatted;
}
/**
* Returns the miliseconds.
*
* @return The miliseconds.
*/
public int getMS() {
return ms;
}
/**
* @see Object#hashCode()
* @deprecated Please use == instead of compare hashcodes.
*/
@Override
public int hashCode() {
return ms;
}
/**
* @see Object#equals(Object)
* @deprecated Please use == instead of equals.
*/
@Override
@Deprecated
public boolean equals(Object obj) {
return super.equals(obj);
}
/**
* A part of {@link Serializable serialization}. The result is directly
* returned to {@link ObjectInputStream#readObject()} used to read a file.
*
* @return A instance of the {@link Moment}, never null
.
* @throws ObjectStreamException
* A bug on the deserialization, should not be happen.
*/
private Object readResolve() throws ObjectStreamException {
return newMoment(ms);
}
@Override
public String toString() {
return format();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy