Package com.google.ical.values

Examples of com.google.ical.values.DateValue


    Random rnd = new Random();
    for (DateValue[] values :
           Arrays.asList(new DateValue[][] { DATE_VALUES, DATE_TIME_VALUES })) {
      for (int i = 0; i < 100; i++) {
        int k = rnd.nextInt(values.length / 2);
        DateValue start = values[k];
        DateValue end = values[k + rnd.nextInt(values.length - k)];
        PeriodValue p = PeriodValueImpl.create(start, end);
        assertEquals(start, p.start());
        assertEquals(end, p.end());
      }
    }
    // check out of order failures
    for (DateValue[] values :
           Arrays.asList(new DateValue[][] { DATE_VALUES, DATE_TIME_VALUES })) {
      for (int i = 0; i < 100; i++) {
        int k = rnd.nextInt(values.length / 2);
        DateValue start = values[k + 1 + rnd.nextInt(values.length - k - 1)];
        DateValue end = values[k];
        try {
          PeriodValue p = PeriodValueImpl.create(start, end);
          fail("Bad PeriodValue: " + p);
        } catch (RuntimeException ex) {
          // pass
        }
      }
    }
    // check cross genus failures
    for (int i = 0; i < 100; i++) {
      DateValue a = DATE_VALUES[rnd.nextInt(DATE_VALUES.length)],
                b = DATE_TIME_VALUES[rnd.nextInt(DATE_TIME_VALUES.length)];
      if (a.compareTo(b) > 0) {
        DateValue t = a;
        a = b;
        b = t;
      }
      try {
        PeriodValue p = PeriodValueImpl.create(a, b);
View Full Code Here


      }
    }
  }

  public void testDurationConstructor() throws Exception {
    final DateValue DV = new DateValueImpl(2005, 2, 15);
    final DateTimeValue DTV0 = new DateTimeValueImpl(2005, 2, 15, 0, 0, 0),
      DTV12 = new DateTimeValueImpl(2005, 2, 15, 12, 0, 0);
    DateValue ONE_DAY = new DateValueImpl(0, 0, 1),
      YESTERDAY = new DateValueImpl(0, 0, -1),
      ONE_WEEK = new DateValueImpl(0, 0, 7),
      ONE_MONTH = new DateValueImpl(0, 1, 0),
      ONE_YEAR = new DateValueImpl(1, 0, 0);
    DateTimeValue ONE_HOUR = new DateTimeValueImpl(0, 0, 0, 1, 0, 0),
View Full Code Here

      if (collisionRate > 0.10) {
        fail("Unacceptable hash collision rate: " + collisionRate + " for "
             + Arrays.asList(values));
      }
      for (int i = 0; i < 10000; ++i) {
        DateValue a = values[rnd.nextInt(values.length)],
                  b = values[rnd.nextInt(values.length)];
        implies(a.equals(b), a.hashCode() == b.hashCode());
      }
      for (int i = 0; i < 1000; ++i) {
        int a = rnd.nextInt(4),
            b = a + rnd.nextInt(4),
            c = rnd.nextInt(4),
            d = c + rnd.nextInt(4);
        // should see at least 6 equal pairs chosen per 100 runs
        PeriodValue pAB = PeriodValueImpl.create(values[a], values[b]),
                    pCD = PeriodValueImpl.create(values[c], values[d]);
        assertEquals(a == c && b == d, pAB.equals(pCD));
        implies(pAB.equals(pCD), pAB.hashCode() == pCD.hashCode());
      }
      for (int i = 0; i < 10000; ++i) {
        int a = rnd.nextInt(values.length),
            b = rnd.nextInt(values.length);
        DateValue dvA = values[a], dvB = values[b];
        assertEquals(a == b, dvA.equals(dvB));
        assertEquals(dvA.equals(dvB), 0 == dvA.compareTo(dvB));
      }
    }
  }
View Full Code Here

        {
          // step back one interval
          DTBuilder dtStartMinus1B = new DTBuilder(dtStart);
          dtStartMinus1B.day -= interval;
          DateValue dtStartMinus1 = dtStartMinus1B.toDate();
          year = dtStartMinus1.year();
          month = dtStartMinus1.month();
          date = dtStartMinus1.day();
          nDays = TimeUtils.monthLength(year, month);
        }

        @Override
        boolean generate(DTBuilder builder) {
View Full Code Here

  }

  public DateValue next() {
    requirePending();
    if (null == pending) { throw new NoSuchElementException(); }
    DateValue head = pending.head();
    reattach(pending);
    pending = null;
    return head;
  }
