class Duration {

    public static Duration parse(String duration) {
        String d = duration.trim();
        if (!PATTERN.matcher(d).matches()) {
            throw new DurationFormatException(
                    "Duration string " + duration + " doesn't match pattern " + PATTERN);
        }
        String u = PATTERN_NUMBER.matcher(d).replaceFirst(EMPTY);
        long v = Long.parseLong(PATTERN_UNIT.matcher(d).replaceFirst(EMPTY));
        if (u.length() == 0) {
            // v already in units of milliseconds
        } else if (u.equals(SECOND)) {
            v = v * 1000L;
        } else if (u.equals(MINUTE)) {
            v = v * 1000L * 60L;
        } else if (u.equals(HOUR)) {
            v = v * 1000L * 60L * 60L;
        } else if (u.equals(DAY)) {
            v = v * 1000L * 60L * 60L * 24L;
        } 
        return new Duration(v);
    }

    public  Duration(long milliseconds);

}