Package net.sf.freecol.common.util
Class Utils
- java.lang.Object
-
- net.sf.freecol.common.util.Utils
-
public class Utils extends java.lang.Object
Collection of small static helper methods.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
HEX_DIGITS
Hex constant digits for get/restoreRandomState.private static java.util.logging.Logger
logger
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
delay(long ms, java.lang.String warning)
Delay by a number of milliseconds.static void
deleteFile(java.io.File file)
Delete a file.static void
deleteFiles(java.util.List<java.io.File> files)
Delete a list of files.static int
determineDpi(java.awt.GraphicsDevice gd)
Tries to determine the DPI of the givenGraphicsDevice
.static boolean
directoryAllPresent(java.io.File dir, java.lang.String... names)
Does a directory contain the given file/s?static <T> boolean
equals(T one, T two)
Check if two objects are equal but also checks for null.static boolean
fileAnySuffix(java.io.File file, java.lang.String... suffixes)
Does a readable file have a matching suffix?static void
garbageCollect()
Run the garbage collector.private static java.io.Writer
getF8W(java.io.File file, boolean append)
Create a new file writer that uses UTF-8.static java.io.Writer
getFileUTF8AppendWriter(java.io.File file)
Create a new appending file writer that uses UTF-8.static java.io.Reader
getFileUTF8Reader(java.io.File file)
Create a new file reader that uses UTF-8.static java.io.Writer
getFileUTF8Writer(java.io.File file)
Create a new file writer that uses UTF-8.static java.awt.GraphicsDevice
getGoodGraphicsDevice()
Get a good screen device for starting FreeCol.static java.lang.String
getRandomState(java.util.Random random)
Get the internal state of a random number generator as a string.static java.lang.String
getUTF8Contents(java.io.File file)
Get the UTF-8 encoded contents of a file.static java.io.Writer
getUTF8Writer(java.io.OutputStream os)
Create a new file writer that uses UTF-8.static int
hashCode(java.lang.Object object)
Get a hash code for an object, even null.static boolean
isHeadless()
Are we in headless mode?static javax.xml.transform.Transformer
makeTransformer(boolean declaration, boolean indent)
Helper to make an XML Transformer.static long
now()
Current time since epoch in milliseconds.static java.util.Random
restoreRandomState(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
- TheObject
to use.- Returns:
- A hash code.
-
getRandomState
public static java.lang.String getRandomState(java.util.Random random) throws java.io.IOException
Get 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
String
encapsulating 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
- AFile
to read from.- Returns:
- A
Reader
for this file.
-
getUTF8Contents
public static java.lang.String getUTF8Contents(java.io.File file)
Get the UTF-8 encoded contents of a file.- Parameters:
file
- TheFile
to 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
- AnOutputStream
to write to.- Returns:
- A
Writer
for 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
- AFile
to write to.append
- If true, append to the file.- Returns:
- A
Writer
for this file.
-
getFileUTF8Writer
public static java.io.Writer getFileUTF8Writer(java.io.File file)
Create a new file writer that uses UTF-8.- Parameters:
file
- AFile
to write to.- Returns:
- A
Writer
for 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
- AFile
to append to.- Returns:
- A
Writer
for 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
- TheFile
to delete.
-
deleteFiles
public static void deleteFiles(java.util.List<java.io.File> files)
Delete a list of files.- Parameters:
files
- The list ofFile
s 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
- TheFile
to 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 directoryFile
to 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
- TheGraphicsDevice
to determine the DPI for.- Returns:
- The calculated DPI.
-
-