public class Util extends Object
Constructor and Description |
---|
Util() |
Modifier and Type | Method and Description |
---|---|
static String |
calculateSHA1(File file)
Calculate the SHA-1 digest of a file and return it as an
upper-case hexadecimal string.
|
static Line2D |
clipLineToRectangle(Line2D line,
Rectangle2D r)
Clips a line to a supplied rectangle.
|
static void |
downloadUrlToFile(String url,
Path outputPath)
Downloads data from a URL and saves it to a file.
|
static Rectangle2D |
envelope(Collection<Point2D> points)
Returns the rectangular envelope for the supplied points.
|
static Path |
getAppDataDirectory()
Return a directory where PuffinPlot can store files for "internal"
use.
|
static DecimalFormatSymbols |
getDecimalFormatSymbols()
Return symbols for formatting decimal numbers for on-screen display.
|
static BitSet |
numberRangeStringToBitSet(String input,
int limit)
Converts a string specification of number ranges to a corresponding
BitSet . |
static double |
parseDoubleSafely(String s)
A wrapper around
Double.parseDouble which returns
a default value of 0 if the supplied string cannot
be parsed. |
static boolean |
runningOnOsX()
Reports whether the JVM is running on Mac OS X.
|
static Line2D |
scaleLine(Line2D line,
double scale)
Scales a line around its midpoint.
|
static Double |
tryToParseDouble(Component parentWindow,
String s)
Try to parse a string to a Double object; if a
NumberFormatException is thrown, show an error dialog and return null.
|
static Integer |
tryToParseInteger(Component parentWindow,
String s)
Try to parse a string to an Integer object; if a
NumberFormatException is thrown, show an error dialog and return null.
|
static void |
zipDirectory(Path dir,
Path zipFile)
Create a zip file from the contents of a directory.
|
public static BitSet numberRangeStringToBitSet(String input, int limit)
Converts a string specification of number ranges to a corresponding
BitSet
. The specification is of the form commonly encountered
in Print dialog boxes: a sequence of comma-separated units. Each unit
can be either an integer (meaning that the corresponding item should
be selected) or two integers separated by a hyphen (-) character
(meaning that all items in the corresponding range should be
selected). Example inputs and outputs are shown below.
1 | 1 |
1,3 | 101 |
4-6 | 000111 |
4-6,8-10,10,11,15-16 | 0001110111100011 |
1-4,3-5,10,12-14,17 | 11111000010111001 |
Note that the range specifications are one-based (the first
item is specified by 1, not 0) but the BitSet
output
is zero-based.
Since an error in the specification string might result in an impractically large bitset, this method also takes a limit argument; no bits will be set beyond the specified limit.
input
- a specification stringlimit
- upper limit for bits to set; limit-1
will be the
highest possible set bitpublic static Line2D clipLineToRectangle(Line2D line, Rectangle2D r)
line
- a line (null if none)r
- a clipping rectangle (null if none)public static double parseDoubleSafely(String s)
Double.parseDouble
which returns
a default value of 0
if the supplied string cannot
be parsed. (Double.parseDouble
, in contrast, throws
a NumberFormatException
.)s
- A string representation of a floating-point number.double
with a value corresponding to the
supplied string, or 0
if the string could not be
interpreted as a floating-point number.public static Path getAppDataDirectory() throws IOException
IOException
- if there was an error creating or finding the
directorypublic static Rectangle2D envelope(Collection<Point2D> points)
points
- a set of pointspublic static Line2D scaleLine(Line2D line, double scale)
line
- the line to scalescale
- the scale factorpublic static void zipDirectory(Path dir, Path zipFile) throws IOException
dir
- the directory to archivezipFile
- the path of the zip file to createIOException
- if there was an exception while reading the
directory contents or writing the zip filepublic static String calculateSHA1(File file) throws IOException, NoSuchAlgorithmException
file
- the file for which to calculate the digestIOException
- if there is an error reading the fileNoSuchAlgorithmException
- if the SHA-1 algorithm is not availablepublic static void downloadUrlToFile(String url, Path outputPath) throws IOException
url
- the URL to use as a data sourceoutputPath
- the file to which to save the data found at the URLIOException
- if an error occurs while downloading
or savingpublic static boolean runningOnOsX()
true
if this PuffinApp is running on Mac OS Xpublic static Integer tryToParseInteger(Component parentWindow, String s)
parentWindow
- parent for the error dialogs
- string to parsepublic static Double tryToParseDouble(Component parentWindow, String s)
parentWindow
- parent for the error dialogs
- string to parsepublic static DecimalFormatSymbols getDecimalFormatSymbols()
Copyright © 2022. All rights reserved.