コード例 #1
0
  /**
   * Initialize derived fields from defining fields. This is called from constructor and from
   * readObject (de-serialization)
   *
   * @param definingCalendar the {@link Calendar} instance used to initialize this FastDateParser
   */
  private void init(Calendar definingCalendar) {

    final StringBuilder regex = new StringBuilder();
    final List<Strategy> collector = new ArrayList<Strategy>();

    final Matcher patternMatcher = formatPattern.matcher(pattern);
    if (!patternMatcher.lookingAt()) {
      throw new IllegalArgumentException(
          "Illegal pattern character '" + pattern.charAt(patternMatcher.regionStart()) + "'");
    }

    currentFormatField = patternMatcher.group();
    Strategy currentStrategy = getStrategy(currentFormatField, definingCalendar);
    for (; ; ) {
      patternMatcher.region(patternMatcher.end(), patternMatcher.regionEnd());
      if (!patternMatcher.lookingAt()) {
        nextStrategy = null;
        break;
      }
      final String nextFormatField = patternMatcher.group();
      nextStrategy = getStrategy(nextFormatField, definingCalendar);
      if (currentStrategy.addRegex(this, regex)) {
        collector.add(currentStrategy);
      }
      currentFormatField = nextFormatField;
      currentStrategy = nextStrategy;
    }
    if (patternMatcher.regionStart() != patternMatcher.regionEnd()) {
      throw new IllegalArgumentException(
          "Failed to parse \"" + pattern + "\" ; gave up at index " + patternMatcher.regionStart());
    }
    if (currentStrategy.addRegex(this, regex)) {
      collector.add(currentStrategy);
    }
    currentFormatField = null;
    strategies = collector.toArray(new Strategy[collector.size()]);
    parsePattern = Pattern.compile(regex.toString());
  }