Java 类java.time.jdk8.Jdk7Methods 实例源码

项目:optashift-employee-rostering    文件:LocalTime.java   
/**
 * Compares this {@code LocalTime} to another time.
 * <p>
 * The comparison is based on the time-line position of the local times within a day. It is
 * "consistent with equals", as defined by {@link Comparable}.
 * 
 * @param other the other time to compare to, not null
 * @return the comparator value, negative if less, positive if greater
 * @throws NullPointerException if {@code other} is null
 */
@Override
public int compareTo(LocalTime other) {

  int cmp = Jdk7Methods.Integer_compare(this.hour, other.hour);
  if (cmp == 0) {
    cmp = Jdk7Methods.Integer_compare(this.minute, other.minute);
    if (cmp == 0) {
      cmp = Jdk7Methods.Integer_compare(this.second, other.second);
      if (cmp == 0) {
        cmp = Jdk7Methods.Integer_compare(this.nano, other.nano);
      }
    }
  }
  return cmp;
}
项目:optashift-employee-rostering    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} strictly validating the combination of local date-time,
 * offset and zone ID.
 * <p>
 * This creates a zoned date-time ensuring that the offset is valid for the local date-time according to the
 * rules of the specified zone. If the offset is invalid, an exception is thrown.
 * 
 * @param localDateTime the local date-time, not null
 * @param offset the zone offset, not null
 * @param zone the time-zone, not null
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(offset, "offset");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  if (rules.isValidOffset(localDateTime, offset) == false) {
    // ZoneOffsetTransition trans = rules.getTransition(localDateTime);
    // if (trans != null && trans.isGap()) {
    // // error message says daylight savings for simplicity
    // // even though there are other kinds of gaps
    // throw new DateTimeException("LocalDateTime '" + localDateTime + "' does not exist in zone '" + zone
    // + "' due to a gap in the local time-line, typically caused by daylight savings");
    // }
    // throw new DateTimeException("ZoneOffset '" + offset + "' is not valid for LocalDateTime '" +
    // localDateTime
    // + "' in zone '" + zone + "'");
  }
  return new ZonedDateTime(localDateTime, offset, zone);
}
项目:java8-backports    文件:DateTimeFormatterBuilder.java   
/**
 * Appends the value of a date-time field to the formatter providing full control over printing.
 * <p>
 * The value of the field will be output during a print. If the value cannot be obtained then an exception
 * will be thrown.
 * <p>
 * This method provides full control of the numeric formatting, including zero-padding and the
 * positive/negative sign.
 * <p>
 * The parser for a variable width value normally behaves greedily, accepting as many digits as possible.
 * This behavior can be affected by 'adjacent value parsing'. See {@link #appendValue(DateTimeField, int)}
 * for full details.
 * 
 * @param field the field to append, not null
 * @param minWidth the minimum field width of the printed field, from 1 to 19
 * @param maxWidth the maximum field width of the printed field, from 1 to 19
 * @param signStyle the positive/negative output style, not null
 * @return this, for chaining, not null
 * @throws IllegalArgumentException if the widths are invalid
 */
public DateTimeFormatterBuilder appendValue(DateTimeField field, int minWidth, int maxWidth, SignStyle signStyle) {

  if (minWidth == maxWidth && signStyle == SignStyle.NOT_NEGATIVE) {
    return appendValue(field, maxWidth);
  }
  Jdk7Methods.Objects_requireNonNull(field, "field");
  Jdk7Methods.Objects_requireNonNull(signStyle, "signStyle");
  if (minWidth < 1 || minWidth > 19) {
    throw new IllegalArgumentException("The minimum width must be from 1 to 19 inclusive but was " + minWidth);
  }
  if (maxWidth < 1 || maxWidth > 19) {
    throw new IllegalArgumentException("The maximum width must be from 1 to 19 inclusive but was " + maxWidth);
  }
  if (maxWidth < minWidth) {
    throw new IllegalArgumentException("The maximum width must exceed or equal the minimum width but " + maxWidth
        + " < " + minWidth);
  }
  NumberPrinterParser pp = new NumberPrinterParser(field, minWidth, maxWidth, signStyle);
  if (minWidth == maxWidth) {
    appendInternal(pp);
  } else {
    this.active.valueParserIndex = appendInternal(pp);
  }
  return this;
}
项目:gwt-time    文件:LocalTime.java   
/**
 * Compares this {@code LocalTime} to another time.
 * <p>
 * The comparison is based on the time-line position of the local times within a day. It is
 * "consistent with equals", as defined by {@link Comparable}.
 * 
 * @param other the other time to compare to, not null
 * @return the comparator value, negative if less, positive if greater
 * @throws NullPointerException if {@code other} is null
 */
