示例#1
0
  public GlobScanner(
      File rootDir, List<String> includes, List<String> excludes, boolean ignoreCase) {
    if (rootDir == null) throw new IllegalArgumentException("rootDir cannot be null.");
    if (!rootDir.exists())
      throw new IllegalArgumentException("Directory does not exist: " + rootDir);
    if (!rootDir.isDirectory())
      throw new IllegalArgumentException("File must be a directory: " + rootDir);
    try {
      rootDir = rootDir.getCanonicalFile();
    } catch (IOException ex) {
      throw new RuntimeException("OS error determining canonical path: " + rootDir, ex);
    }
    this.rootDir = rootDir;

    if (includes == null) throw new IllegalArgumentException("includes cannot be null.");
    if (excludes == null) throw new IllegalArgumentException("excludes cannot be null.");

    if (includes.isEmpty()) includes.add("**");
    List<Pattern> includePatterns = new ArrayList(includes.size());
    for (String include : includes) includePatterns.add(new Pattern(include, ignoreCase));

    List<Pattern> allExcludePatterns = new ArrayList(excludes.size());
    for (String exclude : excludes) allExcludePatterns.add(new Pattern(exclude, ignoreCase));

    scanDir(rootDir, includePatterns);

    if (!allExcludePatterns.isEmpty()) {
      // For each file, see if any exclude patterns match.
      outerLoop:
      //
      for (Iterator matchIter = matches.iterator(); matchIter.hasNext(); ) {
        String filePath = (String) matchIter.next();
        List<Pattern> excludePatterns = new ArrayList(allExcludePatterns);
        try {
          // Shortcut for excludes that are "**/XXX", just check file name.
          for (Iterator excludeIter = excludePatterns.iterator(); excludeIter.hasNext(); ) {
            Pattern exclude = (Pattern) excludeIter.next();
            if (exclude.values.length == 2 && exclude.values[0].equals("**")) {
              exclude.incr();
              String fileName = filePath.substring(filePath.lastIndexOf(File.separatorChar) + 1);
              if (exclude.matches(fileName)) {
                matchIter.remove();
                continue outerLoop;
              }
              excludeIter.remove();
            }
          }
          // Get the file names after the root dir.
          String[] fileNames = filePath.split("\\" + File.separator);
          for (String fileName : fileNames) {
            for (Iterator excludeIter = excludePatterns.iterator(); excludeIter.hasNext(); ) {
              Pattern exclude = (Pattern) excludeIter.next();
              if (!exclude.matches(fileName)) {
                excludeIter.remove();
                continue;
              }
              exclude.incr(fileName);
              if (exclude.wasFinalMatch()) {
                // Exclude pattern matched.
                matchIter.remove();
                continue outerLoop;
              }
            }
            // Stop processing the file if none of the exclude patterns matched.
            if (excludePatterns.isEmpty()) continue outerLoop;
          }
        } finally {
          for (Pattern exclude : allExcludePatterns) exclude.reset();
        }
      }
    }
  }
示例#2
0
  private static int evaluateForPattern(State state, int patternSize) {
    int size = State.BORAD_SIZE;
    int value = 0;
    int i, j;
    Pattern pattern1 = new Pattern(patternSize);
    Pattern pattern2 = new Pattern(patternSize);
    for (i = 0; i < size; i++) {
      pattern1.reset();
      pattern2.reset();
      for (j = 0; j < patternSize; j++) {
        pattern1.put(state.board[j][i]);
        pattern2.put(state.board[i][j]);
      }
      j--;
      do {
        //                System.out.println("i:" + i + " j:" + j);
        value += pattern1.matches();
        value += pattern2.matches();

        j++;
        if (j == size) break;
        //                pattern1 -= state.board[j - patternSize][i];
        pattern1.put(state.board[j][i]);
        //                pattern1 += state.board[j][i];
        //                pattern2 -= state.board[i][j - patternSize];
        pattern2.put(state.board[i][j]);
        //                pattern2 += state.board[i][j];
      } while (true);
    }
    // (k1-k2: 45 degree, k1-k3 : 135 degree)
    int k1, k2, k3;
    for (i = -(size - patternSize); i <= size - patternSize; i++) {
      if (i < 0) {
        k1 = -i;
        k2 = 0;
        k3 = size - 1;
      } else {
        k1 = 0;
        k2 = i;
        k3 = size - i - 1;
      }
      pattern1.reset();
      pattern2.reset();
      for (j = 0; j < patternSize; j++) {
        pattern1.put(state.board[k1 + j][k2 + j]);
        pattern2.put(state.board[k1 + j][k3 - j]);
      }
      j--;
      do {
        value += pattern1.matches();
        value += pattern2.matches();

        j++;
        // 2 patterns are symmetric, so detection for 1 is enough
        if (k1 + j == size || k2 + j == size) break;
        //                pattern1 -= state.board[k1 + j - patternSize][k2 + j - patternSize];
        pattern1.put(state.board[k1 + j][k2 + j]);
        //                pattern1 += state.board[k1 + j][k2 + j];
        //                pattern2 -= state.board[k1 + j - patternSize][k3 - j + patternSize];
        pattern2.put(state.board[k1 + j][k3 - j]);
        //                pattern2 += state.board[k1 + j][k3 - j];
      } while (true);
    }

    return value;
  }