Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.EnumSet;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @author Oifan
- *
- */
- public class Utils {
- /**
- * There is no reason to instantiate class containing <b>only static</b> methods.
- */
- private Utils() {
- throw new UnsupportedOperationException("Instantiation of 'STATIC' class is not allowed");
- }
- /**
- * Tries to convert <code>fileName</code> to URL - first on filesystem, then via ClassLoader as
- * a resource.
- *
- * @param fileName
- * @return
- */
- public static URL fileOrResourceToURL(final String fileName) {
- try {
- final File file = new File(fileName);
- return (file.exists() ? file.toURI().toURL() : Utils.class.getClassLoader().getResource(fileName));
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * Copies from List <code>data</code> only elements flagged as <b>true</b> in
- * <code>allowedIdx</code> into <code>result</code>.
- *
- * @param data
- * @param allowedIdx
- * @param result
- */
- public static <T> void filterList(List<T> data, boolean[] allowedIdx, List<T> result) {
- final int len = Math.min(data.size(), allowedIdx.length);
- for (int i = 0; i < len; ++i) {
- if (allowedIdx[i])
- result.add(data.get(i));
- }
- }
- /**
- * Retrieves the last non-empty token (part) of '/'-delimited path.
- * @param path
- * '/'-delimited path
- * @return
- * last non-empty path token or <code>path</code> if no '/' was found
- */
- public static String getLastTokenFromPath(String path) {
- String[] tokens = path.split("\\/");
- return (tokens.length > 0 ? tokens[tokens.length - 1] : path);
- }
- /**
- * Tries to locate / create <code>file</code>.
- *
- * @param file
- * @throws IOException
- */
- public static void initFile(File file) throws IOException {
- final String logPrefix = "initFile: ";
- if (!file.createNewFile()) {
- if (!file.isFile()) {
- throw new IllegalStateException(String.format("%sCannot create file '%s', since it is NOT a FILE!", logPrefix,
- file.getAbsolutePath()));
- }
- if (!file.canWrite()) {
- throw new IllegalStateException(String.format("%sCannot write to file '%s'!", logPrefix, file
- .getAbsolutePath()));
- }
- }
- }
- /**
- * Tries to locate / create <code>folder</code>.
- *
- * @param folder
- * @throws IllegalStateException
- */
- public static void initFolder(File folder) throws IllegalStateException {
- final String logPrefix = "initFolder: ";
- if (folder.exists()) {
- // check whether it is a directory
- if (!folder.isDirectory()) {
- throw new IllegalStateException(String.format(
- "%sCannot enter folder '%s', since it is a FILE - cannot write files in there!", logPrefix,
- folder.getAbsolutePath()));
- }
- } else {
- // create folder since it does not exist
- if (!folder.mkdirs()) {
- // creating folder failed - abort
- throw new IllegalStateException(String.format("%sCould not create folder '%s' - cannot write files in there!",
- logPrefix, folder.getAbsolutePath()));
- }
- }
- }
- /**
- * Tries to create <b>destination folder</b>, which is composed from destParentFolder and
- * <code>child</code>.
- *
- * @param destParentFolder
- * can be null, in which case no folder will be created and null will be returned
- * @param child
- * @throws IllegalStateException
- * @returns <b>destination folder</b> if created / exists, null otherwise
- */
- public static File createDestFolder(File destParentFolder, String child)
- throws IllegalStateException {
- if (destParentFolder != null) {
- final File destFolder = new File(destParentFolder, child);
- Utils.initFolder(destFolder);
- return destFolder;
- } else {
- return null;
- }
- }
- /**
- * Uses {@link ServiceLoader} to load all implementations of given API <code>api</code>.<br>
- * For this to work, you have to write a list of implementations (fully qualified class names
- * w/o ".class") into file <code>META-INF/services/<T></code> (containing 1 implementation
- * per line).
- *
- * @param <T>
- * type of {@link Interface} to find
- * @param apiClazz
- * {@link Interface} of the Service
- * @return List of found Service implementations
- */
- public static <T> List<T> loadServices(Class<T> apiClazz) {
- final List<T> result = new ArrayList<T>();
- for (T loadedImpl : ServiceLoader.load(apiClazz)) {
- result.add(loadedImpl);
- }
- return result;
- }
- /**
- * Builds unmodifiable lookup-Map for an {@link Enum} - useful for Enums modified by
- * {@link #setEnumOrdinal(Enum, int)}.
- *
- * @param <E>
- * @param enumClazz
- * @return Unmodifiable Map
- */
- public static <E extends Enum<E>> Map<Integer, E> lookupMap(Class<E> enumClazz) {
- final Map<Integer, E> lookup = new HashMap<Integer, E>();
- for (E element : EnumSet.allOf(enumClazz)) {
- lookup.put(element.ordinal(), element);
- }
- return Collections.unmodifiableMap(lookup);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement