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