Package net.sf.freecol.common.util
Class Utils
- java.lang.Object
-
- net.sf.freecol.common.util.Utils
-
public class Utils extends java.lang.ObjectCollection of small static helper methods.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringHEX_DIGITSHex constant digits for get/restoreRandomState.private static java.util.logging.Loggerlogger
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voiddelay(long ms, java.lang.String warning)Delay by a number of milliseconds.static voiddeleteFile(java.io.File file)Delete a file.static voiddeleteFiles(java.util.List<java.io.File> files)Delete a list of files.static intdetermineDpi(java.awt.GraphicsDevice gd)Tries to determine the DPI of the givenGraphicsDevice.static booleandirectoryAllPresent(java.io.File dir, java.lang.String... names)Does a directory contain the given file/s?static <T> booleanequals(T one, T two)Check if two objects are equal but also checks for null.static booleanfileAnySuffix(java.io.File file, java.lang.String... suffixes)Does a readable file have a matching suffix?static voidgarbageCollect()Run the garbage collector.private static java.io.WritergetF8W(java.io.File file, boolean append)Create a new file writer that uses UTF-8.static java.io.WritergetFileUTF8AppendWriter(java.io.File file)Create a new appending file writer that uses UTF-8.static java.io.ReadergetFileUTF8Reader(java.io.File file)Create a new file reader that uses UTF-8.static java.io.WritergetFileUTF8Writer(java.io.File file)Create a new file writer that uses UTF-8.static java.awt.GraphicsDevicegetGoodGraphicsDevice()Get a good screen device for starting FreeCol.static java.lang.StringgetRandomState(java.util.Random random)Get the internal state of a random number generator as a string.static java.lang.StringgetUTF8Contents(java.io.File file)Get the UTF-8 encoded contents of a file.static java.io.WritergetUTF8Writer(java.io.OutputStream os)Create a new file writer that uses UTF-8.static inthashCode(java.lang.Object object)Get a hash code for an object, even null.static booleanisHeadless()Are we in headless mode?static javax.xml.transform.TransformermakeTransformer(boolean declaration, boolean indent)Helper to make an XML Transformer.static longnow()Current time since epoch in milliseconds.static java.util.RandomrestoreRandomState(java.lang.String state)Restore a previously saved state.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
HEX_DIGITS
private static final java.lang.String HEX_DIGITS
Hex constant digits for get/restoreRandomState.- See Also:
- Constant Field Values
-
-
Method Detail
-
equals
public static <T> boolean equals(T one, T two)Check if two objects are equal but also checks for null.- Type Parameters:
T- The object type.- Parameters:
one- First object to comparetwo- Second object to compare- Returns:
- True if the arguments are either both null or equal in the sense of their equals() method.
-
hashCode
public static int hashCode(java.lang.Object object)
Get a hash code for an object, even null.- Parameters:
object- TheObjectto use.- Returns:
- A hash code.
-
getRandomState
public static java.lang.String getRandomState(java.util.Random random) throws java.io.IOExceptionGet the internal state of a random number generator as a string. It would have been more convenient to simply return the current seed, but unfortunately it is private.- Parameters:
random- A pseudo-random number source.- Returns:
- A
Stringencapsulating the object state. - Throws:
java.io.IOException- is the byte stream output breaks.
-
restoreRandomState
public static java.util.Random restoreRandomState(java.lang.String state)
Restore a previously saved state.- Parameters:
state- The saved state (@see #getRandomState()).- Returns:
- The restored
Random.
-
getFileUTF8Reader
public static java.io.Reader getFileUTF8Reader(java.io.File file)
Create a new file reader that uses UTF-8.- Parameters:
file- AFileto read from.- Returns:
- A
Readerfor this file.
-
getUTF8Contents
public static java.lang.String getUTF8Contents(java.io.File file)
Get the UTF-8 encoded contents of a file.- Parameters:
file- TheFileto query.- Returns:
- The contents string, or null on error.
-
getUTF8Writer
public static java.io.Writer getUTF8Writer(java.io.OutputStream os)
Create a new file writer that uses UTF-8.- Parameters:
os- AnOutputStreamto write to.- Returns:
- A
Writerfor this file.
-
getF8W
private static java.io.Writer getF8W(java.io.File file, boolean append)Create a new file writer that uses UTF-8.- Parameters:
file- AFileto write to.append- If true, append to the file.- Returns:
- A
Writerfor this file.
-
getFileUTF8Writer
public static java.io.Writer getFileUTF8Writer(java.io.File file)
Create a new file writer that uses UTF-8.- Parameters:
file- AFileto write to.- Returns:
- A
Writerfor this file.
-
getFileUTF8AppendWriter
public static java.io.Writer getFileUTF8AppendWriter(java.io.File file)
Create a new appending file writer that uses UTF-8.- Parameters:
file- AFileto append to.- Returns:
- A
Writerfor this file.
-
makeTransformer
public static javax.xml.transform.Transformer makeTransformer(boolean declaration, boolean indent)Helper to make an XML Transformer.- Parameters:
declaration- If true, include the XML declaration.indent- If true, set up the transformer to indent.- Returns:
- A suitable
Transformer.
-
deleteFile
public static void deleteFile(java.io.File file)
Delete a file.- Parameters:
file- TheFileto delete.
-
deleteFiles
public static void deleteFiles(java.util.List<java.io.File> files)
Delete a list of files.- Parameters:
files- The list ofFiles to delete.
-
fileAnySuffix
public static boolean fileAnySuffix(java.io.File file, java.lang.String... suffixes)Does a readable file have a matching suffix?- Parameters:
file- TheFileto check.suffixes- Suffixes to test.- Returns:
- True if any suffix matches.
-
directoryAllPresent
public static boolean directoryAllPresent(java.io.File dir, java.lang.String... names)Does a directory contain the given file/s?- Parameters:
dir- The directoryFileto check.names- The name of the files to find therein.- Returns:
- True if the file is present and readable.
-
delay
public static void delay(long ms, java.lang.String warning)Delay by a number of milliseconds.- Parameters:
ms- The number of milliseconds to delay.warning- If non-null, log this warning message on interrupt, otherwise propagate the interrupt.
-
now
public static long now()
Current time since epoch in milliseconds.- Returns:
- Time since epoch.
-
garbageCollect
public static void garbageCollect()
Run the garbage collector. Route all gc calls here, so we can disable the findbugs warning.
-
isHeadless
public static boolean isHeadless()
Are we in headless mode?- Returns:
- True if in headless mode.
-
getGoodGraphicsDevice
public static java.awt.GraphicsDevice getGoodGraphicsDevice()
Get a good screen device for starting FreeCol.- Returns:
- A screen device, or null if none available (as in headless mode).
-
determineDpi
public static int determineDpi(java.awt.GraphicsDevice gd)
Tries to determine the DPI of the givenGraphicsDevice.- Parameters:
gd- TheGraphicsDeviceto determine the DPI for.- Returns:
- The calculated DPI.
-
-