@Override
public int compareTo(LocalTime other) {

  int cmp = Jdk7Methods.Integer_compare(this.hour, other.hour);
  if (cmp == 0) {
    cmp = Jdk7Methods.Integer_compare(this.minute, other.minute);
    if (cmp == 0) {
      cmp = Jdk7Methods.Integer_compare(this.second, other.second);
      if (cmp == 0) {
        cmp = Jdk7Methods.Integer_compare(this.nano, other.nano);
      }
    }
  }
  return cmp;
}
项目:gwt-time    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} strictly validating the combination of local date-time,
 * offset and zone ID.
 * <p>
 * This creates a zoned date-time ensuring that the offset is valid for the local date-time according to the
 * rules of the specified zone. If the offset is invalid, an exception is thrown.
 * 
 * @param localDateTime the local date-time, not null
 * @param offset the zone offset, not null
 * @param zone the time-zone, not null
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(offset, "offset");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  if (rules.isValidOffset(localDateTime, offset) == false) {
    // ZoneOffsetTransition trans = rules.getTransition(localDateTime);
    // if (trans != null && trans.isGap()) {
    // // error message says daylight savings for simplicity
    // // even though there are other kinds of gaps
    // throw new DateTimeException("LocalDateTime '" + localDateTime + "' does not exist in zone '" + zone
    // + "' due to a gap in the local time-line, typically caused by daylight savings");
    // }
    // throw new DateTimeException("ZoneOffset '" + offset + "' is not valid for LocalDateTime '" +
    // localDateTime
    // + "' in zone '" + zone + "'");
  }
  return new ZonedDateTime(localDateTime, offset, zone);
}
项目:java8-backports    文件:DateTimeFormatter.java   
/**
 * Parses the text to a builder.
 * <p>
 * This parses to a {@code DateTimeBuilder} ensuring that the text is fully parsed. This method throws
 * {@link DateTimeParseException} if unable to parse, or some other {@code DateTimeException} if another
 * date/time problem occurs.
 * 
 * @param text the text to parse, not null
 * @return the engine representing the result of the parse, not null
 * @throws DateTimeParseException if the parse fails
 * @throws DateTimeException if there is a date/time problem
 */
public DateTimeBuilder parseToBuilder(CharSequence text) {

  Jdk7Methods.Objects_requireNonNull(text, "text");
  String str = text.toString(); // parsing whole String, so this makes sense
  ParsePosition pos = new ParsePosition(0);
  DateTimeBuilder result = parseToBuilder(str, pos);
  if (result == null || pos.getErrorIndex() >= 0 || pos.getIndex() < str.length()) {
    String abbr = str.toString();
    if (abbr.length() > 64) {
      abbr = abbr.substring(0, 64) + "...";
    }
    if (pos.getErrorIndex() >= 0) {
      throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " + pos.getErrorIndex(),
          str, pos.getErrorIndex());
    } else {
      throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index "
          + pos.getIndex(), str, pos.getIndex());
    }
  }
  return result;
}
项目:java8-backports    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} strictly validating the combination of local date-time,
 * offset and zone ID.
 * <p>
 * This creates a zoned date-time ensuring that the offset is valid for the local date-time according to the
 * rules of the specified zone. If the offset is invalid, an exception is thrown.
 * 
 * @param localDateTime the local date-time, not null
 * @param offset the zone offset, not null
 * @param zone the time-zone, not null
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(offset, "offset");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  if (rules.isValidOffset(localDateTime, offset) == false) {
    ZoneOffsetTransition trans = rules.getTransition(localDateTime);
    if (trans != null && trans.isGap()) {
      // error message says daylight savings for simplicity
      // even though there are other kinds of gaps
      throw new DateTimeException("LocalDateTime '" + localDateTime + "' does not exist in zone '" + zone
          + "' due to a gap in the local time-line, typically caused by daylight savings");
    }
    throw new DateTimeException("ZoneOffset '" + offset + "' is not valid for LocalDateTime '" + localDateTime
        + "' in zone '" + zone + "'");
  }
  return new ZonedDateTime(localDateTime, offset, zone);
}
项目:java8-backports    文件:DateTimeFormatterBuilder.java   
/**
 * Constructor.
 * 
 * @param field the field to output, not null
 * @param minWidth the minimum width to output, from 0 to 9
 * @param maxWidth the maximum width to output, from 0 to 9
 * @param decimalPoint whether to output the localized decimal point symbol
 */
public FractionPrinterParser(DateTimeField field, int minWidth, int maxWidth, boolean decimalPoint) {

  Jdk7Methods.Objects_requireNonNull(field, "field");
  if (field.range().isFixed() == false) {
    throw new IllegalArgumentException("Field must have a fixed set of values: " + field.getName());
  }
  if (minWidth < 0 || minWidth > 9) {
    throw new IllegalArgumentException("Minimum width must be from 0 to 9 inclusive but was " + minWidth);
  }
  if (maxWidth < 1 || maxWidth > 9) {
    throw new IllegalArgumentException("Maximum width must be from 1 to 9 inclusive but was " + maxWidth);
  }
  if (maxWidth < minWidth) {
    throw new IllegalArgumentException("Maximum width must exceed or equal the minimum width but " + maxWidth
        + " < " + minWidth);
  }
  this.field = field;
  this.minWidth = minWidth;
  this.maxWidth = maxWidth;
  this.decimalPoint = decimalPoint;
}
项目:java8-backports    文件:Chrono.java   
/**
 * Obtains an instance of {@code Chrono} from a locale.
 * <p>
 * The locale can be used to identify a calendar. This uses {@link Locale#getUnicodeLocaleType(String)} to
 * obtain the "ca" key to identify the calendar system.
 * <p>
 * If the locale does not contain calendar system information, the standard ISO calendar system is used.
 * 
 * @param locale the locale to use to obtain the calendar system, not null
 * @return the calendar system associated with the locale, not null
 * @throws DateTimeException if the locale-specified calendar cannot be found
 */
