ar.com.zauber.garfio.services.impl.HourMinuteTimeConverter.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.zauber.garfio.services.impl.HourMinuteTimeConverter.java

Source

/**
 * Copyright (c) 2007-2009 Zauber S.A. <http://www.zauber.com.ar/>
 *
 * 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 ar.com.zauber.garfio.services.impl;

import java.util.Formatter;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.Validate;

import ar.com.zauber.garfio.services.TimeConverter;

/**
 * TimeConverter that supports times represented in the following format:
 * 
 * <strong>xxh|yym|xxhyym</strong>
 * <p>
 * Examples:
 * <ul>
 *  <li>[+-]2h</li>
 *  <li>[+-]2h3m</li>
 *  <li>[+-]39m</li>
 *  <li>[+-]123m</li>
 * </ul>
 * </p>
 * 
 * @author Fernando Zunino
 * @since 02/06/2007
 */
public class HourMinuteTimeConverter implements TimeConverter {
    private final Pattern timePattern = Pattern
            .compile("^\\s*([+-])?((\\d+)h|((\\d+)h)?\\s*([0-5]?\\d)m" + "|(\\d+)m)\\s*$");

    /** @see TimeConverter#supportsTime(String) */
    public final boolean supportsTime(final String time) {
        Validate.notNull(time);

        return timePattern.matcher(time).matches();
    }

    /** @see TimeConverter#toHours(String)*/
    public final String toHours(final String time) {
        Validate.isTrue(supportsTime(time));

        Matcher m = timePattern.matcher(time);
        m.matches();
        StringBuilder builder = new StringBuilder();

        if (m.group(1) != null) {
            builder.append(m.group(1));
        }
        if (m.group(3) != null) {
            /* Only hour */
            builder.append(m.group(3));
        } else if (m.group(5) != null || m.group(6) != null) {
            /* [xxh]yym */
            if (m.group(5) != null) {
                builder.append(m.group(5));
            } else {
                builder.append("0");
            }
            if (m.group(6) != null) {
                Formatter f = new Formatter();
                f.format(Locale.US, ".%02.0f", 100 * minutesToHours(Integer.valueOf(m.group(6))));
                builder.append(f.toString());
            }
        } else {
            /* Only minutes >= 60 */
            Formatter f = new Formatter();
            f.format(Locale.US, "%.2f", minutesToHours(Integer.valueOf(m.group(7))));
            builder.append(f.toString());
        }

        return builder.toString();
    }

    /** converts to decimal notation a given number of minutes */
    private double minutesToHours(final int minutes) {
        return minutes / 60d;
    }
}