org.cp.elements.util.search.SearcherFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cp-elements Show documentation
Show all versions of cp-elements Show documentation
Java Simplified. Extensions and Useful Constructs for the Java Platform.
Codeprimate Elements (a.k.a. cp-elements) is a Java library and micro-framework used to simplify
the development of software applications written in Java. Elements packages several APIs into one
library in order to address various application concerns and aspects of software design and development
collectively and conveniently. Elements is a highly simple, yet robust and proven library built on
solid OO principles, software design patterns and best practices to effectively solve common
and reoccurring problems in software development.
/*
* Copyright 2016 Author or Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cp.elements.util.search;
import org.cp.elements.lang.ObjectUtils;
import org.cp.elements.util.search.support.BinarySearch;
import org.cp.elements.util.search.support.LinearSearch;
/**
* The SearcherFactory class is a factory for creating instances of different Searcher implementations that implement
* different searching algorithms.
*
* @author John J. Blum
* @see org.cp.elements.util.search.Searcher
* @see org.cp.elements.util.search.SearchType
* @see org.cp.elements.util.search.support.BinarySearch
* @see org.cp.elements.util.search.support.LinearSearch
* @since 1.0.0
*/
@SuppressWarnings("unused")
public class SearcherFactory {
/**
* Creates an instance of the Searcher interface implementing the searching algorithm based on the SearchType.
*
* @param the Class type of the actual Searcher implementation based on the SearchType.
* @param type the type of searching algorithm Searcher implementation to create.
* @return a Searcher implementation subclass that implements the searching algorithm based on the SearchType.
* @see org.cp.elements.util.search.Searcher
* @see org.cp.elements.util.search.SearchType
*/
@SuppressWarnings("unchecked")
public static T createSearcher(final SearchType type) {
switch (ObjectUtils.defaultIfNull(type, SearchType.UNKNOWN_SEARCH)) {
case BINARY_SEARCH:
return (T) new BinarySearch();
case LINEAR_SEARCH:
return (T) new LinearSearch();
default:
throw new IllegalArgumentException(String.format("The SearchType (%1$s) is not supported by the %2$s!", type,
SearcherFactory.class.getSimpleName()));
}
}
/**
* Creates an instance of the Searcher interface implementing the searching algorithm based on the SearchType,
* otherwise returns the provided default Searcher implementation if a Searcher based on the specified SearchType
* is not available.
*
* @param the Class type of the actual Searcher implementation based on the SearchType.
* @param type the type of searching algorithm Searcher implementation to create.
* @param defaultSearcher the default Searcher implementation to use if a Searcher based on the specified SearchType
* is not available.
* @return a Searcher implementation subclass that implements the searching algorithm based on the SearchType,
* or the provided default Searcher implementation if the Searcher based on the SearchType is not available.
* @see #createSearcher(SearchType)
* @see org.cp.elements.util.search.Searcher
* @see org.cp.elements.util.search.SearchType
*/
public static T createSearcherElseDefault(final SearchType type, final T defaultSearcher) {
try {
return createSearcher(type);
}
catch (IllegalArgumentException ignore) {
return defaultSearcher;
}
}
}