public static Chrono<?> ofLocale(Locale locale) {

  Jdk7Methods.Objects_requireNonNull(locale, "locale");
  String type = locale.getUnicodeLocaleType("ca");
  if (type == null) {
    return ISOChrono.INSTANCE;
  } else if ("iso".equals(type) || "iso8601".equals(type)) {
    return ISOChrono.INSTANCE;
  } else {
    Chrono<?> chrono = CHRONOS_BY_TYPE.get(type);
    if (chrono == null) {
      throw new DateTimeException("Unknown calendar system: " + type);
    }
    return chrono;
  }
}
项目:optashift-employee-rostering    文件:OffsetTime.java   
/**
 * Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset time with the same instant as that specified. Finding the offset from
 * UTC/Greenwich is simple as there is only one valid offset for each instant.
 * <p>
 * The date component of the instant is dropped during the conversion. This means that the conversion can
 * never fail due to the instant being out of the valid range of dates.
 * 
 * @param instant the instant to create the time from, not null
 * @param zone the time-zone, which may be an offset, not null
 * @return the offset time, not null
 */
public static OffsetTime ofInstant(Instant instant, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(instant, "instant");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  long secsOfDay = instant.getEpochSecond() % SECONDS_PER_DAY;
  secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
  if (secsOfDay < 0) {
    secsOfDay += SECONDS_PER_DAY;
  }
  LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, instant.getNano());
  return new OffsetTime(time, offset);
}
项目:optashift-employee-rostering    文件:Period.java   
/**
 * Returns a copy of this period with the specified period added.
 * <p>
 * The specified unit must be one of the supported units from {@link ChronoUnit}, {@code YEARS},
 * {@code MONTHS} or {@code DAYS} or be a time unit with an {@link PeriodUnit#isDurationEstimated() exact
 * duration}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 * 
 * @param amount the amount to add, positive or negative
 * @param unit the unit that the amount is expressed in, not null
 * @return a {@code Period} based on this period with the requested amount added, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
public Period plus(long amount, PeriodUnit unit) {

  Jdk7Methods.Objects_requireNonNull(unit, "unit");
  if (unit instanceof ChronoUnit) {
    if (unit == YEARS || unit == MONTHS || unit == DAYS || unit.isDurationEstimated() == false) {
      if (amount == 0) {
        return this;
      }
      switch ((ChronoUnit) unit) {
        case NANOS:
          return plusNanos(amount);
        case MICROS:
          return plusNanos(Jdk8Methods.safeMultiply(amount, 1000L));
        case MILLIS:
          return plusNanos(Jdk8Methods.safeMultiply(amount, 1000000L));
        case SECONDS:
          return plusSeconds(amount);
        case MINUTES:
          return plusMinutes(amount);
        case HOURS:
          return plusHours(amount);
        case HALF_DAYS:
          return plusNanos(Jdk8Methods.safeMultiply(amount, 12 * NANOS_PER_HOUR));
        case DAYS:
          return plusDays(amount);
        case MONTHS:
          return plusMonths(amount);
        case YEARS:
          return plusYears(amount);
        default :
          throw new DateTimeException("Unsupported unit: " + unit.getName());
      }
    }
  }
  if (unit.isDurationEstimated()) {
    throw new DateTimeException("Unsupported unit: " + unit.getName());
  }
  return plusNanos(Duration.of(amount, unit).toNanos());
}
项目:optashift-employee-rostering    文件:Duration.java   
/**
 * Returns a copy of this duration with the specified duration added.
 * <p>
 * The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by
 * this method. The unit must either have an {@link PeriodUnit#isDurationEstimated() exact duration} or be
 * {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 * 
 * @param amountToAdd the amount of the period, measured in terms of the unit, positive or negative
 * @param unit the unit that the period is measured in, must have an exact duration, not null
 * @return a {@code Duration} based on this duration with the specified duration added, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
public Duration plus(long amountToAdd, PeriodUnit unit) {

  Jdk7Methods.Objects_requireNonNull(unit, "unit");
  if (unit == DAYS) {
    return plus(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY), 0);
  }
  if (unit.isDurationEstimated()) {
    throw new DateTimeException("Unit must not have an estimated duration");
  }
  if (amountToAdd == 0) {
    return this;
  }
  if (unit instanceof ChronoUnit) {
    switch ((ChronoUnit) unit) {
      case NANOS:
        return plusNanos(amountToAdd);
      case MICROS:
        return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000).plusNanos(
            (amountToAdd % (1000000L * 1000)) * 1000);
      case MILLIS:
        return plusMillis(amountToAdd);
      case SECONDS:
        return plusSeconds(amountToAdd);
    }
    return plusSeconds(Jdk8Methods.safeMultiply(unit.getDuration().seconds, amountToAdd));
  }
  Duration duration = unit.getDuration().multipliedBy(amountToAdd);
  return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
}
项目:optashift-employee-rostering    文件:Duration.java   
/**
 * Compares this duration to the specified {@code Duration}.
 * <p>
 * The comparison is based on the total length of the durations. It is "consistent with equals", as defined
 * by {@link Comparable}.
 * 
 * @param otherDuration the other duration to compare to, not null
 * @return the comparator value, negative if less, positive if greater
 */
