001/*
002  Copyright 2010-2016 Boxfuse GmbH
003  <p/>
004  Licensed under the Apache License, Version 2.0 (the "License");
005  you may not use this file except in compliance with the License.
006  You may obtain a copy of the License at
007  <p/>
008  http://www.apache.org/licenses/LICENSE-2.0
009  <p/>
010  Unless required by applicable law or agreed to in writing, software
011  distributed under the License is distributed on an "AS IS" BASIS,
012  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  See the License for the specific language governing permissions and
014  limitations under the License.
015 */
016package io.avaje.classpath.scanner.core;
017
018import io.avaje.classpath.scanner.Resource;
019import io.avaje.classpath.scanner.internal.EnvironmentDetection;
020import io.avaje.classpath.scanner.internal.ResourceAndClassScanner;
021import io.avaje.classpath.scanner.internal.scanner.classpath.ClassPathScanner;
022import io.avaje.classpath.scanner.internal.scanner.classpath.android.AndroidScanner;
023import io.avaje.classpath.scanner.internal.scanner.filesystem.FileSystemScanner;
024
025import java.util.List;
026import java.util.function.Predicate;
027
028/**
029 * Scanner for Resources and Classes.
030 */
031public class Scanner implements io.avaje.classpath.scanner.ClassPathScanner {
032
033  private final ResourceAndClassScanner resourceAndClassScanner;
034
035  private final FileSystemScanner fileSystemScanner = new FileSystemScanner();
036
037  public Scanner(ClassLoader classLoader) {
038    if (EnvironmentDetection.isAndroid()) {
039      resourceAndClassScanner = new AndroidScanner(classLoader);
040    } else {
041      resourceAndClassScanner = new ClassPathScanner(classLoader);
042    }
043  }
044
045  /**
046   * Scans this location for resources matching the given predicate.
047   * <p>
048   * The location can have a prefix of <code>filesystem:</code> or <code>classpath:</code> to determine
049   * how to scan. If no prefix is used then classpath scan is the default.
050   * </p>
051   *
052   * @param location  The location to start searching. Subdirectories are also searched.
053   * @param predicate The predicate used to match resource names.
054   * @return The resources that were found.
055   */
056  public List<Resource> scanForResources(Location location, Predicate<String> predicate) {
057    if (location.isFileSystem()) {
058      return fileSystemScanner.scanForResources(location, predicate);
059    }
060    return resourceAndClassScanner.scanForResources(location, predicate);
061  }
062
063  /**
064   * Scans this location for resources matching the given predicate.
065   * <p>
066   * The location can have a prefix of <code>filesystem:</code> or <code>classpath:</code> to determine
067   * how to scan. If no prefix is used then classpath scan is the default.
068   * </p>
069   *
070   * @param location  The location to start searching. Subdirectories are also searched.
071   * @param predicate The predicate used to match resource names.
072   * @return The resources that were found.
073   */
074  @Override
075  public List<Resource> scanForResources(String location, Predicate<String> predicate) {
076    return scanForResources(new Location(location), predicate);
077  }
078
079  /**
080   * Scans the classpath for classes under the specified package matching the given predicate.
081   *
082   * @param location  The package in the classpath to start scanning. Subpackages are also scanned.
083   * @param predicate The predicate used to match scanned classes.
084   * @return The classes found matching the predicate
085   */
086  public List<Class<?>> scanForClasses(Location location, Predicate<Class<?>> predicate) {
087    return resourceAndClassScanner.scanForClasses(location, predicate);
088  }
089
090  /**
091   * Scans the classpath for classes under the specified package matching the given predicate.
092   *
093   * @param location  The package in the classpath to start scanning. Subpackages are also scanned.
094   * @param predicate The predicate used to match scanned classes.
095   * @return The classes found matching the predicate
096   */
097  @Override
098  public List<Class<?>> scanForClasses(String location, Predicate<Class<?>> predicate) {
099    return scanForClasses(new Location(location), predicate);
100  }
101
102}