View Full Code Here

            if (done) { return false; }

            // (1) Make sure that builder is appropriately initialized so that
            // we only generate instances in the next set

            DateValue d0 = null;
            if (null != pushback) {
              d0 = pushback;
              builder.year = d0.year();
              builder.month = d0.month();
              builder.day = d0.day();
              pushback = null;
            } else if (!first) {
              // we need to skip ahead to the next item since we didn't exhaust
              // the last period
              switch (freq) {
                case YEARLY:
                  if (!yearGenerator.generate(builder)) { return false; }
                  // $FALL-THROUGH$
                case MONTHLY:
                  while (!monthGenerator.generate(builder)) {
                    if (!yearGenerator.generate(builder)) { return false; }
                  }
                  break;
                case WEEKLY:
                  // consume because just incrementing date doesn't do anything
                  DateValue nextWeek =
                    Util.nextWeekStart(builder.toDateTime(), wkst);
                  do {
                    if (!serialInstanceGenerator.generate(builder)) {
                      return false;
                    }
                  } while (builder.compareTo(nextWeek) < 0);
                  d0 = builder.toDateTime();
                  break;
                default:
                  break;
              }
            } else {
              first = false;
            }

            // (2) Build a set of the dates in the year/month/week that match
            // the other rule.
            List<DateValue> dates = new ArrayList<DateValue>();
            if (null != d0) { dates.add(d0); }

            // Optimization: if min(bySetPos) > 0 then we already have absolute
            // positions, so we don't need to generate all of the instances for
            // the period.
            // This speeds up things like the first weekday of the year:
            //     RRULE:FREQ=YEARLY;BYDAY=MO,TU,WE,TH,FR,BYSETPOS=1
            // that would otherwise generate 260+ instances per one emitted
            // TODO(msamuel): this may be premature.  If needed, We could
            // improve more generally by inferring a BYMONTH generator based on
            // distribution of set positions within the year.
            int limit = allPositive ? maxPos : Integer.MAX_VALUE;

            while (limit > dates.size()) {
              if (!serialInstanceGenerator.generate(builder)) {
                // If we can't generate any, then make sure we return false
                // once the instances we have generated are exhausted.
                // If this is returning false due to some artificial limit, such
                // as the 100 year limit in serialYearGenerator, then we exit
                // via an exception because otherwise we would pick the wrong
                // elements for some uSetPoses that contain negative elements.
                done = true;
                break;
              }
              DateValue d = builder.toDateTime();
              boolean contained = false;
              if (null == d0) {
                d0 = d;
                contained = true;
              } else {
                switch (freq) {
                  case WEEKLY:
                    int nb = TimeUtils.daysBetween(d, d0);
                    // Two dates (d, d0) are in the same week
                    // if there isn't a whole week in between them and the
                    // later day is later in the week than the earlier day.
                    contained =
                      nb < 7
                      && ((7 + Weekday.valueOf(d).javaDayNum
                           - wkst.javaDayNum) % 7)
                      > ((7 + Weekday.valueOf(d0).javaDayNum
                          - wkst.javaDayNum) % 7);
                    break;
                  case MONTHLY:
                    contained =
                      d0.month() == d.month() && d0.year() == d.year();
                    break;
                  case YEARLY:
                    contained = d0.year() == d.year();
                    break;
                  default:
                    done = true;
                    return false;
                }
              }
              if (contained) {
                dates.add(d);
              } else {
                // reached end of the set
                pushback = d;  // save d so we can use it later
                break;
              }
            }

            // (3) Resolve the positions to absolute positions and order them
            int[] absSetPos;
            if (allPositive) {
              absSetPos = uSetPos;
            } else {
              IntSet uAbsSetPos = new IntSet();
              for (int j = 0; j < uSetPos.length; ++j) {
                int p = uSetPos[j];
                if (p < 0) { p = dates.size() + p + 1; }
                uAbsSetPos.add(p);
              }
              absSetPos = uAbsSetPos.toIntArray();
            }

            candidates = new ArrayList<DateValue>();
            for (int p : absSetPos) {
              if (p >= 1 && p <= dates.size()) {  // p is 1-indexed
                candidates.add(dates.get(p - 1));
              }
            }
            i = 0;
            if (candidates.isEmpty()) {
              // none in this region, so keep looking
              candidates = null;
              continue;
            }
          }
          // (5) Emit a date.  It will be checked against the end condition and
          // dtStart elsewhere
          DateValue d = candidates.get(i++);
          builder.year = d.year();
          builder.month = d.month();
          builder.day = d.day();
          if (d instanceof TimeValue) {
            TimeValue t = (TimeValue) d;
            builder.hour = t.hour();
            builder.minute = t.minute();
            builder.second = t.second();
View Full Code Here

   * wkst.
   * @param builder non null.
   * @param wkst the day of the week that the week starts on
   */
  static void rollToNextWeekStart(DTBuilder builder, Weekday wkst) {
    DateValue bd = builder.toDate();
    builder.day += (7 - ((7 + (Weekday.valueOf(bd).javaDayNum
                               - wkst.javaDayNum))
                         % 7)) % 7;
    builder.normalize();
  }
View Full Code Here

    assert null != tzid;
    assert null != dtStart;

    Frequency freq = rrule.getFreq();
    Weekday wkst = rrule.getWkSt();
    DateValue untilUtc = rrule.getUntil();
    int count = rrule.getCount();
    int interval = rrule.getInterval();
    WeekdayNum[] byDay = rrule.getByDay().toArray(new WeekdayNum[0]);
    int[] byMonth = rrule.getByMonth();
    int[] byMonthDay = rrule.getByMonthDay();
    int[] byWeekNo = rrule.getByWeekNo();
    int[] byYearDay = rrule.getByYearDay();
    int[] bySetPos = rrule.getBySetPos();
    int[] byHour = rrule.getByHour();
    int[] byMinute = rrule.getByMinute();
    int[] bySecond = rrule.getBySecond();

    if (interval <= 0) {  interval = 1; }

    if (null == wkst) {
      wkst = Weekday.MO;
    }

    // Optimize out BYSETPOS where possible.
    if (bySetPos.length != 0) {
      switch (freq) {
        case HOURLY:
          // ;BYHOUR=3,6,9;BYSETPOS=-1,1
          //     is equivalent to
          // ;BYHOUR=3,9
          if (byHour.length != 0 && byMinute.length <= 1
              && bySecond.length <= 1) {
            byHour = filterBySetPos(byHour, bySetPos);
          }
          // Handling bySetPos for rules that are more frequent than daily
          // tends to lead to large amounts of processor being used before other
          // work limiting features can kick in since there many seconds between
          // dtStart and where the year limit kicks in.
          // There are no known use cases for the use of bySetPos with hourly
          // minutely and secondly rules so we just ignore it.
          bySetPos = NO_INTS;
          break;
        case MINUTELY:
          // ;BYHOUR=3,6,9;BYSETPOS=-1,1
          //     is equivalent to
          // ;BYHOUR=3,9
          if (byMinute.length != 0 && bySecond.length <= 1) {
            byMinute = filterBySetPos(byMinute, bySetPos);
          }
          // See bySetPos handling comment above.
          bySetPos = NO_INTS;
          break;
        case SECONDLY:
          // ;BYHOUR=3,6,9;BYSETPOS=-1,1
          //     is equivalent to
          // ;BYHOUR=3,9
          if (bySecond.length != 0) {
            bySecond = filterBySetPos(bySecond, bySetPos);
          }
          // See bySetPos handling comment above.
          bySetPos = NO_INTS;
          break;
        default:
      }
    }

    DateValue start = dtStart;
    if (bySetPos.length != 0) {
      // Roll back till the beginning of the period to make sure that any
      // positive indices are indexed properly.
      // The actual iterator implementation is responsible for anything
      // < dtStart.
      switch (freq) {
        case YEARLY:
          start = dtStart instanceof TimeValue
              ? new DateTimeValueImpl(start.year(), 1, 1, 0, 0, 0)
              : new DateValueImpl(start.year(), 1, 1);
          break;
        case MONTHLY:
          start = dtStart instanceof TimeValue
              ? new DateTimeValueImpl(start.year(), start.month(), 1, 0, 0, 0)
              : new DateValueImpl(start.year(), start.month(), 1);
          break;
        case WEEKLY:
          int d = (7 + wkst.ordinal() - Weekday.valueOf(dtStart).ordinal()) % 7;
          start = TimeUtils.add(dtStart, new DateValueImpl(0, 0, -d));
          break;
View Full Code Here

  /** fetch and return the next date in this recurrence. */
  public DateValue next() {
    if (null == this.pendingUtc_) {
      this.fetchNext();
    }
    DateValue next = this.pendingUtc_;
    this.pendingUtc_ = null;
    return next;
  }
View Full Code Here

    // generate it again.
    if (this.pendingUtc_ !=  null && dateUtc.compareTo(this.pendingUtc_) <= 0) {
      return;
    }

    DateValue dateLocal = TimeUtils.fromUtc(dateUtc, tzid_);
    // Short-circuit if we're already past dateUtc.
    if (dateLocal.compareTo(this.builder_.toDate()) <= 0) { return; }
    this.pendingUtc_ = null;

    try {
      if (this.canShortcutAdvance_) {
        // skip years before date.year
        if (this.builder_.year < dateLocal.year()) {
          do {
            if (!this.yearGenerator_.generate(this.builder_)) {
              this.done_ = true;
              return;
            }
          } while (this.builder_.year < dateLocal.year());
          while (!this.monthGenerator_.generate(this.builder_)) {
            if (!this.yearGenerator_.generate(this.builder_)) {
              this.done_ = true;
              return;
            }
          }
        }
        // skip months before date.year/date.month
        while (this.builder_.year == dateLocal.year()
               && this.builder_.month < dateLocal.month()) {
          while (!this.monthGenerator_.generate(this.builder_)) {
            // if there are more years available fetch one
            if (!this.yearGenerator_.generate(this.builder_)) {
              // otherwise the recurrence is exhausted
              this.done_ = true;
              return;
            }
          }
        }
      }

      // consume any remaining instances
      while (!this.done_) {
        DateValue dUtc = this.generateInstance();
        if (null == dUtc) {
          this.done_ = true;
        } else {
          if (!this.condition_.apply(dUtc)) {
            this.done_ = true;
          } else if (dUtc.compareTo(dateUtc) >= 0) {
            this.pendingUtc_ = dUtc;
            break;
          }
        }
      }
View Full Code Here

TOP

Related Classes of com.google.ical.values.DateValue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.