@Override
public int compareTo(Duration otherDuration) {

  int cmp = Jdk7Methods.Long_compare(this.seconds, otherDuration.seconds);
  if (cmp != 0) {
    return cmp;
  }
  return this.nanos - otherDuration.nanos;
}
项目:optashift-employee-rostering    文件:ZonedDateTime.java   
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time using the preferred offset if
 * possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line. This is achieved by finding a valid
 * offset from UTC/Greenwich for the local date-time as defined by the {@link ZoneRules rules} of the zone
 * ID.
 * <p>
 * In most cases, there is only one valid offset for a local date-time. In the case of an overlap, where
 * clocks are set back, there are two valid offsets. If the preferred offset is one of the valid offsets
 * then it is used. Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset. Instead, the local date-time
 * is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the
 * local date-time will be moved one hour later into the offset typically corresponding to "summer".
 * 
 * @param localDateTime the local date-time, not null
 * @param zone the time-zone, not null
 * @param preferredOffset the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  if (zone instanceof ZoneOffset) {
    return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
  }
  ZoneRules rules = zone.getRules();
  List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
  ZoneOffset offset;
  if (validOffsets.size() == 1) {
    offset = validOffsets.get(0);
  } else if (validOffsets.size() == 0) {
    // ZoneOffsetTransition trans = rules.getTransition(localDateTime);
    // localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
    offset = rules.getOffset(localDateTime); // trans.getOffsetAfter();
  } else {
    if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
      offset = preferredOffset;
    } else {
      offset = Jdk7Methods.Objects_requireNonNull(validOffsets.get(0), "offset"); // protect against bad
                                                                                  // ZoneRules
    }
  }
  return new ZonedDateTime(localDateTime, offset, zone);
}
项目:optashift-employee-rostering    文件:WeekDefinition.java   
/**
 * Obtains an instance of {@code WeekDefinition} appropriate for a locale.
 * <p>
 * This will look up appropriate values from the provider of localization data.
 * 
 * @param locale the locale to use, not null
 * @return the week-definition, not null
 */
public static WeekDefinition of(Locale locale) {

  Jdk7Methods.Objects_requireNonNull(locale, "locale");
  DayOfWeek dow = DayOfWeek.MONDAY;
  if ("US".equals(locale.getCountry())) {
    dow = DayOfWeek.SUNDAY;
  }
  int minDays = 4; // ISO-08601 // gcal.getMinimalDaysInFirstWeek();
  return WeekDefinition.of(dow, minDays);
}
项目:optashift-employee-rostering    文件:WeekDefinition.java   
/**
 * Creates an instance of the definition.
 * 
 * @param firstDayOfWeek the first day of the week, not null
 * @param minimalDaysInFirstWeek the minimal number of days in the first week, from 1 to 7
 * @throws IllegalArgumentException if the minimal days value is invalid
 */
private WeekDefinition(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {

  Jdk7Methods.Objects_requireNonNull(firstDayOfWeek, "firstDayOfWeek");
  if (minimalDaysInFirstWeek < 1 || minimalDaysInFirstWeek > 7) {
    throw new IllegalArgumentException("Minimal number of days is invalid");
  }
  this.firstDayOfWeek = firstDayOfWeek;
  this.minimalDays = minimalDaysInFirstWeek;
}
项目:optashift-employee-rostering    文件:DateTimeBuilder.java   
/**
 * Gets the value of the specified field from the builder.
 * 
 * @param field the field to query in the field-value map, not null
 * @return the value of the field, may be out of range
 * @throws DateTimeException if the field is not present
 */
public long getFieldValue(DateTimeField field) {

  Jdk7Methods.Objects_requireNonNull(field, "field");
  Long value = getFieldValue0(field);
  if (value == null) {
    throw new DateTimeException("Field not found: " + field);
  }
  return value;
}
项目:optashift-employee-rostering    文件:DateTimeBuilder.java   
/**
 * Removes a field-value pair from the builder.
 * <p>
 * This removes a field, which must exist, from the builder. See
 * {@link #removeFieldValues(DateTimeField...)} for a version which does not throw an exception
 * 
 * @param field the field to remove, not null
 * @return the previous value of the field
 * @throws DateTimeException if the field is not found
 */
public long removeFieldValue(DateTimeField field) {

  Jdk7Methods.Objects_requireNonNull(field, "field");
  Long value = null;
  if (field instanceof ChronoField) {
    value = this.standardFields.remove(field);
  } else if (this.otherFields != null) {
    value = this.otherFields.remove(field);
  }
  if (value == null) {
    throw new DateTimeException("Field not found: " + field);
  }
  return value;
}
项目:optashift-employee-rostering    文件:DateTimeBuilder.java   
/**
 * Adds a date-time object to the builder.
 * <p>
 * This adds a date-time object to the builder. If the object is a {@code DateTimeBuilder}, each field is
 * added using {@link #addFieldValue}. If the object is not already present, then the object is added. If
 * the object is already present and it is equal to that specified, no action occurs. If the object is
 * already present and it is not equal to that specified, then an exception is thrown.
 * 
 * @param object the object to add, not null
 * @return {@code this}, for method chaining
 * @throws DateTimeException if the field is already present with a different value
 */
public DateTimeBuilder addCalendrical(Object object) {

  Jdk7Methods.Objects_requireNonNull(object, "object");
  // special case
  if (object instanceof DateTimeBuilder) {
    DateTimeBuilder dtb = (DateTimeBuilder) object;
    for (DateTimeField field : dtb.getFieldValueMap().keySet()) {
      addFieldValue(field, dtb.getFieldValue(field));
    }
    return this;
  }
  if (object instanceof Instant) {
    addFieldValue(INSTANT_SECONDS, ((Instant) object).getEpochSecond());
    addFieldValue(NANO_OF_SECOND, ((Instant) object).getNano());
  } else {
    this.objects.add(object);
  }
  // TODO
  // // preserve state of builder until validated
  // Class<?> cls = dateTime.extract(Class.class);
  // if (cls == null) {
  // throw new DateTimeException("Invalid dateTime, unable to extract Class");
  // }
  // Object obj = objects.get(cls);
  // if (obj != null) {
  // if (obj.equals(dateTime) == false) {
  // throw new DateTimeException("Conflict found: " + dateTime.getClass().getSimpleName() + " " + obj +
  // " differs from " + dateTime + ": " + this);
  // }
  // } else {
  // objects.put(cls, dateTime);
  // }
  return this;
}
项目:optashift-employee-rostering    文件:ChronoDateTimeImpl.java   
/**
 * Constructor.
 * 
 * @param date the date part of the date-time, not null
 * @param time the time part of the date-time, not null
 */
private ChronoDateTimeImpl(ChronoLocalDate<C> date, LocalTime time) {

  Jdk7Methods.Objects_requireNonNull(date, "date");
  Jdk7Methods.Objects_requireNonNull(time, "time");
  this.date = date;
  this.time = time;
}
项目:optashift-employee-rostering    文件:ChronoZonedDateTimeImpl.java   
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time using the preferred offset if
 * possible.
 * 
 * @param localDateTime the local date-time, not null
 * @param zoneId the zone identifier, not null
 * @param preferredOffset the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends Chrono<R>> ChronoZonedDateTime<R> ofBest(ChronoDateTimeImpl<R> localDateTime, ZoneId zoneId,
    ZoneOffset preferredOffset) {

  Jdk7Methods.Objects_requireNonNull(localDateTime, "localDateTime");
  Jdk7Methods.Objects_requireNonNull(zoneId, "zoneId");
  if (zoneId instanceof ZoneOffset) {
    return new ChronoZonedDateTimeImpl<R>(localDateTime, (ZoneOffset) zoneId, zoneId);
  }
  ZoneRules rules = zoneId.getRules();
  LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
  List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
  ZoneOffset offset;
  if (validOffsets.size() == 1) {
    offset = validOffsets.get(0);
  } else if (validOffsets.size() == 0) {
    // TODO what to do with chrono support in GWT?
    // ZoneOffsetTransition trans = rules.getTransition(isoLDT);
    // localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
    // offset = trans.getOffsetAfter();
    offset = rules.getOffset(isoLDT);
  } else {
    if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
      offset = preferredOffset;
    } else {
      offset = validOffsets.get(0);
    }
  }
  Jdk7Methods.Objects_requireNonNull(offset, "offset"); // protect against bad ZoneRules
  return new ChronoZonedDateTimeImpl<R>(localDateTime, offset, zoneId);
}
项目:optashift-employee-rostering    文件:ChronoZonedDateTime.java   
@Override
public int compare(ChronoZonedDateTime<?> datetime1, ChronoZonedDateTime<?> datetime2) {

  int cmp = Jdk7Methods.Long_compare(datetime1.toEpochSecond(), datetime2.toEpochSecond());
  if (cmp == 0) {
    cmp = Jdk7Methods.Long_compare(datetime1.getTime().toNanoOfDay(), datetime2.getTime().toNanoOfDay());
  }
  return cmp;
}
项目:optashift-employee-rostering    文件:ChronoLocalDateTime.java   
@Override
public int compare(ChronoLocalDateTime<?> datetime1, ChronoLocalDateTime<?> datetime2) {

  int cmp = Jdk7Methods.Long_compare(datetime1.getDate().toEpochDay(), datetime2.getDate().toEpochDay());
  if (cmp == 0) {
    cmp = Jdk7Methods.Long_compare(datetime1.getTime().toNanoOfDay(), datetime2.getTime().toNanoOfDay());
  }
  return cmp;
}
项目:optashift-employee-rostering    文件:OffsetDate.java   
/**
 * Obtains an instance of {@code OffsetDate} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date with the same instant as midnight at the start of day of the instant
 * specified. Finding the offset from UTC/Greenwich is simple as there is only one valid offset for each
 * instant.
 * 
 * @param instant the instant to create the time from, not null
 * @param zone the time-zone, which may be an offset, not null
 * @return the offset time, not null
 */
