Java tutorial
/* * Copyright (C) 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.google.soxlib; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.ISODateTimeFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Utility constants and methods that are useful to the SOX library or code that * uses the SOX library. * * @author css@google.com (Charles Spirakis) * */ public class Utility { /** * The time (in milliseconds) to sleep when inside a spin forever loop. Why 60 * seconds? Why not? Could pick any arbitrarily large number. */ public static final int SPIN_LOOP_SLEEP_MS = 60000; // A simple pattern match to catch a subset of invalid timestamp formats. public static final Pattern SIMPLE_TIMESTAMP_CHECK = Pattern .compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}"); /** * Get the time now in a string format suitable for use in SOX timestamp * fields. * * @return UTC timestamp to the nearest millisecond as a string. */ public static final String getTimestampNow() { DateTime now = new DateTime(DateTimeZone.UTC); return now.toString(ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC)); } /** * Convert from "unix time" (utc milliseconds since 1970) into a string format * for use in SOX timestamp fields. * * @param timestampMsUtc utc time in milliseconds since Jan 1, 1970 (aka unix * time). * @return UTC timestamp to the nearest millisecond as a string. */ public static final String getTimestampFromUnixTime(long timestampMsUtc) { DateTime dt = new DateTime(timestampMsUtc, DateTimeZone.UTC); return dt.toString(ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC)); } /** * Convert from a SOX string timestamp into a "unix timestamp" (milliseconds * since 1970). * * @param timestamp as a SOX string (i.e. from a timestamp field). * @return milliseconds since Jan 1, 1970 UTC. */ public static final long getUnixTimeFromTimestamp(String timestamp) { DateTime dt = new DateTime(timestamp); return dt.getMillis(); } /** * Verify that the string provided is a valid RFC 3339 timestamp string and * converts the string into the UTC timezone for use by the SOX library. * * @param timestamp string to be verified. * @return timestamp converted to UTC in the SOX format. * @throws IllegalArgumentException if the string cannot be converted to UTC. */ public static final String verifyTimestamp(String timestamp) throws IllegalArgumentException { Matcher m = Utility.SIMPLE_TIMESTAMP_CHECK.matcher(timestamp); if (!m.find()) { throw new IllegalArgumentException("timestamp not valid"); } // convert entered value to the ISO standard and make that // the official value DateTime dt; try { dt = new DateTime(timestamp); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("timestamp not valid"); } return dt.toString(ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC)); } /** * Sleep for the specified period of time. Hides the need for try/catch in the * caller. * * @param sleepMs sleep time in milliseconds. * @return true if the slept for the full time, false if interrupted. */ public static final boolean simpleSleepMs(int sleepMs) { try { Thread.sleep(sleepMs); return true; } catch (InterruptedException e) { return false; } } /** * Sleep forever. Intended for code that is event driven and they need a * thread to sit around forever. The thread should have a mechanism for * killing itself and/or be a daemon thread that the jvm can kill upon exit. * <p> * This method <b>NEVER</b> returns. */ public static final void spinForever() { while (true) { try { Thread.sleep(SPIN_LOOP_SLEEP_MS); } catch (InterruptedException e) { // Don't care if we were interrupted, just go back to sleep. } } } }