📄 utility.java
字号:
package org.momeunit.ant.core;import java.io.File;import java.text.NumberFormat;import java.util.Random;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;import org.momeunit.ant.taskdefs.EmptyAttributeException;/** * Collection of static utility methods. * * @author Sergio Morozov * @version 1.1.2 */public class Utility{ private static final NumberFormat timeFormatter = NumberFormat .getNumberInstance(); static { timeFormatter.setMaximumFractionDigits(6); timeFormatter.setGroupingUsed(false); } /** * Prohibits instantiation. * * @since 1.1 */ protected Utility() { super(); } /** * New line character. * * @since 1.1 */ public static final String NEWLINE = System.getProperty("line.separator"); /** * Asserts that given file is an existent directory. * * @param dir * file to test. * @throws BuildException * if given file is not an existent directory. * @since 1.1 */ public static void assertDirectory(File dir) { assertDirectory(dir, null); } /** * Asserts that given file is an existent directory. * * @param dir * file to test. * @param type * type of directory: string prepended to error message. * @throws BuildException * if given file is not an existent directory. * @since 1.1 */ public static void assertDirectory(File dir, String type) { if (dir == null || !dir.exists() || !dir.isDirectory()) throw new BuildException( (type != null ? type + " " : "") + (dir != null ? dir.getAbsolutePath() : "unknown") + " is not a directory."); } /** * Asserts that given file exists. * * @param file * file to test. * @param type * type of file: string prepended to error message. * @throws BuildException * if given file doesn't exist. * @since 1.1 */ public static void assertExists(File file, String type) { if (file == null || !file.exists()) throw new BuildException( (type != null ? type + " " : "") + (file != null ? file.getAbsolutePath() : "unknown") + " does not exist."); } /** * Asserts that given file exists. * * @param file * file to test. * @throws BuildException * if given file doesn't exist. * @since 1.1 */ public static void assertExists(File file) { assertExists(file, null); } /** * Asserts that given file is a regular file. * * @param file * file to test. * @param type * type of file: string prepended to error message. * @throws BuildException * if given file is not a regular file. * @since 1.1 */ public static void assertFile(File file, String type) { if (file == null || !file.exists() || !file.isFile()) throw new BuildException( (type != null ? type + " " : "") + (file != null ? file.getAbsolutePath() : "unknown") + " is not an existent file."); } /** * Asserts that given file is a regular file. * * @param file * file to test. * @throws BuildException * if given file is not a regular file. * @since 1.1 */ public static void assertFile(File file) { assertFile(file, null); } /** * Asserts that given attribute is not empty. * * @param attr * value of attribute to test. * @param attrName * name of attribute to test. * @param elName * name of element containing an attribute. * @throws BuildException * if given attribute is empty. * @since 1.1 */ public static void assertNotEmpty(String attr, String attrName, String elName) { if (attr == null || attr.length() == 0) throw new EmptyAttributeException( attrName, elName); } /** * Asserts that required attribute is set. * * @param attr * value of attribute to test. * @param attrName * name of attribute to test. * @param elName * name of element containing an attribute. * @throws BuildException * if given attribute is empty. * @since 1.1 */ public static void assertRequiredAttribute(Object attr, String attrName, String elName) { if (attr == null) throw new BuildException( "Unspecified required attribute " + attrName + " of <" + elName + ">"); } /** * Formats time specified as double value of seconds. * * @param time * seconds as double value. * @return formatted string. * @since 1.1 */ public static String formatTime(double time) { return timeFormatter.format(time); } /** * <p> * Parses given string. Returns the time interval as number of milliseconds or * <code>-1</code> if parse error occurs. String must be of the form: * </p> * <code> <DoubleLiteral>[<UnitSuffix>]</code> * <p> * <code>DoubleLiteral</code> is double number string representation as * specified by {@link Double#valueOf(String)}. * </p> * <p> * <code>UnitSuffix</code> is an optional specification of unit of time. It * can be one of the following * <ul> * <li><code>h</code> - for hours;</li> * <li><code>m</code> - for minutes;</li> * <li><code>s</code> - for seconds;</li> * <li><code>ms</code> - for milliseconds.</li> * </ul> * If omitted milliseconds are implied. <code>UnitSuffix</code> is case * insensitive (<code>ms</code>, <code>MS</code>, <code>Ms</code>, * <code>mS</code> mean the same). * </p> * * @param timeInterval * string representing time interval. * @return the time interval in milliseconds or <code>-1</code> if string is * not parsable. * @since 1.1.1 */ public static long parseTimeInterval(String timeInterval) { long res = -1; if (timeInterval != null) { int m = 1; int lastCharPos = timeInterval.length() - 1; char lastChar = timeInterval.charAt(lastCharPos); switch (lastChar) { case 'S': case 's': m = 1000; if (lastCharPos > 0 && (timeInterval.charAt(lastCharPos - 1) == 'M' || timeInterval .charAt(lastCharPos - 1) == 'm')) { m = 1; lastCharPos--; } break; case 'M': case 'm': m = 60000; break; case 'H': case 'h': m = 3600000; break; default: lastCharPos++; break; } try { res = (int) (Double.parseDouble(timeInterval.substring(0, lastCharPos)) * m); } catch (Throwable e) {} } return res; } /** * Logs specified <code>message</code> using given <code>project</code> if * <code>project</code> is not <code>null</code>. Does nothing if * <code>project</code> is <code>null</code>. * * @param project * project that logs the message. * @param message * message to log. * @since 1.1 */ public static void log(Project project, String message) { if (project != null) project.log(message); } /** * Logs specified <code>message</code> using given <code>project</code> * and <code>priority</code> if <code>project</code> is not * <code>null</code>. Does nothing if <code>project</code> is * <code>null</code>. * * @param project * project that logs the message. * @param message * message to log. * @param priority * priority with which to log the message. * @since 1.1 */ public static void log(Project project, String message, int priority) { if (project != null) project.log(message, priority); } /** * Logs specified <code>message</code> using given <code>task</code> if * <code>task</code> is not <code>null</code>. Does nothing if * <code>task</code> is <code>null</code>. * * @param task * task that logs the message. * @param message * message to log. * @since 1.1 */ public static void log(Task task, String message) { if (task != null) task.log(message); } /** * Logs specified <code>message</code> using given <code>task</code> and * <code>priority</code> if <code>task</code> is not <code>null</code>. * Does nothing if <code>task</code> is <code>null</code>. * * @param task * task that logs the message. * @param message * message to log. * @param priority * priority with which to log the message. * @since 1.1 */ public static void log(Task task, String message, int priority) { if (task != null) task.log(message, priority); } /** * Creates and returns unique directory with name composed of specified prefix * and random numeric part not greater than given value. Directory is created * in specified <code>parent</code> directory. * * @param parent * parent directory to create directory in. If <code>null</code> * current directory is used. * @param prefix * prefix of created directory. * @param maxNumber * maximum value of random numeric part. For example * <code>99999</code> for five characters long random part. * @return created directory. * @throws BuildException * if an error occurred during directory creation. * @since 1.1.2 */ public static File createUniqueDirectory(File parent, String prefix, int maxNumber) { File dir = null; for (Random random = new Random(); dir == null || dir.exists(); dir = new File( parent, prefix + random.nextInt(maxNumber))); if (!dir.mkdir()) throw new BuildException("Error creating directory " + dir.getAbsolutePath()); return dir; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -