Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;

/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */

public class Main {

    /** Millisecond conversion constants */
    private static final long MSEC = 1;
    private static final long SECS = 1000;
    private static final long MINS = 60 * 1000;
    private static final long HOUR = 60 * 60 * 1000;

    /**
     * Parses a time period into a long.
     *
     * Translates possible [msec|sec|min|h] suffixes
     *
     * For example:
     *   "1"      ->  1 (msec)
     *   "1msec   ->  1 (msec)
     *   "1sec"   ->  1000 (msecs)
     *   "1min"   ->  60000 (msecs)
     *   "1h"     ->  3600000 (msecs)
     * 
     * Accepts negative periods, e.g. "-1"
     * 
     * @param period the stringfied time period
     * @return the parsed time period as long
     * @throws NumberFormatException
     */
    public static long parseTimePeriod(String period) {
        try {
            String s = period.toLowerCase();
            long factor;

            // look for suffix
            if (s.endsWith("msec")) {
                s = s.substring(0, s.lastIndexOf("msec"));
                factor = MSEC;
            } else if (s.endsWith("sec")) {
                s = s.substring(0, s.lastIndexOf("sec"));
                factor = SECS;
            } else if (s.endsWith("min")) {
                s = s.substring(0, s.lastIndexOf("min"));
                factor = MINS;
            } else if (s.endsWith("h")) {
                s = s.substring(0, s.lastIndexOf("h"));
                factor = HOUR;
            } else {
                factor = 1;
            }
            return Long.parseLong(s) * factor;
        } catch (RuntimeException e) {
            // thrown in addition when period is 'null'
            throw new NumberFormatException("For input time period: '" + period + "'");
        }
    }

    /**
     * Same like parseTimePeriod(), but guards for negative entries.
     * 
     * @param period the stringfied time period
     * @return the parsed time period as long
     * @throws NumberFormatException
     */
    public static long parsePositiveTimePeriod(String period) {
        long retval = parseTimePeriod(period);
        if (retval < 0) {
            throw new NumberFormatException("Negative input time period: '" + period + "'");
        }
        return retval;
    }

}