Created
February 19, 2018 14:14
-
-
Save americanstone/d8e2eca4f6d2eef0bff94bc247ad6947 to your computer and use it in GitHub Desktop.
Factory pattern with enum type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Supported filters (with names). | |
*/ | |
public enum Type { | |
SEQUENTIAL_LOOPS(SequentialLoopsCrawler.class), | |
SEQUENTIAL_STREAMS(SequentialStreamsCrawler.class); | |
private final Class<? extends ImageCrawlerBase> clazz; | |
Type(Class<? extends ImageCrawlerBase> clazz) { | |
this.clazz = clazz; | |
} | |
@Override | |
public String toString() { | |
return clazz.getSimpleName(); | |
} | |
} | |
private CrawlerFactory () { | |
} | |
/** | |
* Creates the specified {@code type} filter. Any images downloaded using | |
* this filter will be saved in a folder that has the same name as the | |
* filter class. | |
* | |
* @param crawlerType Type of filter. | |
* @param filterTypes List of filter types for crawler to use. | |
* @param rootUri The root uri where the crawl should begin. | |
* @return A filter instance of the specified type. | |
*/ | |
public static ImageCrawlerBase newCrawler(Type crawlerType, | |
List<FilterFactory.Type> filterTypes, | |
String rootUri) { | |
try { | |
ImageCrawlerBase crawler = crawlerType.clazz.newInstance(); // must have default constructor | |
crawler.initialize(FilterFactory.newFilters(filterTypes), | |
rootUri); | |
return crawler; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public abstract class ImageCrawlerBase | |
implements Runnable { | |
/** | |
* Debugging tag. | |
*/ | |
protected final String TAG = this.getClass().getName(); | |
/** | |
* The List of filters to apply to the images. | |
*/ | |
protected List<Filter> mFilters; | |
/** | |
* The root URL or pathname to start the search. | |
*/ | |
protected String mRootUri; | |
/** | |
* A cache of unique URIs that have already been processed. | |
*/ | |
protected ConcurrentHashSet<String> mUniqueUris; | |
/** | |
* Flag used to cancel the current crawl. | |
*/ | |
private boolean mStopCrawl; | |
/** | |
* Constructor that is only used by CrawlerFactory. | |
*/ | |
protected ImageCrawlerBase() { | |
} | |
/** | |
* Constructor that initializes filters and rootUri. | |
*/ | |
public ImageCrawlerBase(List<Filter> filters, | |
String rootUri) { | |
initialize(filters, rootUri); | |
} | |
/** | |
* Called from CrawlerFactory after creating a new crawler instance. | |
*/ | |
protected void initialize(List<Filter> filters, | |
String rootUri) { | |
// Should only ever be called once. | |
if (mFilters != null || mRootUri != null || mUniqueUris != null) { | |
throw new IllegalStateException("A crawler should onlly be initialized once."); | |
} | |
// Store the Filters to apply as a list. | |
mFilters = filters; | |
// Store the root Uri. | |
mRootUri = rootUri; | |
// Initialize the cache of processed Uris. | |
mUniqueUris = new ConcurrentHashSet<>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment