All Downloads are FREE. Search and download functionalities are using the official Maven repository.

stream.util.parser.TimeParser Maven / Gradle / Ivy

The newest version!
/*
 *  streams library
 *
 *  Copyright (C) 2011-2014 by Christian Bockermann, Hendrik Blom
 * 
 *  streams is a library, API and runtime environment for processing high
 *  volume data streams. It is composed of three submodules "stream-api",
 *  "stream-core" and "stream-runtime".
 *
 *  The streams library (and its submodules) is free software: you can 
 *  redistribute it and/or modify it under the terms of the 
 *  GNU Affero General Public License as published by the Free Software 
 *  Foundation, either version 3 of the License, or (at your option) any 
 *  later version.
 *
 *  The stream.ai library (and its submodules) is distributed in the hope
 *  that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package stream.util.parser;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeParser {
	static Logger log = LoggerFactory.getLogger(TimeParser.class);
	static Map UNITS = new LinkedHashMap();

	public final static Long MILLISECOND = 1L;
	public final static Long SECOND = 1000 * MILLISECOND;
	public final static Long MINUTE = 60 * SECOND;
	public final static Long HOUR = 60 * MINUTE;
	public final static Long DAY = 24 * HOUR;
	public final static Long WEEK = 7 * DAY;
	public final static Long MONTH = 30 * DAY;
	public final static Long YEAR = 365 * DAY;

	static {
		UNITS.put("ms", MILLISECOND);
		UNITS.put("years", YEAR);
		UNITS.put("months", MONTH);
		UNITS.put("weeks", WEEK);
		UNITS.put("days", DAY);
		UNITS.put("hours", HOUR);
		UNITS.put("minutes", MINUTE);
		// UNITS.put( "mins", History.MINUTE );
		UNITS.put("seconds", SECOND);
		// UNITS.put( "secs", History.SECOND );
	}

	public static Long parseTime(String str) throws Exception {

		long time = 0L;

		String s = str.trim();
		while (s.length() > 0) {

			String[] tok = readInteger(s);
			Integer i = new Integer(tok[0]);

			s = tok[1].trim();
			tok = readUnit(s);
			String unit = tok[0];

			List uk = findUnits(unit);
			if (uk.size() == 1) {
				unit = uk.get(0);
				log.debug("integer i = " + i + ",  unit = " + unit);
			} else {
				if (uk.isEmpty())
					throw new Exception("Unknown time-unit '" + unit
							+ "' found!");

				if (uk.size() > 1)
					throw new Exception("Ambiguous time-unit '" + unit
							+ "' found!");
			}

			Long ms = UNITS.get(unit);
			time += i * ms;

			s = tok[1].trim();
		}

		return time;
	}

	public static String[] readInteger(String str) {

		StringBuffer s = new StringBuffer();
		int i = 0;
		while (i < str.length() && isDigit(str, i))
			s.append(str.charAt(i++));

		return new String[] { s.toString(), str.substring(i).trim() };
	}

	public static String[] readUnit(String str) {

		StringBuffer s = new StringBuffer();
		int i = 0;
		while (i < str.length() & isUnitChar(str, i))
			s.append(str.charAt(i++));

		return new String[] { s.toString(), str.substring(i) };
	}

	public static boolean isDigit(String str, int i) {
		if (i >= 0 && i < str.length()) {
			char ch = str.charAt(i);
			return Character.isDigit(ch);
		} else
			return false;
	}

	public static boolean isUnitChar(String str, int i) {
		if (i >= 0 && i < str.length()) {
			char ch = str.charAt(i);
			return Character.isLetter(ch);
		} else
			return false;
	}

	public static List findUnits(String str) {

		LinkedList units = new LinkedList();

		for (String key : UNITS.keySet()) {
			if (key.startsWith(str))
				units.add(key);
		}

		return units;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy