📄 util.java
字号:
* Returns full path to the file stored in the demo directory of JRobin
* @param filename Partial path to the file stored in the demo directory of JRobin
* (just name and extension, without parent directories)
* @return Full path to the file
*/
public static String getJRobinDemoPath(String filename) {
String demoDir = getJRobinDemoDirectory();
if(demoDir != null) {
return demoDir + filename;
}
else {
return null;
}
}
static boolean sameFilePath(String path1, String path2) throws IOException {
File file1 = new File(path1);
File file2 = new File(path2);
return file1.getCanonicalPath().equals(file2.getCanonicalPath());
}
static int getMatchingDatasourceIndex(RrdDb rrd1, int dsIndex, RrdDb rrd2) throws IOException {
String dsName = rrd1.getDatasource(dsIndex).getDsName();
try {
return rrd2.getDsIndex(dsName);
} catch (RrdException e) {
return -1;
}
}
static int getMatchingArchiveIndex(RrdDb rrd1, int arcIndex, RrdDb rrd2)
throws IOException {
Archive archive = rrd1.getArchive(arcIndex);
String consolFun = archive.getConsolFun();
int steps = archive.getSteps();
try {
return rrd2.getArcIndex(consolFun, steps);
} catch (RrdException e) {
return -1;
}
}
static String getTmpFilename() throws IOException {
return File.createTempFile("JROBIN_", ".tmp").getCanonicalPath();
}
static final String ISO_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; // ISO
/**
* Creates GregorianCalendar object from a string. The string should represent
* either a long integer (UNIX timestamp in seconds without milliseconds,
* like "1002354657") or a human readable date string in the format "yyyy-MM-dd HH:mm:ss"
* (like "2004-02-25 12:23:45").
* @param timeStr Input string
* @return GregorianCalendar object
*/
public static GregorianCalendar getGregorianCalendar(String timeStr) {
// try to parse it as long
try {
long timestamp = Long.parseLong(timeStr);
return Util.getGregorianCalendar(timestamp);
} catch (NumberFormatException e) { }
// not a long timestamp, try to parse it as data
SimpleDateFormat df = new SimpleDateFormat(ISO_DATE_FORMAT);
df.setLenient(false);
try {
Date date = df.parse(timeStr);
return Util.getGregorianCalendar(date);
} catch (ParseException e) {
throw new IllegalArgumentException("Time/date not in " + ISO_DATE_FORMAT +
" format: " + timeStr);
}
}
/**
* Various DOM utility functions
*/
public static class Xml {
public static Node[] getChildNodes(Node parentNode) {
return getChildNodes(parentNode, null);
}
public static Node[] getChildNodes(Node parentNode, String childName) {
ArrayList nodes = new ArrayList();
NodeList nodeList = parentNode.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (childName == null || node.getNodeName().equals(childName)) {
nodes.add(node);
}
}
return (Node[]) nodes.toArray(new Node[0]);
}
public static Node getFirstChildNode(Node parentNode, String childName) throws RrdException {
Node[] childs = getChildNodes(parentNode, childName);
if (childs.length > 0) {
return childs[0];
}
throw new RrdException("XML Error, no such child: " + childName);
}
public static boolean hasChildNode(Node parentNode, String childName) {
Node[] childs = getChildNodes(parentNode, childName);
return childs.length > 0;
}
// -- Wrapper around getChildValue with trim
public static String getChildValue( Node parentNode, String childName ) throws RrdException {
return getChildValue( parentNode, childName, true );
}
public static String getChildValue( Node parentNode, String childName, boolean trim ) throws RrdException {
NodeList children = parentNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equals(childName)) {
return getValue(child, trim);
}
}
throw new RrdException("XML Error, no such child: " + childName);
}
// -- Wrapper around getValue with trim
public static String getValue(Node node) {
return getValue( node, true );
}
public static String getValue(Node node, boolean trimValue ) {
String value = null;
Node child = node.getFirstChild();
if(child != null) {
value = child.getNodeValue();
if( value != null && trimValue ) {
value = value.trim();
}
}
return value;
}
public static int getChildValueAsInt(Node parentNode, String childName) throws RrdException {
String valueStr = getChildValue(parentNode, childName);
return Integer.parseInt(valueStr);
}
public static int getValueAsInt(Node node) {
String valueStr = getValue(node);
return Integer.parseInt(valueStr);
}
public static long getChildValueAsLong(Node parentNode, String childName) throws RrdException {
String valueStr = getChildValue(parentNode, childName);
return Long.parseLong(valueStr);
}
public static long getValueAsLong(Node node) {
String valueStr = getValue(node);
return Long.parseLong(valueStr);
}
public static double getChildValueAsDouble(Node parentNode, String childName) throws RrdException {
String valueStr = getChildValue(parentNode, childName);
return Util.parseDouble(valueStr);
}
public static double getValueAsDouble(Node node) {
String valueStr = getValue(node);
return Util.parseDouble(valueStr);
}
public static boolean getChildValueAsBoolean(Node parentNode, String childName) throws RrdException {
String valueStr = getChildValue(parentNode, childName);
return Util.parseBoolean(valueStr);
}
public static boolean getValueAsBoolean(Node node) {
String valueStr = getValue(node);
return Util.parseBoolean(valueStr);
}
public static Element getRootElement(InputSource inputSource) throws RrdException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputSource);
return doc.getDocumentElement();
} catch (ParserConfigurationException e) {
throw new RrdException(e);
} catch (SAXException e) {
throw new RrdException(e);
}
}
public static Element getRootElement(String xmlString) throws RrdException, IOException {
return getRootElement(new InputSource(new StringReader(xmlString)));
}
public static Element getRootElement(File xmlFile) throws RrdException, IOException {
Reader reader = null;
try {
reader = new FileReader(xmlFile);
return getRootElement(new InputSource(reader));
}
finally {
if(reader != null) {
reader.close();
}
}
}
}
private static Date lastLap = new Date();
/**
* Function used for debugging purposes and performance bottlenecks detection.
* Probably of no use for end users of JRobin.
* @return String representing time in seconds since last
* <code>getLapTime()</code> method call.
*/
public static String getLapTime() {
Date newLap = new Date();
double seconds = (newLap.getTime() - lastLap.getTime()) / 1000.0;
lastLap = newLap;
return "[" + seconds + " sec]";
}
/**
* Returns the root directory of the JRobin distribution. Useful in some demo applications,
* probably of no use anywhere else.<p>
*
* The function assumes that all JRobin .class files are placed under
* the <root>/classes subdirectory and that all jars (libraries) are placed in the
* <root>/lib subdirectory (the original JRobin directory structure).<p>
*
* @return absolute path to JRobin's home directory
*/
public static String getJRobinHomeDirectory() {
String className = Util.class.getName().replace('.', '/');
String uri = Util.class.getResource("/" + className + ".class").toString();
if(uri.startsWith("file:/")) {
uri = uri.substring(6);
File file = new File(uri);
// let's go 5 steps backwards
for(int i = 0; i < 5; i++) {
file = file.getParentFile();
}
uri = file.getAbsolutePath();
}
else if(uri.startsWith("jar:file:/")) {
uri = uri.substring(10, uri.lastIndexOf('!'));
File file = new File(uri);
// let's go 2 steps backwards
for(int i = 0; i < 2; i++) {
file = file.getParentFile();
}
uri = file.getAbsolutePath();
}
else {
uri = null;
}
return uri;
}
/**
* Compares two doubles, but returns true if x = y = Double.NaN
* @param x First double
* @param y Second double
* @return true, if doubles are equal, false otherwise.
*/
public static boolean equal(double x, double y) {
if(Double.isNaN(x) && Double.isNaN(y)) {
return true;
}
else {
return x == y;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -