src/net/fortuna/ical4j/model/Dur.java

branch
ICAL4J_EMBED_1
changeset 15
cc93757aeca3
parent 14
5ae3e5665a0b
child 18
6dcaece8ec41
     1.1 --- a/src/net/fortuna/ical4j/model/Dur.java	Thu Feb 12 18:02:00 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,528 +0,0 @@
     1.4 -/**
     1.5 - * Copyright (c) 2012, Ben Fortuna
     1.6 - * All rights reserved.
     1.7 - *
     1.8 - * Redistribution and use in source and binary forms, with or without
     1.9 - * modification, are permitted provided that the following conditions
    1.10 - * are met:
    1.11 - *
    1.12 - *  o Redistributions of source code must retain the above copyright
    1.13 - * notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *  o Redistributions in binary form must reproduce the above copyright
    1.16 - * notice, this list of conditions and the following disclaimer in the
    1.17 - * documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 - * may be used to endorse or promote products derived from this software
    1.21 - * without specific prior written permission.
    1.22 - *
    1.23 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 - */
    1.35 -package net.fortuna.ical4j.model;
    1.36 -
    1.37 -import java.io.IOException;
    1.38 -import java.io.Serializable;
    1.39 -import java.util.Calendar;
    1.40 -import java.util.Date;
    1.41 -import java.util.StringTokenizer;
    1.42 -import net.fortuna.ical4j.util.Dates;
    1.43 -
    1.44 -import org.apache.commons.lang3.builder.HashCodeBuilder;
    1.45 -
    1.46 -/**
    1.47 - * $Id$
    1.48 - *
    1.49 - * Created on 20/06/2005
    1.50 - *
    1.51 - * Represents a duration of time in iCalendar. Note that according to RFC2445 durations represented in weeks are
    1.52 - * mutually exclusive of other duration fields.
    1.53 - * 
    1.54 - * <pre>
    1.55 - *  4.3.6   Duration
    1.56 - *  
    1.57 - *     Value Name: DURATION
    1.58 - *  
    1.59 - *     Purpose: This value type is used to identify properties that contain
    1.60 - *     a duration of time.
    1.61 - *  
    1.62 - *     Formal Definition: The value type is defined by the following
    1.63 - *     notation:
    1.64 - *  
    1.65 - *       dur-value  = ([&quot;+&quot;] / &quot;-&quot;) &quot;P&quot; (dur-date / dur-time / dur-week)
    1.66 - *  
    1.67 - *       dur-date   = dur-day [dur-time]
    1.68 - *       dur-time   = &quot;T&quot; (dur-hour / dur-minute / dur-second)
    1.69 - *       dur-week   = 1*DIGIT &quot;W&quot;
    1.70 - *       dur-hour   = 1*DIGIT &quot;H&quot; [dur-minute]
    1.71 - *       dur-minute = 1*DIGIT &quot;M&quot; [dur-second]
    1.72 - *       dur-second = 1*DIGIT &quot;S&quot;
    1.73 - *       dur-day    = 1*DIGIT &quot;D&quot;
    1.74 - * </pre>
    1.75 - * 
    1.76 - * @author Ben Fortuna
    1.77 - */
    1.78 -public class Dur implements Comparable, Serializable {
    1.79 -
    1.80 -    private static final long serialVersionUID = 5013232281547134583L;
    1.81 -
    1.82 -    private static final int DAYS_PER_WEEK = 7;
    1.83 -
    1.84 -    private static final int SECONDS_PER_MINUTE = 60;
    1.85 -
    1.86 -    private static final int MINUTES_PER_HOUR = 60;
    1.87 -
    1.88 -    private static final int HOURS_PER_DAY = 24;
    1.89 -
    1.90 -    private static final int DAYS_PER_YEAR = 365;
    1.91 -
    1.92 -    private boolean negative;
    1.93 -
    1.94 -    private int weeks;
    1.95 -
    1.96 -    private int days;
    1.97 -
    1.98 -    private int hours;
    1.99 -
   1.100 -    private int minutes;
   1.101 -
   1.102 -    private int seconds;
   1.103 -
   1.104 -    /**
   1.105 -     * Constructs a new duration instance from a string representation.
   1.106 -     * @param value a string representation of a duration
   1.107 -     */
   1.108 -    public Dur(final String value) {
   1.109 -        negative = false;
   1.110 -        weeks = 0;
   1.111 -        days = 0;
   1.112 -        hours = 0;
   1.113 -        minutes = 0;
   1.114 -        seconds = 0;
   1.115 -
   1.116 -        String token = null;
   1.117 -        String prevToken = null;
   1.118 -
   1.119 -        final StringTokenizer t = new StringTokenizer(value, "+-PWDTHMS", true);
   1.120 -        while (t.hasMoreTokens()) {
   1.121 -            prevToken = token;
   1.122 -            token = t.nextToken();
   1.123 -
   1.124 -            if ("+".equals(token)) {
   1.125 -                negative = false;
   1.126 -            }
   1.127 -            else if ("-".equals(token)) {
   1.128 -                negative = true;
   1.129 -            }
   1.130 -            else if ("P".equals(token)) {
   1.131 -                // does nothing..
   1.132 -            }
   1.133 -            else if ("W".equals(token)) {
   1.134 -                weeks = Integer.parseInt(prevToken);
   1.135 -            }
   1.136 -            else if ("D".equals(token)) {
   1.137 -                days = Integer.parseInt(prevToken);
   1.138 -            }
   1.139 -            else if ("T".equals(token)) {
   1.140 -                // does nothing..
   1.141 -            }
   1.142 -            else if ("H".equals(token)) {
   1.143 -                hours = Integer.parseInt(prevToken);
   1.144 -            }
   1.145 -            else if ("M".equals(token)) {
   1.146 -                minutes = Integer.parseInt(prevToken);
   1.147 -            }
   1.148 -            else if ("S".equals(token)) {
   1.149 -                seconds = Integer.parseInt(prevToken);
   1.150 -            }
   1.151 -        }
   1.152 -    }
   1.153 -
   1.154 -    /**
   1.155 -     * Constructs a new duration from the specified weeks.
   1.156 -     * @param weeks a duration in weeks.
   1.157 -     */
   1.158 -    public Dur(final int weeks) {
   1.159 -        this.weeks = Math.abs(weeks);
   1.160 -        this.days = 0;
   1.161 -        this.hours = 0;
   1.162 -        this.minutes = 0;
   1.163 -        this.seconds = 0;
   1.164 -        this.negative = weeks < 0;
   1.165 -    }
   1.166 -
   1.167 -    /**
   1.168 -     * Constructs a new duration from the specified arguments.
   1.169 -     * @param days duration in days
   1.170 -     * @param hours duration in hours
   1.171 -     * @param minutes duration in minutes
   1.172 -     * @param seconds duration in seconds
   1.173 -     */
   1.174 -    public Dur(final int days, final int hours, final int minutes,
   1.175 -            final int seconds) {
   1.176 -
   1.177 -        if (!(days >= 0 && hours >= 0 && minutes >= 0 && seconds >= 0)
   1.178 -                && !(days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0)) {
   1.179 -            
   1.180 -            throw new IllegalArgumentException("Invalid duration representation");
   1.181 -        }
   1.182 -        
   1.183 -        this.weeks = 0;
   1.184 -        this.days = Math.abs(days);
   1.185 -        this.hours = Math.abs(hours);
   1.186 -        this.minutes = Math.abs(minutes);
   1.187 -        this.seconds = Math.abs(seconds);
   1.188 -        
   1.189 -        this.negative = days < 0 || hours < 0 || minutes < 0 || seconds < 0;
   1.190 -    }
   1.191 -
   1.192 -    /**
   1.193 -     * Constructs a new duration representing the time between the two specified dates. The end date may precede the
   1.194 -     * start date in order to represent a negative duration.
   1.195 -     * @param date1 the first date of the duration
   1.196 -     * @param date2 the second date of the duration
   1.197 -     */
   1.198 -    public Dur(final Date date1, final Date date2) {
   1.199 -        
   1.200 -        Date start = null;
   1.201 -        Date end = null;
   1.202 -        
   1.203 -        // Negative range? (start occurs after end)
   1.204 -        negative = date1.compareTo(date2) > 0;
   1.205 -        if (negative) {
   1.206 -            // Swap the dates (which eliminates the need to bother with
   1.207 -            // negative after this!)
   1.208 -            start = date2;
   1.209 -            end = date1;
   1.210 -        }
   1.211 -        else {
   1.212 -            start = date1;
   1.213 -            end = date2;
   1.214 -        }
   1.215 -
   1.216 -        final Calendar startCal;
   1.217 -        if (start instanceof net.fortuna.ical4j.model.Date) {
   1.218 -            startCal = Dates.getCalendarInstance((net.fortuna.ical4j.model.Date)start);
   1.219 -        } else {
   1.220 -            startCal = Calendar.getInstance();
   1.221 -        }
   1.222 -        startCal.setTime(start);
   1.223 -        final Calendar endCal = Calendar.getInstance(startCal.getTimeZone());
   1.224 -        endCal.setTime(end);
   1.225 -
   1.226 -        // Init our duration interval (which is in units that evolve as we
   1.227 -        // compute, below)
   1.228 -        int dur = 0;
   1.229 -
   1.230 -        // Count days to get to the right year (loop in the very rare chance
   1.231 -        // that a leap year causes us to come up short)
   1.232 -        int nYears = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
   1.233 -        while (nYears > 0) {
   1.234 -            startCal.add(Calendar.DATE, DAYS_PER_YEAR * nYears);
   1.235 -            dur += DAYS_PER_YEAR * nYears;
   1.236 -            nYears = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
   1.237 -        }
   1.238 -
   1.239 -        // Count days to get to the right day
   1.240 -        dur += endCal.get(Calendar.DAY_OF_YEAR)
   1.241 -                - startCal.get(Calendar.DAY_OF_YEAR);
   1.242 -
   1.243 -        // Count hours to get to right hour
   1.244 -        dur *= HOURS_PER_DAY; // days -> hours
   1.245 -        dur += endCal.get(Calendar.HOUR_OF_DAY)
   1.246 -                - startCal.get(Calendar.HOUR_OF_DAY);
   1.247 -
   1.248 -        // ... to the right minute
   1.249 -        dur *= MINUTES_PER_HOUR; // hours -> minutes
   1.250 -        dur += endCal.get(Calendar.MINUTE) - startCal.get(Calendar.MINUTE);
   1.251 -
   1.252 -        // ... and second
   1.253 -        dur *= SECONDS_PER_MINUTE; // minutes -> seconds
   1.254 -        dur += endCal.get(Calendar.SECOND) - startCal.get(Calendar.SECOND);
   1.255 -
   1.256 -        // Now unwind our units
   1.257 -        seconds = dur % SECONDS_PER_MINUTE;
   1.258 -        dur = dur / SECONDS_PER_MINUTE; // seconds -> minutes (drop remainder seconds)
   1.259 -        minutes = dur % MINUTES_PER_HOUR;
   1.260 -        dur /= MINUTES_PER_HOUR; // minutes -> hours (drop remainder minutes)
   1.261 -        hours = dur % HOURS_PER_DAY;
   1.262 -        dur /= HOURS_PER_DAY; // hours -> days (drop remainder hours)
   1.263 -        days = dur;
   1.264 -        weeks = 0;
   1.265 -
   1.266 -        // Special case for week-only representation
   1.267 -        if (seconds == 0 && minutes == 0 && hours == 0
   1.268 -                && (days % DAYS_PER_WEEK) == 0) {
   1.269 -            weeks = days / DAYS_PER_WEEK;
   1.270 -            days = 0;
   1.271 -        }
   1.272 -    }
   1.273 -
   1.274 -    /**
   1.275 -     * Returns a date representing the end of this duration from the specified start date.
   1.276 -     * @param start the date to start the duration
   1.277 -     * @return the end of the duration as a date
   1.278 -     */
   1.279 -    public final Date getTime(final Date start) {
   1.280 -        final Calendar cal;
   1.281 -        if (start instanceof net.fortuna.ical4j.model.Date) {
   1.282 -            cal = Dates.getCalendarInstance((net.fortuna.ical4j.model.Date)start);
   1.283 -        } else {
   1.284 -            cal = Calendar.getInstance();
   1.285 -        }
   1.286 -
   1.287 -        cal.setTime(start);
   1.288 -        if (isNegative()) {
   1.289 -            cal.add(Calendar.WEEK_OF_YEAR, -weeks);
   1.290 -            cal.add(Calendar.DAY_OF_WEEK, -days);
   1.291 -            cal.add(Calendar.HOUR_OF_DAY, -hours);
   1.292 -            cal.add(Calendar.MINUTE, -minutes);
   1.293 -            cal.add(Calendar.SECOND, -seconds);
   1.294 -        }
   1.295 -        else {
   1.296 -            cal.add(Calendar.WEEK_OF_YEAR, weeks);
   1.297 -            cal.add(Calendar.DAY_OF_WEEK, days);
   1.298 -            cal.add(Calendar.HOUR_OF_DAY, hours);
   1.299 -            cal.add(Calendar.MINUTE, minutes);
   1.300 -            cal.add(Calendar.SECOND, seconds);
   1.301 -        }
   1.302 -        return cal.getTime();
   1.303 -    }
   1.304 -
   1.305 -    /**
   1.306 -     * Provides a negation of this instance.
   1.307 -     * @return a Dur instance that represents a negation of this instance
   1.308 -     */
   1.309 -    public final Dur negate() {
   1.310 -        final Dur negated = new Dur(days, hours, minutes, seconds);
   1.311 -        negated.weeks = weeks;
   1.312 -        negated.negative = !negative;
   1.313 -        return negated;
   1.314 -    }
   1.315 -    
   1.316 -    /**
   1.317 -     * Add two durations. Durations may only be added if they are both positive
   1.318 -     * or both negative durations.
   1.319 -     * @param duration the duration to add to this duration
   1.320 -     * @return a new instance representing the sum of the two durations.
   1.321 -     */
   1.322 -    public final Dur add(final Dur duration) {
   1.323 -        if ((!isNegative() && duration.isNegative())
   1.324 -                || (isNegative() && !duration.isNegative())) {
   1.325 -            
   1.326 -            throw new IllegalArgumentException(
   1.327 -                    "Cannot add a negative and a positive duration");
   1.328 -        }
   1.329 -        
   1.330 -        Dur sum = null;
   1.331 -        if (weeks > 0 && duration.weeks > 0) {
   1.332 -            sum = new Dur(weeks + duration.weeks);
   1.333 -        }
   1.334 -        else {
   1.335 -            int daySum = (weeks > 0) ? weeks * DAYS_PER_WEEK + days : days;
   1.336 -            int hourSum = hours;
   1.337 -            int minuteSum = minutes;
   1.338 -            int secondSum = seconds;
   1.339 -            
   1.340 -            if ((secondSum + duration.seconds) / SECONDS_PER_MINUTE > 0) {
   1.341 -                minuteSum += (secondSum + duration.seconds) / SECONDS_PER_MINUTE;
   1.342 -                secondSum = (secondSum + duration.seconds) % SECONDS_PER_MINUTE;
   1.343 -            }
   1.344 -            else {
   1.345 -                secondSum += duration.seconds;
   1.346 -            }
   1.347 -            
   1.348 -            if ((minuteSum + duration.minutes) / MINUTES_PER_HOUR > 0) {
   1.349 -                hourSum += (minuteSum + duration.minutes) / MINUTES_PER_HOUR;
   1.350 -                minuteSum = (minuteSum + duration.minutes) % MINUTES_PER_HOUR;
   1.351 -            }
   1.352 -            else {
   1.353 -                minuteSum += duration.minutes;
   1.354 -            }
   1.355 -            
   1.356 -            if ((hourSum + duration.hours) / HOURS_PER_DAY > 0) {
   1.357 -                daySum += (hourSum + duration.hours) / HOURS_PER_DAY;
   1.358 -                hourSum = (hourSum + duration.hours) % HOURS_PER_DAY;
   1.359 -            }
   1.360 -            else {
   1.361 -                hourSum += duration.hours;
   1.362 -            }
   1.363 -            
   1.364 -            daySum += (duration.weeks > 0) ? duration.weeks * DAYS_PER_WEEK
   1.365 -                    + duration.days : duration.days;
   1.366 -            
   1.367 -            sum = new Dur(daySum, hourSum, minuteSum, secondSum);
   1.368 -        }
   1.369 -        sum.negative = negative;
   1.370 -        return sum;
   1.371 -    }
   1.372 -    
   1.373 -    /**
   1.374 -     * {@inheritDoc}
   1.375 -     */
   1.376 -    public final String toString() {
   1.377 -        final StringBuffer b = new StringBuffer();
   1.378 -        if (negative) {
   1.379 -            b.append('-');
   1.380 -        }
   1.381 -        b.append('P');
   1.382 -        if (weeks > 0) {
   1.383 -            b.append(weeks);
   1.384 -            b.append('W');
   1.385 -        }
   1.386 -        else {
   1.387 -            if (days > 0) {
   1.388 -                b.append(days);
   1.389 -                b.append('D');
   1.390 -            }
   1.391 -            if (hours > 0 || minutes > 0 || seconds > 0) {
   1.392 -                b.append('T');
   1.393 -                if (hours > 0) {
   1.394 -                    b.append(hours);
   1.395 -                    b.append('H');
   1.396 -                }
   1.397 -                if (minutes > 0) {
   1.398 -                    b.append(minutes);
   1.399 -                    b.append('M');
   1.400 -                }
   1.401 -                if (seconds > 0) {
   1.402 -                    b.append(seconds);
   1.403 -                    b.append('S');
   1.404 -                }
   1.405 -            }
   1.406 -            // handle case of zero length duration
   1.407 -            if ((hours + minutes + seconds + days + weeks) == 0) {
   1.408 -                b.append("T0S");
   1.409 -            }
   1.410 -        }
   1.411 -        return b.toString();
   1.412 -    }
   1.413 -
   1.414 -    /**
   1.415 -     * {@inheritDoc}
   1.416 -     */
   1.417 -    public final int compareTo(final Object arg0) {
   1.418 -        return compareTo((Dur) arg0);
   1.419 -    }
   1.420 -
   1.421 -    /**
   1.422 -     * Compares this duration with another, acording to their length.
   1.423 -     * @param arg0 another duration instance
   1.424 -     * @return a postive value if this duration is longer, zero if the duration
   1.425 -     * lengths are equal, otherwise a negative value
   1.426 -     */
   1.427 -    public final int compareTo(final Dur arg0) {
   1.428 -        int result;
   1.429 -        if (isNegative() != arg0.isNegative()) {
   1.430 -            // return Boolean.valueOf(isNegative()).compareTo(Boolean.valueOf(arg0.isNegative()));
   1.431 -            // for pre-java 1.5 compatibility..
   1.432 -            if (isNegative()) {
   1.433 -                return Integer.MIN_VALUE;
   1.434 -            }
   1.435 -            else {
   1.436 -                return Integer.MAX_VALUE;
   1.437 -            }
   1.438 -        }
   1.439 -        else if (getWeeks() != arg0.getWeeks()) {
   1.440 -            result = getWeeks() - arg0.getWeeks();
   1.441 -        }
   1.442 -        else if (getDays() != arg0.getDays()) {
   1.443 -            result = getDays() - arg0.getDays();
   1.444 -        }
   1.445 -        else if (getHours() != arg0.getHours()) {
   1.446 -            result = getHours() - arg0.getHours();
   1.447 -        }
   1.448 -        else if (getMinutes() != arg0.getMinutes()) {
   1.449 -            result = getMinutes() - arg0.getMinutes();
   1.450 -        }
   1.451 -        else {
   1.452 -            result = getSeconds() - arg0.getSeconds();
   1.453 -        }
   1.454 -        // invert sense of all tests if both durations are negative
   1.455 -        if (isNegative()) {
   1.456 -            return -result;
   1.457 -        }
   1.458 -        else {
   1.459 -            return result;
   1.460 -        }
   1.461 -    }
   1.462 -
   1.463 -    /**
   1.464 -     * {@inheritDoc}
   1.465 -     */
   1.466 -    public boolean equals(final Object obj) {
   1.467 -        if (obj instanceof Dur) {
   1.468 -            return ((Dur) obj).compareTo(this) == 0;
   1.469 -        }
   1.470 -        return super.equals(obj);
   1.471 -    }
   1.472 -    
   1.473 -    /**
   1.474 -     * {@inheritDoc}
   1.475 -     */
   1.476 -    public int hashCode() {
   1.477 -        return new HashCodeBuilder().append(weeks).append(days).append(
   1.478 -                hours).append(minutes).append(seconds).append(negative).toHashCode();
   1.479 -    }
   1.480 -    
   1.481 -    /**
   1.482 -     * @return Returns the days.
   1.483 -     */
   1.484 -    public final int getDays() {
   1.485 -        return days;
   1.486 -    }
   1.487 -
   1.488 -    /**
   1.489 -     * @return Returns the hours.
   1.490 -     */
   1.491 -    public final int getHours() {
   1.492 -        return hours;
   1.493 -    }
   1.494 -
   1.495 -    /**
   1.496 -     * @return Returns the minutes.
   1.497 -     */
   1.498 -    public final int getMinutes() {
   1.499 -        return minutes;
   1.500 -    }
   1.501 -
   1.502 -    /**
   1.503 -     * @return Returns the negative.
   1.504 -     */
   1.505 -    public final boolean isNegative() {
   1.506 -        return negative;
   1.507 -    }
   1.508 -
   1.509 -    /**
   1.510 -     * @return Returns the seconds.
   1.511 -     */
   1.512 -    public final int getSeconds() {
   1.513 -        return seconds;
   1.514 -    }
   1.515 -
   1.516 -    /**
   1.517 -     * @return Returns the weeks.
   1.518 -     */
   1.519 -    public final int getWeeks() {
   1.520 -        return weeks;
   1.521 -    }
   1.522 -
   1.523 -    /**
   1.524 -     * @param stream
   1.525 -     * @throws IOException
   1.526 -     * @throws ClassNotFoundException
   1.527 -     */
   1.528 -    private void readObject(final java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {
   1.529 -        stream.defaultReadObject();
   1.530 -    }
   1.531 -}

mercurial