Esempio n. 1
0
  /**
   * Constructs a Partial with the specified fields and values. The fields must be specified in the
   * order largest to smallest. For year and weekyear fields with equal duration, year is defined as
   * being larger than weekyear.
   *
   * <p>The constructor uses the specified chronology.
   *
   * @param types the types to create the partial from, not null
   * @param values the values to store, not null
   * @param chronology the chronology, null means ISO
   * @throws IllegalArgumentException if the types or values are invalid
   */
  public Partial(DateTimeFieldType[] types, int[] values, Chronology chronology) {
    super();
    chronology = DateTimeUtils.getChronology(chronology).withUTC();
    iChronology = chronology;
    if (types == null) {
      throw new IllegalArgumentException("Types array must not be null");
    }
    if (values == null) {
      throw new IllegalArgumentException("Values array must not be null");
    }
    if (values.length != types.length) {
      throw new IllegalArgumentException("Values array must be the same length as the types array");
    }
    if (types.length == 0) {
      iTypes = types;
      iValues = values;
      return;
    }
    for (int i = 0; i < types.length; i++) {
      if (types[i] == null) {
        throw new IllegalArgumentException("Types array must not contain null: index " + i);
      }
    }
    DurationField lastUnitField = null;
    for (int i = 0; i < types.length; i++) {
      DateTimeFieldType loopType = types[i];
      DurationField loopUnitField = loopType.getDurationType().getField(iChronology);
      if (i > 0) {
        if (loopUnitField.isSupported() == false) {
          if (lastUnitField.isSupported()) {
            throw new IllegalArgumentException(
                "Types array must be in order largest-smallest: "
                    + types[i - 1].getName()
                    + " < "
                    + loopType.getName());
          } else {
            throw new IllegalArgumentException(
                "Types array must not contain duplicate unsupported: "
                    + types[i - 1].getName()
                    + " and "
                    + loopType.getName());
          }
        }
        int compare = lastUnitField.compareTo(loopUnitField);
        if (compare < 0) {
          throw new IllegalArgumentException(
              "Types array must be in order largest-smallest: "
                  + types[i - 1].getName()
                  + " < "
                  + loopType.getName());
        } else if (compare == 0) {
          if (lastUnitField.equals(loopUnitField)) {
            DurationFieldType lastRangeType = types[i - 1].getRangeDurationType();
            DurationFieldType loopRangeType = loopType.getRangeDurationType();
            if (lastRangeType == null) {
              if (loopRangeType == null) {
                throw new IllegalArgumentException(
                    "Types array must not contain duplicate: "
                        + types[i - 1].getName()
                        + " and "
                        + loopType.getName());
              }
            } else {
              if (loopRangeType == null) {
                throw new IllegalArgumentException(
                    "Types array must be in order largest-smallest: "
                        + types[i - 1].getName()
                        + " < "
                        + loopType.getName());
              }
              DurationField lastRangeField = lastRangeType.getField(iChronology);
              DurationField loopRangeField = loopRangeType.getField(iChronology);
              if (lastRangeField.compareTo(loopRangeField) < 0) {
                throw new IllegalArgumentException(
                    "Types array must be in order largest-smallest: "
                        + types[i - 1].getName()
                        + " < "
                        + loopType.getName());
              }
              if (lastRangeField.compareTo(loopRangeField) == 0) {
                throw new IllegalArgumentException(
                    "Types array must not contain duplicate: "
                        + types[i - 1].getName()
                        + " and "
                        + loopType.getName());
              }
            }
          } else {
            if (lastUnitField.isSupported()
                && lastUnitField.getType() != DurationFieldType.YEARS_TYPE) {
              throw new IllegalArgumentException(
                  "Types array must be in order largest-smallest,"
                      + " for year-based fields, years is defined as being largest: "
                      + types[i - 1].getName()
                      + " < "
                      + loopType.getName());
            }
          }
        }
      }
      lastUnitField = loopUnitField;
    }

    iTypes = (DateTimeFieldType[]) types.clone();
    chronology.validate(this, values);
    iValues = (int[]) values.clone();
  }