public static OffsetDate ofInstant(Instant instant, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(instant, "instant");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  long epochSec = instant.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later
  long epochDay = Jdk8Methods.floorDiv(epochSec, SECONDS_PER_DAY);
  LocalDate date = LocalDate.ofEpochDay(epochDay);
  return new OffsetDate(date, offset);
}
项目:gwt-time    文件:OffsetTime.java   
/**
 * Obtains an instance of {@code OffsetTime} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset time with the same instant as that specified. Finding the offset from
 * UTC/Greenwich is simple as there is only one valid offset for each instant.
 * <p>
 * The date component of the instant is dropped during the conversion. This means that the conversion can
 * never fail due to the instant being out of the valid range of dates.
 * 
 * @param instant the instant to create the time from, not null
 * @param zone the time-zone, which may be an offset, not null
 * @return the offset time, not null
 */
public static OffsetTime ofInstant(Instant instant, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(instant, "instant");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  long secsOfDay = instant.getEpochSecond() % SECONDS_PER_DAY;
  secsOfDay = (secsOfDay + offset.getTotalSeconds()) % SECONDS_PER_DAY;
  if (secsOfDay < 0) {
    secsOfDay += SECONDS_PER_DAY;
  }
  LocalTime time = LocalTime.ofSecondOfDay(secsOfDay, instant.getNano());
  return new OffsetTime(time, offset);
}
项目:java8-backports    文件:TzdbZoneRulesProvider.java   
@Override
protected ZoneRules provideRules(String zoneId) {

  Jdk7Methods.Objects_requireNonNull(zoneId, "zoneId");
  ZoneRules rules = this.versions.lastEntry().getValue().getRules(zoneId);
  if (rules == null) {
    throw new ZoneRulesException("Unknown time-zone ID: " + zoneId);
  }
  return rules;
}
项目:java8-backports    文件:OffsetDate.java   
/**
 * Obtains an instance of {@code OffsetDate} from an {@code Instant} and zone ID.
 * <p>
 * This creates an offset date with the same instant as midnight at the start of day of the instant
 * specified. Finding the offset from UTC/Greenwich is simple as there is only one valid offset for each
 * instant.
 * 
 * @param instant the instant to create the time from, not null
 * @param zone the time-zone, which may be an offset, not null
 * @return the offset time, not null
 */
public static OffsetDate ofInstant(Instant instant, ZoneId zone) {

  Jdk7Methods.Objects_requireNonNull(instant, "instant");
  Jdk7Methods.Objects_requireNonNull(zone, "zone");
  ZoneRules rules = zone.getRules();
  ZoneOffset offset = rules.getOffset(instant);
  long epochSec = instant.getEpochSecond() + offset.getTotalSeconds(); // overflow caught later
  long epochDay = Jdk8Methods.floorDiv(epochSec, SECONDS_PER_DAY);
  LocalDate date = LocalDate.ofEpochDay(epochDay);
  return new OffsetDate(date, offset);
}
项目:gwt-time    文件:Duration.java   
/**
 * Returns a copy of this duration with the specified duration added.
 * <p>
 * The duration amount is measured in terms of the specified unit. Only a subset of units are accepted by
 * this method. The unit must either have an {@link PeriodUnit#isDurationEstimated() exact duration} or be
 * {@link ChronoUnit#DAYS} which is treated as 24 hours. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 * 
 * @param amountToAdd the amount of the period, measured in terms of the unit, positive or negative
 * @param unit the unit that the period is measured in, must have an exact duration, not null
 * @return a {@code Duration} based on this duration with the specified duration added, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
public Duration plus(long amountToAdd, PeriodUnit unit) {

  Jdk7Methods.Objects_requireNonNull(unit, "unit");
  if (unit == DAYS) {
    return plus(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY), 0);
  }
  if (unit.isDurationEstimated()) {
    throw new DateTimeException("Unit must not have an estimated duration");
  }
  if (amountToAdd == 0) {
    return this;
  }
  if (unit instanceof ChronoUnit) {
    switch ((ChronoUnit) unit) {
      case NANOS:
        return plusNanos(amountToAdd);
      case MICROS:
        return plusSeconds((amountToAdd / (1000000L * 1000)) * 1000).plusNanos(
            (amountToAdd % (1000000L * 1000)) * 1000);
      case MILLIS:
        return plusMillis(amountToAdd);
      case SECONDS:
        return plusSeconds(amountToAdd);
    }
    return plusSeconds(Jdk8Methods.safeMultiply(unit.getDuration().seconds, amountToAdd));
  }
  Duration duration = unit.getDuration().multipliedBy(amountToAdd);
  return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
}
项目:gwt-time    文件:Duration.java   
/**
 * Compares this duration to the specified {@code Duration}.
 * <p>
 * The comparison is based on the total length of the durations. It is "consistent with equals", as defined
 * by {@link Comparable}.
 * 
 * @param otherDuration the other duration to compare to, not null
 * @return the comparator value, negative if less, positive if greater
 */
@Override
public int compareTo(Duration otherDuration) {

  int cmp = Jdk7Methods.Long_compare(this.seconds, otherDuration.seconds);
  if (cmp != 0) {
    return cmp;
  }
  return this.nanos - otherDuration.nanos;
}
项目:gwt-time    文件:WeekDefinition.java   
/**
 * Obtains an instance of {@code WeekDefinition} appropriate for a locale.
 * <p>
 * This will look up appropriate values from the provider of localization data.
 * 
 * @param locale the locale to use, not null
 * @return the week-definition, not null
 */
