public class Util
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Util.ImageFormat
Image formats supported for UML diagrams.
|
Modifier and Type | Field and Description |
---|---|
static char |
EN_DASH
IEC editors were replacing the regular dash "-" in captions with EN DASH "–".
|
static java.lang.String |
FILE_SEP |
static int |
INDENT_COUNT |
static java.lang.String |
NL |
static char |
NON_BREAKING_WHITE_SPACE
This is what in MS Word looks like degree celsius...: ' '
|
static java.lang.String |
PATH_SEP |
static char |
TOKEN_DELIMITTER
Character used to "enclose" a token that is to be concatenated with a separator.
|
static int |
TRUNCATE_GREATER_THAN |
static java.lang.String |
USER_DIR |
static java.lang.String |
USER_DIR_KEY |
static java.lang.Integer |
ZERO |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
capitalise(java.lang.String input)
Returns string starting with upper-case letter, all the other letters lower-case.
|
static void |
clearClipboard()
Clears system clipboard.
|
static java.lang.String |
concatCharSeparatedTokens(java.lang.String separator,
boolean delimitTokens,
java.util.List<java.lang.String> tokens)
Concatenates
tokens with the separator string between consecutive
ones, and returns the resulting string. |
static void |
copy(java.io.File src,
java.io.File dst)
Copies src file to dst file.
|
static void |
copyHtmlToClipboard(java.lang.String htmlBody)
Surrounds the non-empty, non-null
htmlBody into doctype and html tags to produce
a valid HTML document; no-op otherwise. |
static void |
copyImageToClipboard(java.io.File pic)
Copies image in
pic to clipboard. |
static void |
copyTextToClipboard(java.lang.String txt)
Copies non-empty, non-null
txt to clipboard, no-op otherwise. |
static <K,V> java.util.Map<K,V> |
createKeyValuePair(K key,
V value)
Retruns the map with a single key/value pair.
|
static java.io.File |
createTempImageFile(java.lang.String dirAbsPath,
java.lang.String fileName,
Util.ImageFormat format,
boolean deleteOnExit)
Creates file in the given directory (or in default OS tmp directory) and returns the result.
|
static void |
delete(java.io.File f)
Wrapper for the
File.delete() that accepts null argument and returns nothing. |
static void |
ensureContainsNoNull(java.util.Collection<?> arg,
java.lang.String name) |
static void |
ensureContainsNoNull(java.lang.Object[] arg,
java.lang.String name) |
static void |
ensureNotEmpty(boolean[] arg,
java.lang.String name) |
static void |
ensureNotEmpty(java.util.Collection<?> arg,
java.lang.String name) |
static void |
ensureNotEmpty(int[] arg,
java.lang.String name) |
static void |
ensureNotEmpty(java.util.Map<?,?> arg,
java.lang.String name) |
static void |
ensureNotEmpty(java.lang.Object[] arg,
java.lang.String name) |
static void |
ensureNotEmpty(java.lang.String arg,
java.lang.String name) |
static void |
ensureNotNull(java.lang.Object arg,
java.lang.String name) |
static java.lang.String |
fetchTextFromClipboard()
Returns text contained in the clipboard (text could be plain or markup), null if clipboard is
empty.
|
static java.lang.String |
fillString(int count,
char ch)
Returns the string filled with number
count of characters c . |
static java.io.InputStream |
findResourceOnClasspath(java.lang.String resourceName)
Returns resource as input stream for its name, given that it is found on the classpath.
|
static java.lang.String |
formatDuration(long millis) |
static java.io.File |
getDirectory(java.lang.String dirRelPath,
boolean createIfMissing)
Returns file representing directory
dirName under "user.dir". |
static java.lang.String |
getFileExtension(java.lang.String filePath)
Returns extension (after the last ".") if being part of
filePath , null
otherwise. |
static java.lang.String |
getFileExtensionWithDot(java.lang.String filePath)
Returns extension with the "." if being part of
filePath , null otherwise. |
static java.lang.String |
getIndentSpaces(int count)
Returns string of spaces of the size equal to count *
INDENT_COUNT . |
static <K,V> K |
getKeyByValue(java.util.Map<K,V> map,
V value)
FIXME: tests
|
static <K,V> java.util.Set<K> |
getKeysByValue(java.util.Map<K,V> map,
V value)
FIXME: tests
|
static java.lang.String |
getNonBreakingSpaces(int count)
Returns string of
count non-breaking spaces. |
static java.io.File |
getOutputFileRenameIfExists(java.lang.String outDirName,
java.lang.String outFileName)
|
static java.lang.String |
getResourceAbsPath(java.lang.String resourceName,
java.lang.String detail)
Returns absolute path of the resource found on the classpath.
|
static boolean |
hasContent(java.lang.String value)
Returns true is
value is not null, and the trimmed content is not empty. |
static java.util.Properties |
initPropsFromFile(java.lang.String propsFileName)
Returns properties loaded from file
propsFileName expected to be on the
classpath, empty properties if the file has not been found. |
static java.util.List<java.io.File> |
listFiles(java.io.File directory,
java.io.FilenameFilter filter,
boolean recurse)
Returns potentially empty list of files under
directory (and its sub-directories
if recurse is true ), filtered with filter . |
static <T> void |
logCollection(org.apache.log4j.Level level,
java.util.Collection<T> objects,
java.lang.String what)
Logs each element in
objects . |
static void |
logCompletion(org.apache.log4j.Level level,
java.lang.String text,
long startMillis,
boolean skipTime)
Logs
text with level (and if skipTime=false , duration
since startMillis ). |
static <T,V> void |
logMap(org.apache.log4j.Level level,
java.util.Map<T,? extends java.util.Collection<V>> objects,
java.lang.String what)
Logs each element in
objects . |
static void |
logSubtitle(org.apache.log4j.Level level,
java.lang.String subtitle)
Logs
subtitle with level (for sub-steps in the application). |
static void |
logTitle(org.apache.log4j.Level level,
java.lang.String... title)
Logs
title with level (for major steps in the application). |
static boolean |
looksLikePlural(java.lang.String token)
Returns whether
token looks like plural; returns false for null or empty arg. |
static java.lang.String |
null2empty(java.lang.String s)
Returns empty string if
s is null, s otherwise. |
static java.lang.Integer |
parseInt(java.lang.String intStr)
Returns integer from
intStr if it isn't null and isn't empty after trimming;
otherwise returns null. |
static java.lang.Integer |
parseIntZero(java.lang.String intStr)
Returns integer from
intStr if it isn't null and isn't empty after trimming;
otherwise returns integer with value 0. |
static void |
saveImageFromClipboard(java.io.File pic) |
static java.io.File |
saveToFile(java.lang.String filePath,
java.lang.String content)
Saves
content to filePath and logs the confirmation with
level and return the file. |
static <T> java.util.Map<java.lang.String,T> |
sortByDecreasingLength(java.util.Map<java.lang.String,T> items)
Returns copy of
items sorted by decreasing length of keys (longest first). |
static java.lang.String[] |
sortByDecreasingLength(java.lang.String[] items)
Returns copy of
items sorted by decreasing length (longest first). |
static java.util.List<java.lang.String> |
splitCharSeparatedTokens(java.lang.String input,
char c)
Splits
c -separated string into a list of non-empty tokens. |
static java.util.List<java.lang.String> |
splitCommaSeparatedTokens(java.lang.String input)
Splits comma-separated string into a list of non-empty tokens.
|
static java.util.List<java.lang.String> |
splitDirAndFileNames(java.lang.String basePath,
java.lang.String relPath)
Returns (potentially empty) list of split members of
relPath , starting
immediately after the basePath . |
static java.util.List<java.lang.String> |
splitLines(java.lang.String input,
boolean compact)
Uses buffered and string reader to identify lines in
input and adds them to the
result to return. |
static java.util.List<java.lang.String> |
splitStringSeparatedTokens(java.lang.String input,
java.lang.String separator)
Splits
c -separated string into a list of non-empty tokens. |
static java.lang.String |
truncateEnd(java.lang.String input)
|
static java.lang.String |
truncateEnd(java.lang.String input,
int charCount)
Truncates
input to first charCount characters and appends "...". |
static java.lang.String |
truncateStart(java.lang.String input)
|
static java.lang.String |
truncateStart(java.lang.String input,
int charCount)
Truncates
input to last charCount characters and prepends "...". |
public static final int INDENT_COUNT
public static final java.lang.String NL
public static final java.lang.String FILE_SEP
public static final java.lang.String PATH_SEP
public static final java.lang.String USER_DIR_KEY
public static final java.lang.String USER_DIR
public static final int TRUNCATE_GREATER_THAN
public static final char TOKEN_DELIMITTER
public static final char EN_DASH
public static final char NON_BREAKING_WHITE_SPACE
public static final java.lang.Integer ZERO
public static java.util.List<java.lang.String> splitCommaSeparatedTokens(java.lang.String input)
input
is null
or empty, returns empty collection.public static java.util.List<java.lang.String> splitCharSeparatedTokens(java.lang.String input, char c)
c
-separated string into a list of non-empty tokens. If input
is null or empty, returns empty collection.
If you work with XML text and want to split text content to lines, use
splitLines(String, boolean)
instead.
public static java.util.List<java.lang.String> splitLines(java.lang.String input, boolean compact)
input
and adds them to the
result to return. If compact
is true, every parsed line is trimmed and in case
it is empty after trimming, that line is not added to the result.
If you work with XML text, use this method (rather than explicit
splitCharSeparatedTokens(String, char)
or
splitStringSeparatedTokens(String, String)
).
input
- compact
- whether to compact resultinput
split to individual lines.public static java.util.List<java.lang.String> splitStringSeparatedTokens(java.lang.String input, java.lang.String separator)
c
-separated string into a list of non-empty tokens. If input
is null or empty, returns empty collection.
If you work with XML text and want to split text content to lines, use
splitLines(String, boolean)
instead.
public static java.lang.String concatCharSeparatedTokens(java.lang.String separator, boolean delimitTokens, java.util.List<java.lang.String> tokens)
tokens
with the separator
string between consecutive
ones, and returns the resulting string. If tokens
is null or empty, returns
empty string.
This method is useful to create a line for e.g. comma separated file format, or for logging and debugging with any desired separation (e.g. " | ", " / ").
separator
- separator string; if null, considered as empty string.delimitTokens
- whether to delimit tokens; set to true if any token may contain
separator
as substring, in which case each token will be enclosed by
one character 34 at its start, and one at its end.tokens
- tokens to concatenate.public static java.lang.String fillString(int count, char ch)
count
of characters c
.count
- number of characters.ch
- the character.public static java.lang.String truncateEnd(java.lang.String input)
public static java.lang.String truncateEnd(java.lang.String input, int charCount)
input
to first charCount
characters and appends "...". If
input
is null or empty, returns empty string. If charCount
is
greater than the input
length, returns input
as is.public static java.lang.String truncateStart(java.lang.String input)
public static java.lang.String truncateStart(java.lang.String input, int charCount)
input
to last charCount
characters and prepends "...". If
input
is null or empty, returns empty string. If charCount
is
greater than the input
length, returns input
as is.public static java.lang.String getIndentSpaces(int count)
INDENT_COUNT
.public static java.lang.String getNonBreakingSpaces(int count)
count
non-breaking spaces.public static boolean hasContent(java.lang.String value)
value
is not null, and the trimmed content is not empty.public static java.lang.String capitalise(java.lang.String input)
in
is null or empty string, returns empty string.public static java.lang.String[] sortByDecreasingLength(java.lang.String[] items)
items
sorted by decreasing length (longest first).public static <T> java.util.Map<java.lang.String,T> sortByDecreasingLength(java.util.Map<java.lang.String,T> items)
items
sorted by decreasing length of keys (longest first).public static boolean looksLikePlural(java.lang.String token)
token
looks like plural; returns false for null or empty arg.public static java.lang.Integer parseInt(java.lang.String intStr)
intStr
if it isn't null and isn't empty after trimming;
otherwise returns null.public static java.lang.Integer parseIntZero(java.lang.String intStr)
intStr
if it isn't null and isn't empty after trimming;
otherwise returns integer with value 0.public static java.lang.String null2empty(java.lang.String s)
s
is null, s
otherwise.public static <K,V> java.util.Map<K,V> createKeyValuePair(K key, V value)
public static <K,V> java.util.Set<K> getKeysByValue(java.util.Map<K,V> map, V value)
public static <K,V> K getKeyByValue(java.util.Map<K,V> map, V value)
public static java.util.Properties initPropsFromFile(java.lang.String propsFileName)
propsFileName
expected to be on the
classpath, empty properties if the file has not been found.propsFileName
- name of the properties file expected to be on the classpath.public static java.lang.String getResourceAbsPath(java.lang.String resourceName, java.lang.String detail) throws ApplicationException
resourceName
- name of the resource.detail
- optional detail to display for logging.ApplicationException
- if resource with resourceName
is not on the classpath.public static java.io.InputStream findResourceOnClasspath(java.lang.String resourceName) throws ResourceNotOnClasspathException
Note: In this project, we have set the following directories to be on the classpath: ./config, ./input, ./test/config and ./test/input.
FIXME: test
ResourceNotOnClasspathException
public static java.util.List<java.io.File> listFiles(java.io.File directory, java.io.FilenameFilter filter, boolean recurse)
directory
(and its sub-directories
if recurse
is true
), filtered with filter
.
Adapted from http://snippets.dzone.com/posts/show/1875.
directory
- filter
- recurse
- public static java.util.List<java.lang.String> splitDirAndFileNames(java.lang.String basePath, java.lang.String relPath)
relPath
, starting
immediately after the basePath
. If the last member in the path has an extension
(.extension), the name of that file is returned without extension. This is useful for
creating e.g. object structure from the structure in the file system.basePath
- relPath
string is processed after this value; if null or empty, the
whole relPath
is processed. It does not contain file
separator.relPath
- actual path that should be split; if null, or (trimmed) empty string, this method
is no-op.public static java.io.File getOutputFileRenameIfExists(java.lang.String outDirName, java.lang.String outFileName) throws ApplicationException
outDirName
/outFileName
.
Creates "user.dir"/outDirName
if it does not already exist. If the
file with outFileName
already exists, renames it by appending the system
nanotime to its name.
This method is useful when generating some output files, as it ensures that the path returned on success will be valid and a potentially existing file will have been backed up.
outDirName
- subdirectory under "user.dir" that will host outFileName
outFileName
- new file nameoutDirName
/outFileName
.ApplicationException
- if fails to create outFileName
, if fails to rename existing file
with name outFileName
public static java.lang.String getFileExtension(java.lang.String filePath)
filePath
, null
otherwise. Implementation from StackOverflowpublic static java.lang.String getFileExtensionWithDot(java.lang.String filePath)
filePath
, null otherwise.public static java.io.File getDirectory(java.lang.String dirRelPath, boolean createIfMissing)
dirName
under "user.dir".dirRelPath
- relative path of directory.createIfMissing
- whether to create dirRelPath
if currently not existing under
"user.dir"dirRelPath
under "user.dir", or
null if there was an OS-related problem that didn't allow for creation of directory.public static void copy(java.io.File src, java.io.File dst) throws java.io.IOException
java.io.IOException
public static void delete(java.io.File f)
File.delete()
that accepts null argument and returns nothing. In case
delete failed, just logs the failure.f
- potentially nullpublic static java.io.File saveToFile(java.lang.String filePath, java.lang.String content) throws java.io.IOException
content
to filePath
and logs the confirmation with
level
and return the file.
TODO: test
java.io.IOException
public static java.io.File createTempImageFile(java.lang.String dirAbsPath, java.lang.String fileName, Util.ImageFormat format, boolean deleteOnExit) throws java.io.IOException
dirAbsPath
- absolute path of the file; if null, temporary directory is used.fileName
- name of the file (witout path, without extension).format
- image format.deleteOnExit
- whether to delete the file on application exit.java.io.IOException
- if a file could not be created.public static void clearClipboard() throws ApplicationException
ApplicationException
public static void copyTextToClipboard(java.lang.String txt)
txt
to clipboard, no-op otherwise. Use this method if
you have a raw text or a well-formed HTML document.txt
- text to put to the clipboardcopyHtmlToClipboard(String)
public static void copyHtmlToClipboard(java.lang.String htmlBody)
htmlBody
into doctype and html tags to produce
a valid HTML document; no-op otherwise. Use this method if you have some markup snippet.htmlBody
- markup to put to the clipboardcopyTextToClipboard(String)
public static java.lang.String fetchTextFromClipboard() throws ApplicationException
ApplicationException
- if the data is no longer available in the clipboard in the requested flavor.public static void copyImageToClipboard(java.io.File pic)
pic
to clipboard.public static void saveImageFromClipboard(java.io.File pic) throws java.io.IOException
pic
- file where to store the imagejava.io.IOException
public static java.lang.String formatDuration(long millis)
public static void ensureNotNull(java.lang.Object arg, java.lang.String name)
public static void ensureNotEmpty(java.lang.String arg, java.lang.String name)
public static void ensureNotEmpty(java.util.Map<?,?> arg, java.lang.String name)
public static void ensureNotEmpty(java.util.Collection<?> arg, java.lang.String name)
public static void ensureNotEmpty(boolean[] arg, java.lang.String name)
public static void ensureNotEmpty(java.lang.Object[] arg, java.lang.String name)
public static void ensureContainsNoNull(java.lang.Object[] arg, java.lang.String name)
public static void ensureContainsNoNull(java.util.Collection<?> arg, java.lang.String name)
public static void ensureNotEmpty(int[] arg, java.lang.String name)
public static void logTitle(org.apache.log4j.Level level, java.lang.String... title)
title
with level
(for major steps in the application).public static void logSubtitle(org.apache.log4j.Level level, java.lang.String subtitle)
subtitle
with level
(for sub-steps in the application).public static void logCompletion(org.apache.log4j.Level level, java.lang.String text, long startMillis, boolean skipTime)
text
with level
(and if skipTime=false
, duration
since startMillis
).public static <T> void logCollection(org.apache.log4j.Level level, java.util.Collection<T> objects, java.lang.String what)
objects
.level
- logging level.objects
- objects to log.what
- title to print when objects
is not empty.public static <T,V> void logMap(org.apache.log4j.Level level, java.util.Map<T,? extends java.util.Collection<V>> objects, java.lang.String what)
objects
.level
- logging level.objects
- objects to log.what
- title to print when objects
is not empty.Copyright 2009-2016 Tatjana (Tanja) Kostic