/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.model;

/**
 * This class contains miscellaneous constants for the data model. The class
 * might be extended to create subclasses that add further constants by using
 * the following declaration:
 * 

* public static final JSCConstants NEW_CONSTANT = new JSCConstants("NEW_CONSTANT"); * * @author Markus Kraetzig */ public class JSCConstants { private final String name; /** * Debug flag that can be set with the DEBUG property. */ public static boolean DEBUG = false; /** * Missing value. */ public static final Double NaN = new Double(Double.NaN); /** * Missing value as long. */ public static final long NaNLongBits = Double.doubleToLongBits(Double.NaN); /** * Positive infinity. */ public static final Double POS_INF = new Double(Double.POSITIVE_INFINITY); /** * Negative infinity. */ public static final Double NEG_INF = new Double(Double.NEGATIVE_INFINITY); protected JSCConstants(String name) { this.name = name; } /** * Checks whether name is a valid variable name and throws an * exception of not. *

* Important:
* null is allowed as an argument because this is often * convenient. In this case null is returned. * * @param name * the string to check * @return trimmed name if ok or null if * name was null * @throws IllegalArgumentException * with error string * if (JSCConstants.isValidName(name) != null) */ public static String checkNameThrowEx(String name) { if (name == null) return null; name = name.trim(); String error = null; if ((error = JSCConstants.isValidName(name)) != null) throw new IllegalArgumentException(error); return name; } /** * Override prevention method to enforce a.equals(b) if and * only if a==b. * * @param o * object to compare with * @return true if equal, false otherwise */ public final boolean equals(Object o) { return super.equals(o); } /** * Override prevention method invokes super method. * * @return hash code */ public final int hashCode() { return super.hashCode(); } /** * Checks whether name conforms to the rules for valid * variable names. Valid names must not be null or empty and * must begin with either a letter or '_' . They may contain letters, digits * and '_', but no other characters or whitespace. * * @param name * the name to be checked * @return null if ok, error message otherwise */ public static String isValidName(String name) { if (name == null) return "Name cannot be null."; if (name.trim().length() == 0) return "Name cannot be empty."; char c = name.charAt(0); if (!(Character.isLetter(c) || c == '_')) return "\"" + name + "\" does not start with a letter or '_'."; for (int i = 1; i < name.length(); i++) { c = name.charAt(i); if (!(Character.isLetter(c) || c == '_' || Character.isDigit(c))) return "\"" + name + "\" contains invalid characters."; } return null; } /** * Gets string representation of this object. * * @return a string */ public String toString() { return name; } /** * Reads out the system property java.io.tempdir. * * @return the directory name of the temporary directory * @throws RuntimeException * if property is empty */ public static String getSystemTemp() { String temp = System.getProperty("java.io.tmpdir"); if (temp != null) return temp; throw new RuntimeException("Property java.io.tmpdir is empty."); } /** * Reads out the system property user.dir. * * @return the directory name of the user directory * @throws RuntimeException * if property is empty */ public static String getUserDir() { String userDir = System.getProperty("user.dir"); if (userDir != null) return userDir; throw new RuntimeException("Property user.dir is empty."); } /** * Reads out the system property home. * * @return the directory name of the user's home directory * @throws RuntimeException * if property is empty */ public static String getUserHome() { String userDir = System.getProperty("user.home"); if (userDir != null) return userDir; throw new RuntimeException("Property user.home is empty."); } }

Ads help maintain this website.