public static WeekDefinition of(Locale locale) {

  Jdk7Methods.Objects_requireNonNull(locale, "locale");
  DayOfWeek dow = DayOfWeek.MONDAY;
  if ("US".equals(locale.getCountry())) {
    dow = DayOfWeek.SUNDAY;
  }
  int minDays = 4; // ISO-08601 // gcal.getMinimalDaysInFirstWeek();
  return WeekDefinition.of(dow, minDays);
}
项目:java8-backports    文件:WeekDefinition.java   
/**
 * Creates an instance of the definition.
 * 
 * @param firstDayOfWeek the first day of the week, not null
 * @param minimalDaysInFirstWeek the minimal number of days in the first week, from 1 to 7
 * @throws IllegalArgumentException if the minimal days value is invalid
 */
private WeekDefinition(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) {

  Jdk7Methods.Objects_requireNonNull(firstDayOfWeek, "firstDayOfWeek");
  if (minimalDaysInFirstWeek < 1 || minimalDaysInFirstWeek > 7) {
    throw new IllegalArgumentException("Minimal number of days is invalid");
  }
  this.firstDayOfWeek = firstDayOfWeek;
  this.minimalDays = minimalDaysInFirstWeek;
}
项目:gwt-time    文件:DateTimeBuilder.java   
/**
 * Removes a field-value pair from the builder.
 * <p>
 * This removes a field, which must exist, from the builder. See
 * {@link #removeFieldValues(DateTimeField...)} for a version which does not throw an exception
 * 
 * @param field the field to remove, not null
 * @return the previous value of the field
 * @throws DateTimeException if the field is not found
 */
public long removeFieldValue(DateTimeField field) {

  Jdk7Methods.Objects_requireNonNull(field, "field");
  Long value = null;
  if (field instanceof ChronoField) {
    value = this.standardFields.remove(field);
  } else if (this.otherFields != null) {
    value = this.otherFields.remove(field);
  }
  if (value == null) {
    throw new DateTimeException("Field not found: " + field);
  }
  return value;
}
项目:java8-backports    文件:DateTimeFormatterBuilder.java   
/**
 * Appends a string literal to the formatter.
 * <p>
 * This string will be output during a print.
 * <p>
 * If the literal is empty, nothing is added to the formatter.
 * 
 * @param literal the literal to append, not null
 * @return this, for chaining, not null
 */
public DateTimeFormatterBuilder appendLiteral(String literal) {

  Jdk7Methods.Objects_requireNonNull(literal, "literal");
  if (literal.length() > 0) {
    if (literal.length() == 1) {
      appendInternal(new CharLiteralPrinterParser(literal.charAt(0)));
    } else {
      appendInternal(new StringLiteralPrinterParser(literal));
    }
  }
  return this;
}
项目:java8-backports    文件:DateTimeBuilder.java   
/**
 * Adds a date-time object to the builder.
 * <p>
 * This adds a date-time object to the builder. If the object is a {@code DateTimeBuilder}, each field is
 * added using {@link #addFieldValue}. If the object is not already present, then the object is added. If
 * the object is already present and it is equal to that specified, no action occurs. If the object is
 * already present and it is not equal to that specified, then an exception is thrown.
 * 
 * @param object the object to add, not null
 * @return {@code this}, for method chaining
 * @throws DateTimeException if the field is already present with a different value
 */
public DateTimeBuilder addCalendrical(Object object) {

  Jdk7Methods.Objects_requireNonNull(object, "object");
  // special case
  if (object instanceof DateTimeBuilder) {
    DateTimeBuilder dtb = (DateTimeBuilder) object;
    for (DateTimeField field : dtb.getFieldValueMap().keySet()) {
      addFieldValue(field, dtb.getFieldValue(field));
    }
    return this;
  }
  if (object instanceof Instant) {
    addFieldValue(INSTANT_SECONDS, ((Instant) object).getEpochSecond());
    addFieldValue(NANO_OF_SECOND, ((Instant) object).getNano());
  } else {
    this.objects.add(object);
  }
  // TODO
  // // preserve state of builder until validated
  // Class<?> cls = dateTime.extract(Class.class);
  // if (cls == null) {
  // throw new DateTimeException("Invalid dateTime, unable to extract Class");
  // }
  // Object obj = objects.get(cls);
  // if (obj != null) {
  // if (obj.equals(dateTime) == false) {
  // throw new DateTimeException("Conflict found: " + dateTime.getClass().getSimpleName() + " " + obj +
  // " differs from " + dateTime + ": " + this);
  // }
  // } else {
  // objects.put(cls, dateTime);
  // }
  return this;
}
项目:gwt-time    文件:ChronoLocalDateTime.java   
@Override
public int compare(ChronoLocalDateTime<?> datetime1, ChronoLocalDateTime<?> datetime2) {

  int cmp = Jdk7Methods.Long_compare(datetime1.getDate().toEpochDay(), datetime2.getDate().toEpochDay());
  if (cmp == 0) {
    cmp = Jdk7Methods.Long_compare(datetime1.getTime().toNanoOfDay(), datetime2.getTime().toNanoOfDay());
  }
  return cmp;
}
项目:java8-backports    文件:DateTimeFormatterBuilder.java   
/**
 * Appends a printer and/or parser to the internal list handling padding.
 * 
 * @param pp the printer-parser to add, not null
 * @return the index into the active parsers list
 */
