001package org.avaje.classpath.scanner;
002
003/**
004 * Some common resource matching predicates.
005 */
006public class FilterResource {
007
008  /**
009   * Return a resource matcher that matches by both prefix and suffix.
010   */
011  public static ResourceFilter byPrefixSuffix(String prefix, String suffix) {
012    return new ByPrefixSuffix(prefix, suffix);
013  }
014
015  /**
016   * Return a resource matcher that matches by suffix.
017   */
018  public static ResourceFilter bySuffix(String suffix) {
019    return new BySuffix(suffix);
020  }
021
022  /**
023   * Return a resource matcher that matches by prefix.
024   */
025  public static ResourceFilter byPrefix(String prefix) {
026    return new ByPrefix(prefix);
027  }
028
029  private FilterResource() {
030  }
031
032  private static class ByPrefixSuffix implements ResourceFilter {
033
034    private final String prefix;
035    private final String suffix;
036
037    ByPrefixSuffix(String prefix, String suffix) {
038      this.prefix = prefix;
039      this.suffix = suffix;
040    }
041
042    @Override
043    public boolean isMatch(String resourceName) {
044      // resources always use '/' as separator
045      String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
046      return fileName.startsWith(prefix) && fileName.endsWith(suffix);
047    }
048  }
049
050  private static class BySuffix implements ResourceFilter {
051
052    private final String suffix;
053
054    BySuffix(String suffix) {
055      this.suffix = suffix;
056    }
057
058    @Override
059    public boolean isMatch(String resourceName) {
060      return resourceName.endsWith(suffix);
061    }
062  }
063
064  private static class ByPrefix implements ResourceFilter {
065
066    private final String prefix;
067
068    ByPrefix(String prefix) {
069      this.prefix = prefix;
070    }
071
072    @Override
073    public boolean isMatch(String resourceName) {
074      return resourceName.startsWith(prefix);
075    }
076  }
077}