private int appendInternal(DateTimePrinterParser pp) {

  Jdk7Methods.Objects_requireNonNull(pp, "pp");
  if (this.active.padNextWidth > 0) {
    if (pp != null) {
      pp = new PadPrinterParserDecorator(pp, this.active.padNextWidth, this.active.padNextChar);
    }
    this.active.padNextWidth = 0;
    this.active.padNextChar = 0;
  }
  this.active.printerParsers.add(pp);
  this.active.valueParserIndex = -1;
  return this.active.printerParsers.size() - 1;
}
项目:java8-backports    文件:JapaneseDate.java   
/**
 * Obtains an instance of {@code JapaneseDate} from the era, year-of-era, month-of-year and day-of-month.
 * 
 * @param era the era to represent, not null
 * @param year the year-of-era to represent
 * @param month the month-of-year to represent
 * @param dayOfMonth the day-of-month to represent, from 1 to 31
 * @return the Japanese date, never null
 * @throws DateTimeException if the value of any field is out of range, or if the day-of-month is invalid
 *         for the month-year
 */
static JapaneseDate of(JapaneseEra era, int yearOfEra, int month, int dayOfMonth) {

  Jdk7Methods.Objects_requireNonNull(era, "era");
  LocalGregorianCalendar.Date jdate = JapaneseChrono.JCAL.newCalendarDate(null);
  jdate.setEra(era.getPrivateEra()).setDate(yearOfEra, month, dayOfMonth);
  if (!JapaneseChrono.JCAL.validate(jdate)) {
    throw new IllegalArgumentException();
  }
  LocalDate date = LocalDate.of(jdate.getNormalizedYear(), month, dayOfMonth);
  return new JapaneseDate(era, yearOfEra, date);
}
项目:java8-backports    文件:DateTimeFormatter.java   
/**
 * Returns a copy of this formatter with a new locale.
 * <p>
 * This instance is immutable and unaffected by this method call.
 * 
 * @param locale the new locale, not null
 * @return a {@code DateTimeFormatter} based on this one with the requested locale, not null
 */
public DateTimeFormatter withLocale(Locale locale) {

  Jdk7Methods.Objects_requireNonNull(locale, "locale");
  if (locale.equals(this.locale)) {
    return this;
  }
  return new DateTimeFormatter(this.printerParser, locale, this.symbols);
}
项目:java8-backports    文件:ZoneRulesProvider.java   
/**
 * Registers the provider.
 * 
 * @param provider the provider to register, not null
 * @throws ZoneRulesException if unable to complete the registration
 */
private static void registerProvider0(ZoneRulesProvider provider) {

  for (String zoneId : provider.provideZoneIds()) {
    Jdk7Methods.Objects_requireNonNull(zoneId, "zoneId");
    ZoneRulesProvider old = ZONES.putIfAbsent(zoneId, provider.provideBind(zoneId));
    if (old != null) {
      throw new ZoneRulesException("Unable to register zone as one already registered with that ID: " + zoneId
          + ", currently loading from provider: " + provider);
    }
  }
}
项目:java8-backports    文件:Period.java   
/**
 * Returns a copy of this period with the specified period added.
 * <p>
 * The specified unit must be one of the supported units from {@link ChronoUnit}, {@code YEARS},
 * {@code MONTHS} or {@code DAYS} or be a time unit with an {@link PeriodUnit#isDurationEstimated() exact
 * duration}. Other units throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 * 
 * @param amount the amount to add, positive or negative
 * @param unit the unit that the amount is expressed in, not null
 * @return a {@code Period} based on this period with the requested amount added, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
public Period plus(long amount, PeriodUnit unit) {

  Jdk7Methods.Objects_requireNonNull(unit, "unit");
  if (unit instanceof ChronoUnit) {
    if (unit == YEARS || unit == MONTHS || unit == DAYS || unit.isDurationEstimated() == false) {
      if (amount == 0) {
        return this;
      }
      switch ((ChronoUnit) unit) {
        case NANOS:
          return plusNanos(amount);
        case MICROS:
          return plusNanos(Jdk8Methods.safeMultiply(amount, 1000L));
        case MILLIS:
          return plusNanos(Jdk8Methods.safeMultiply(amount, 1000000L));
        case SECONDS:
          return plusSeconds(amount);
        case MINUTES:
          return plusMinutes(amount);
        case HOURS:
          return plusHours(amount);
        case HALF_DAYS:
          return plusNanos(Jdk8Methods.safeMultiply(amount, 12 * NANOS_PER_HOUR));
        case DAYS:
          return plusDays(amount);
        case MONTHS:
          return plusMonths(amount);
        case YEARS:
          return plusYears(amount);
        default :
          throw new DateTimeException("Unsupported unit: " + unit.getName());
      }
    }
  }
  if (unit.isDurationEstimated()) {
    throw new DateTimeException("Unsupported unit: " + unit.getName());
  }
  return plusNanos(Duration.of(amount, unit).toNanos());
}