Initial checkin of Java Util classes and the Java Wrapper to WebShell
Util docs: http://www.mozilla.org/projects/blackwood/java-util/ Java Wrapper to WebShell docs: http://www.mozilla.org/projects/blackwood/webclient/ git-svn-id: svn://10.0.0.236/trunk@41569 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
52
mozilla/java/util/classes/Makefile.win
Normal file
52
mozilla/java/util/classes/Makefile.win
Normal file
@@ -0,0 +1,52 @@
|
||||
#!nmake
|
||||
#
|
||||
# The contents of this file are subject to the Mozilla Public License
|
||||
# Version 1.0 (the "License"); you may not use this file except in
|
||||
# compliance with the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS IS"
|
||||
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
# the License for the specific language governing rights and limitations
|
||||
# under the License.
|
||||
#
|
||||
# The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
#
|
||||
# The Initial Developer of the Original Code is Sun Microsystems,
|
||||
# Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
# Microsystems, Inc. All Rights Reserved.
|
||||
|
||||
IGNORE_MANIFEST=1
|
||||
#//------------------------------------------------------------------------
|
||||
#//
|
||||
#// Makefile to build the java util classes
|
||||
#//
|
||||
#//------------------------------------------------------------------------
|
||||
|
||||
|
||||
#//------------------------------------------------------------------------
|
||||
#//
|
||||
#// Specify the depth of the current directory relative to the
|
||||
#// root of NS
|
||||
#//
|
||||
#//------------------------------------------------------------------------
|
||||
DEPTH= ..\..\..
|
||||
|
||||
# PENDING(edburns): find out where this should really get defined
|
||||
JAVA_OR_NSJVM=1
|
||||
NO_CAFE=1
|
||||
|
||||
include <$(DEPTH)\config\config.mak>
|
||||
|
||||
JAR_MJUTIL_CLASSES = org\mozilla\util
|
||||
|
||||
!ifdef JAVA_OR_NSJVM
|
||||
JDIRS = $(JAR_MJUTIL_CLASSES)
|
||||
!endif
|
||||
|
||||
MJUTIL_JAR_NAME=mjutil$(VERSION_NUMBER).jar
|
||||
|
||||
JAVAC_PROG=$(JDKHOME)\bin\javac
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
|
||||
126
mozilla/java/util/classes/org/mozilla/util/Assert.java
Normal file
126
mozilla/java/util/classes/org/mozilla/util/Assert.java
Normal file
@@ -0,0 +1,126 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
|
||||
/**
|
||||
* The <B>Assert</B> class provides convenient means for condition testing.
|
||||
* Class methods in the <B>Assert</B> class can be used to verify certain
|
||||
* conditions and to raise exceptions if the conditions are not met.
|
||||
* Assertions are particularly useful during the development of a
|
||||
* project and may be turned off in production code. Turning off
|
||||
* exceptions causes <B>Assert</B> not to raise exceptions when conditions
|
||||
* are not met.<P>
|
||||
*
|
||||
* Typical usage:
|
||||
* <BLOCKQUOTE><TT>
|
||||
* Assert.assert(Assert.enabled && mytest(), "my test failed");
|
||||
* </TT></BLOCKQUOTE><P>
|
||||
*
|
||||
* Such usage prevents <TT>mytest()</TT> from being executed if assertions
|
||||
* are disabled. This techinique allows assertions to do time-consuming
|
||||
* checks (such as <TT>myArray.containsObject(myObject)</TT>) without
|
||||
* worrying about them impacting the performance of the final release.<P>
|
||||
*
|
||||
* If you know that the condition being tested is fast, you may omit the
|
||||
* <I>enabled</I> flag:
|
||||
* <BLOCKQUOTE><TT>
|
||||
* Assert.assert(myValue != null);
|
||||
* </TT></BLOCKQUOTE><P>
|
||||
*
|
||||
* Note that even in this second usage, if assertions are disabled,
|
||||
* the <B>assert()</B> method will do nothing.<P>
|
||||
*
|
||||
* Another usage that is more efficient but bulkier:
|
||||
* <BLOCKQUOTE><TT>
|
||||
* if (Assert.enabled && Assert.assert(mytest(), "my test failed"));
|
||||
* </TT></BLOCKQUOTE><P>
|
||||
*
|
||||
* Such usage prevents not only <TT>mytest()</TT> from being executed if
|
||||
* assertions are disabled but also the assert method itself, and some
|
||||
* compilers can remove the entire statement if assertions are disabled.<P>
|
||||
*
|
||||
* <B>Assert</B> is intended for general condition and invariant testing;
|
||||
* for parameter testing, use <B>ParameterCheck</B>.
|
||||
*
|
||||
* @see ParameterCheck
|
||||
*/
|
||||
final public class Assert extends Object {
|
||||
|
||||
//
|
||||
// Public Constants
|
||||
//
|
||||
|
||||
/**
|
||||
* True if failed assertions should raise exceptions, or false if they
|
||||
* should do nothing.
|
||||
*/
|
||||
static public boolean enabled = true;
|
||||
|
||||
|
||||
/**
|
||||
* Private constructor prevents instances of this class from being created.
|
||||
*/
|
||||
private Assert() {}
|
||||
|
||||
/**
|
||||
* Sets <I>enabled</I> to <I>newEnabled</I>.
|
||||
*
|
||||
* @param newEnabled true if and only if assertions should be
|
||||
* enabled
|
||||
*/
|
||||
static public void setEnabled(boolean newEnabled) {
|
||||
enabled = newEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws <B>AssertionFailureException</B> with a message of <I>message</I>
|
||||
* if <I>enabled</I> is true and <I>test</I> is false; otherwise, does nothing.
|
||||
*
|
||||
* @param test value to verify
|
||||
* @param message message of exception thrown if <I>test</I> is false
|
||||
* @exception AssertionFailureException if <I>test</I> is false
|
||||
* @return true
|
||||
*/
|
||||
static public boolean assert (boolean test, String message) {
|
||||
if (enabled && !test) {
|
||||
throw new AssertionFailureException (message);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws <B>AssertionFailureException</B> if <I>enabled</I> is true and
|
||||
* <I>test</I> is false; otherwise, does nothing.
|
||||
*
|
||||
* @param test value to verify
|
||||
* @exception AssertionFailureException if <I>test</I> is false
|
||||
* @return true
|
||||
*/
|
||||
static public boolean assert (boolean test) {
|
||||
if (enabled && !test) {
|
||||
throw new AssertionFailureException ();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // End of class Assert
|
||||
@@ -0,0 +1,41 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
// CUAssertionFailureException.java
|
||||
//
|
||||
// $Id: AssertionFailureException.java,v 1.1 1999-07-30 01:02:57 edburns%acm.org Exp $
|
||||
//
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
/**
|
||||
* This exception is thrown when an <B>Assert.assert()</B> fails.
|
||||
*
|
||||
* @see Assert
|
||||
*/
|
||||
public class AssertionFailureException
|
||||
extends RuntimeException {
|
||||
|
||||
|
||||
public AssertionFailureException () { super(); }
|
||||
public AssertionFailureException (String s) { super("\n" + s + "\n"); }
|
||||
|
||||
|
||||
} // End of class AssertionFailureException
|
||||
515
mozilla/java/util/classes/org/mozilla/util/Debug.java
Normal file
515
mozilla/java/util/classes/org/mozilla/util/Debug.java
Normal file
@@ -0,0 +1,515 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <P>
|
||||
* <B>Debug</B> Vendor of debug "filter" strings set & queried by clients. This allows
|
||||
* conditional code to only be executed if a certain filter is set.<BR>
|
||||
*
|
||||
* <I>Example of use from <B>JAG</B> days:<BR>
|
||||
* JDApplication allows the setting of filters from the commandline at app startup time,
|
||||
* and Log supports printing debug messages only when a specific filter string has
|
||||
* been set.</I>
|
||||
*
|
||||
* <P>
|
||||
*
|
||||
* Alternatively, users can use the System Properties table to define
|
||||
* filter strings at runtime: <P>
|
||||
*
|
||||
* <CODE>
|
||||
* java -DDebug.filters=String ... <P>
|
||||
* </CODE>
|
||||
*
|
||||
* where String is comma (,) separated list of constants <B>WITH NO
|
||||
* WHITESPACE</B>. ie "AXISPANEL_PAINT,BODYPANEL_PAINT". <P>
|
||||
*
|
||||
*
|
||||
* All filters are case-sensitive.
|
||||
*
|
||||
* This class also provides various timing routines.
|
||||
*
|
||||
* </P>
|
||||
* @author Keith Bernstein
|
||||
* @version $Id: Debug.java,v 1.1 1999-07-30 01:02:57 edburns%acm.org Exp $ */
|
||||
|
||||
public class Debug extends Object {
|
||||
static public final String HELP_FILTER_STRING = "HELP";
|
||||
static public final String ALL_FILTER_STRING = "ALL";
|
||||
static public final String TIMING_FILTER_STRING = "TIMING";
|
||||
static public final String PROGRESS_FILTER_STRING = "PROGRESS";
|
||||
|
||||
/** the name in the properties table where we look for filter strings */
|
||||
static final String PROPERTY_NAME = "Debug.filters";
|
||||
static final String SEP = ",";
|
||||
|
||||
static boolean showedColumnTitleHelp = false;
|
||||
// PENDING(kbern) When we start using JDK 1.2, we should change this filters Vector to a
|
||||
// "Set" in the new collection API for increased access and verification times.
|
||||
static Vector filters = null;
|
||||
static long initializationTime = new Date().getTime();
|
||||
static long lastTime = initializationTime;
|
||||
static long startTime = 0L;
|
||||
static long lapTime = 0L;
|
||||
|
||||
static {
|
||||
/** This code adds to the filters Vector from the
|
||||
* System.Properties variable named by PROPERTY_NAME
|
||||
* The value of this variable must be a SEP separeted list
|
||||
* and contain *NO WHITESPACE*!!
|
||||
*/
|
||||
String flags = System.getProperty(PROPERTY_NAME);
|
||||
|
||||
if (null != flags) {
|
||||
Vector flagVector = Utilities.vectorFromString(flags, SEP);
|
||||
int i, size = (null != flagVector) ? flagVector.size() : 0;
|
||||
String curFlag;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
curFlag = (String) flagVector.elementAt(i);
|
||||
Debug.addFilter(curFlag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a debug filter, for future consumption by this class, as well as other
|
||||
* utility classes, like Log, etc.
|
||||
*
|
||||
* Virtually all "filters" are simply developer-meaningful strings which will be
|
||||
* tested within developer code, to conditionally execute code.
|
||||
*
|
||||
* There are some predefined filters, which this class actually does something with
|
||||
* (besides simply handing it back when asked for).
|
||||
*
|
||||
* The predefined filters are:
|
||||
* HELP
|
||||
* ALL
|
||||
* TIMING
|
||||
*
|
||||
* If the "HELP" filter is found, this class will print a message displaying the
|
||||
* predefined filters and what they do.
|
||||
*
|
||||
* If the "ALL" filter is found, this class will return "true" when queried for the
|
||||
* existence of <B>any</B> filter, effectively turning on all debugging tests.
|
||||
* This is useful for both quick and easy tests, as well as to find forgotten debug
|
||||
* filters (see Log class for more info on this).
|
||||
*
|
||||
* If the "TIMING" filter is specified, then the time routines will always print
|
||||
* their info, regardless of what filter string is passed to them. This is useful
|
||||
* for turning on all timing tests.
|
||||
* */
|
||||
static public synchronized void addFilter(String aFilter) {
|
||||
if (aFilter != null) {
|
||||
if (filters == null) {
|
||||
Log.log("Debugging has been enabled.");
|
||||
Log.log("os name: "+System.getProperty("os.name"));
|
||||
Log.log("os version: "+System.getProperty("os.version"));
|
||||
Log.log("Java version: "+System.getProperty("java.version"));
|
||||
Log.log("Java home: "+System.getProperty("java.home"));
|
||||
Log.log("User home: "+System.getProperty("user.home"));
|
||||
filters = new Vector(1);
|
||||
}
|
||||
Log.log("Adding debug filter: "+aFilter);
|
||||
if (!filters.contains(aFilter)) {
|
||||
filters.addElement(aFilter);
|
||||
}
|
||||
if (aFilter.equalsIgnoreCase(Debug.HELP_FILTER_STRING)) {
|
||||
Log.log("Set one or more debug filters (usually possible from the commandling), then simply wrap your debug code in a test for a particular debug filter, and only execute the code if that filter has been set.");
|
||||
Log.log("Predifined debug filters:");
|
||||
Log.log(" HELP [prints this info]");
|
||||
Log.log(" ALL [causes all calls to \"containsFilter()\" to return \"true\", effectively enabling all filters]");
|
||||
Log.log(" TIMING [causes all timing messages generated by this class to print]");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("null filter passed to addFilter()");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified filter from the list of filters.
|
||||
*/
|
||||
static public synchronized void removeFilter(String aFilter) {
|
||||
if (filters != null) {
|
||||
filters.removeElement(aFilter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all filters from the list of filters.
|
||||
*/
|
||||
static public synchronized void removeAllFilters() {
|
||||
if (filters != null) {
|
||||
filters.removeAllElements();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for any "filter" with the specified prefix.
|
||||
*
|
||||
* "ALL" is not considered to be a match.
|
||||
*
|
||||
* This method works in (normally) O-n time (if no match).
|
||||
*/
|
||||
|
||||
static public synchronized boolean containsFilterWithPrefix(String aFilterPrefix) {
|
||||
boolean returnValue = false;
|
||||
|
||||
if (Debug.filters != null) {
|
||||
Enumeration filterEnumeration = Debug.filters.elements();
|
||||
|
||||
while (!returnValue && filterEnumeration.hasMoreElements()) {
|
||||
String aFilter = (String)filterEnumeration.nextElement();
|
||||
|
||||
if (aFilter.startsWith(aFilterPrefix)) {
|
||||
returnValue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Funnel-point method, which takes a filter and an "allFiltersgMatchThisString" string.
|
||||
* See the javadoc for "containsFilter(String aFilter)" for the rest of what this method
|
||||
* does.
|
||||
*
|
||||
* This method works in (normally) O-1 time.
|
||||
*
|
||||
* NOTE: The "allFiltersgMatchThisString" parameter can be used to conditionally execute
|
||||
* code while preventing the "ALL" filter from having any effect. So, the conditional code
|
||||
* should use the test "if (Debug.containsFilter("SomeFilter", ""))" to see if a filter
|
||||
* has been set, and not get a false positive from the "ALL" filter.
|
||||
*/
|
||||
static public synchronized boolean containsFilter(String aFilter, String allFiltersgMatchThisString) {
|
||||
if (((aFilter != null) && ((filters != null) && filters.contains(aFilter)))
|
||||
|| ((filters != null) && filters.contains(allFiltersgMatchThisString))
|
||||
|| (aFilter != null) && aFilter.equals("")) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if any of the following conditions are true:
|
||||
* 1. The specified filter is contained in the current filter set.
|
||||
* 2. The "ALL" filter is set (this is true even if the passed-in filter is "null").
|
||||
* 3. The passed-in filter is ""... as that filter is considered
|
||||
* to <B>always</B> be a match, regardless of the current filter set.
|
||||
* 4. The passed in filter is "null" and the "ALL" filter is currently set.
|
||||
* Otherwise returns false.
|
||||
*
|
||||
* NOTE: This method, and all filtering of this class is case-sensitive.
|
||||
*/
|
||||
static public synchronized boolean containsFilter(String aFilter) {
|
||||
return Debug.containsFilter(aFilter, Debug.ALL_FILTER_STRING);
|
||||
}
|
||||
|
||||
// Only used by the timing routines, since they ignore "ALL", but pay attention to
|
||||
// "TIMING".
|
||||
static private synchronized boolean containsTimingFilter(String aFilter) {
|
||||
return Debug.containsFilter(aFilter, Debug.TIMING_FILTER_STRING);
|
||||
}
|
||||
|
||||
static private synchronized void maybeShowColumnHelp() {
|
||||
if (!showedColumnTitleHelp) {
|
||||
Log.enableTimestampPrefix(false);
|
||||
Log.logErrorMessage("************************:");
|
||||
Log.logErrorMessage("Debugging time codes:");
|
||||
Log.logErrorMessage(" etset=elapsed time since elapsed time (since the previous elapsedTime() call)");
|
||||
Log.logErrorMessage(" etsst=elapsed time since start time (since the previous startTime() call)");
|
||||
Log.logErrorMessage(" etsit=elapsed time since initialization time (typically since the program was launched)");
|
||||
Log.logErrorMessage("************************:");
|
||||
showedColumnTitleHelp = true;
|
||||
Log.enableTimestampPrefix(false);
|
||||
}
|
||||
}
|
||||
|
||||
// We don't use Log's debugging logging methods for a few reasons:
|
||||
// - They check for filters, but we've already had to check ourselves because of the
|
||||
// "TIMING" filter, so we don't want to waste time checking again.
|
||||
// - It doesn't know about "TIMING", so if the filter was null, it would assume that
|
||||
// "ALL" was set, not "TIMING".
|
||||
// - We need to add a suffix to the "baseName" of the filter that we deduce, and Log
|
||||
// doesn't want to provide such a method ('cause it would be gross :-))
|
||||
// - We don't want the time and day stamp on each message, and though we could disable
|
||||
// that around each call to Log's debugMessage stuff, that's pretty gross.
|
||||
static private synchronized void logTimingMessageString(String debugMessage, String debugFilter) {
|
||||
String filterString;
|
||||
|
||||
if (debugFilter == null) {
|
||||
if (Debug.containsFilter(Debug.TIMING_FILTER_STRING)) {
|
||||
filterString = Debug.TIMING_FILTER_STRING;
|
||||
} else {
|
||||
// Quiets compiler... If we're here, we should always be able to set it below.
|
||||
filterString = "UNMATCHED FILTER";
|
||||
}
|
||||
} else if (debugFilter.equals("")) {
|
||||
filterString = "ANY";
|
||||
} else {
|
||||
filterString = debugFilter;
|
||||
}
|
||||
String baseStr = Log.getApplicationName();
|
||||
if ( baseStr == null) {
|
||||
System.err.println("["+filterString+"]: "+ debugMessage);
|
||||
System.err.flush();
|
||||
}
|
||||
else {
|
||||
System.err.println(baseStr + "["+filterString+"]: "+ debugMessage);
|
||||
System.err.flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a timer which can be stopped using one of the "stopTiming()" methods.
|
||||
*
|
||||
* This method does not check debug filters... it always does what it's told.
|
||||
*
|
||||
* Calling this method resets the elapsed time ("lap time").
|
||||
*/
|
||||
static public synchronized void startTiming() {
|
||||
startTime = new Date().getTime();
|
||||
lapTime = startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to the "startTiming(String logMessage, String aFilter)" method, except it
|
||||
* will only show the message if the filter "TIMING" exists in Debug's filter list.
|
||||
*/
|
||||
static public synchronized void startTiming(String logMessage) {
|
||||
Debug.startTiming(logMessage, Debug.TIMING_FILTER_STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method does absolutely nothing unless:
|
||||
* 1. The specified filter is contained in the current filter set.
|
||||
* 2. The "TIMING" filter is set (this is true even if the passed-in filter is "null").
|
||||
* 3. The passed-in filter is ""... as that filter is considered
|
||||
* to <B>always</B> be a match, and so will cause this method to always work,
|
||||
* regardless of the current filter set.
|
||||
* NOTE: The "ALL" filter has no effect on timing methods.
|
||||
*
|
||||
* Otherwise, starts a timer which can be stopped using one of the "stopTiming()" methods
|
||||
* and prints out a logMessage indicating that timing has begun.
|
||||
*
|
||||
* This method may be called with a "null" logMessage. A logMessage is sometimes
|
||||
* unneccesary since the matched filter string is printed with the output anyway, and that
|
||||
* is frequently enough information.
|
||||
*
|
||||
* Calling this method resets the elapsed time ("lap time").
|
||||
*/
|
||||
static public synchronized void startTiming(String logMessage, String aFilter) {
|
||||
if (Debug.containsTimingFilter(aFilter)) {
|
||||
if (logMessage == null) {
|
||||
logMessage = "";
|
||||
} else {
|
||||
logMessage = " ["+logMessage+"]";;
|
||||
}
|
||||
Debug.maybeShowColumnHelp();
|
||||
Debug.startTiming();
|
||||
Debug.logTimingMessageString("Resetting start time at etsit of: "+Debug.formatTime(Debug.elapsedTimeSinceInitialization())+logMessage, aFilter);
|
||||
}
|
||||
}
|
||||
|
||||
static private synchronized long elapsedTime(long startTime) {
|
||||
lapTime = new Date().getTime();
|
||||
|
||||
return lapTime - startTime; // "lapTime" happens to be currTime right now!
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the elapsed time since this class was initialized.
|
||||
*
|
||||
* This method does not check debug filters... it always does what it's told.
|
||||
*
|
||||
* Calling this method resets the elapsed time ("lap time").
|
||||
*
|
||||
* This method may be called repeatedly to get "lap" times.
|
||||
*/
|
||||
static public synchronized long elapsedTimeSinceInitialization() {
|
||||
return Debug.elapsedTime(initializationTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the elapsed time since the preceeding startTiming() call,
|
||||
*
|
||||
* This method does not check debug filters... it always does what it's told.
|
||||
*
|
||||
* Calling this method resets the elapsed time ("lap time").
|
||||
*
|
||||
* This method may be called repeatedly to get "lap" times.
|
||||
*/
|
||||
static public synchronized long elapsedTimeSinceStartTime() {
|
||||
if (startTime == 0) {
|
||||
return 0L; // "startTime()" was never called.
|
||||
} else {
|
||||
return Debug.elapsedTime(startTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the elapsed time since this class was initialized.
|
||||
*
|
||||
* This method does not check debug filters... it always does what it's told.
|
||||
*
|
||||
* Calling this method resets the elapsed time ("lap time").
|
||||
*
|
||||
* This method may be called repeatedly to get "lap" times.
|
||||
*/
|
||||
static public synchronized long elapsedTimeSinceElapsedTime() {
|
||||
if (lapTime == 0) {
|
||||
return 0L; // "elapsedTimeXXX()" was never called.
|
||||
} else {
|
||||
return Debug.elapsedTime(lapTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to the "elapsedTime(String logMessage, String aFilter)" method, except it
|
||||
* will only show the message if the filter "TIMING" exists in Debug's filter list.
|
||||
*/
|
||||
static public synchronized void elapsedTime(String logMessage) {
|
||||
Debug.elapsedTime(logMessage, Debug.TIMING_FILTER_STRING);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method does absolutely nothing unless:
|
||||
* 1. The specified filter is contained in the current filter set.
|
||||
* 2. The "TIMING" filter is set (this is true even if the passed-in filter is "null").
|
||||
* 3. The passed-in filter is ""... as that filter is considered
|
||||
* to <B>always</B> be a match, and so will cause this method to always work,
|
||||
* regardless of the current filter set.
|
||||
* NOTE: The "ALL" filter has no effect on timing methods.
|
||||
*
|
||||
* Otherwise, prints the following information:
|
||||
* 1. The elapsed time since initialization of this class.
|
||||
* 2. The elapsed time since the preceeding startTiming() call.
|
||||
* 3. The "lap" time, since the last time "elapsedTime()" was called.
|
||||
* 4. A client-supplied message.
|
||||
*
|
||||
* When a filter matches, this method invokes the following methods:
|
||||
* elapsedTime()
|
||||
* elapsedTimeSinceInitialization()
|
||||
* elapsedTimeSinceElapsedTime()
|
||||
*
|
||||
* When a filter matches, calling this method resets the elapsed time ("lap time").
|
||||
*
|
||||
*
|
||||
* This method may be called with a "null" logMessage. A logMessage is sometimes
|
||||
* unneccesary since the matched filter string is printed with the output anyway, and that
|
||||
* is frequently enough information.
|
||||
*
|
||||
* This method may be called repeatedly to get "lap" times.
|
||||
*/
|
||||
static public synchronized void elapsedTime(String logMessage, String aFilter) {
|
||||
if (Debug.containsTimingFilter(aFilter)) {
|
||||
if (logMessage == null) {
|
||||
logMessage = "";
|
||||
} else {
|
||||
logMessage = " ["+logMessage+"]";;
|
||||
}
|
||||
Debug.maybeShowColumnHelp();
|
||||
Debug.logTimingMessageString("etset: "+Debug.formatTime(Debug.elapsedTimeSinceElapsedTime())+" etsst: "+Debug.formatTime(Debug.elapsedTimeSinceStartTime())+" etsit: "+Debug.formatTime(Debug.elapsedTimeSinceInitialization())+logMessage, aFilter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* format end - start to sec.millisec
|
||||
*/
|
||||
static private synchronized String formatTime(long milliseconds) {
|
||||
String d1000sStr = String.valueOf( milliseconds % 1000);
|
||||
int len = d1000sStr.length();
|
||||
return String.valueOf(milliseconds / 1000)+"."+
|
||||
"000".substring(len) + d1000sStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* format time to %4u%03u so that the decimal points will align
|
||||
*/
|
||||
private static synchronized String formatTimeAligned(long milliseconds) {
|
||||
long deltaSecs = milliseconds / 1000;
|
||||
String dSecsStr = null;
|
||||
if ( deltaSecs == 0)
|
||||
dSecsStr = " ";
|
||||
else {
|
||||
dSecsStr = String.valueOf( deltaSecs);
|
||||
int len = dSecsStr.length();
|
||||
dSecsStr = " ".substring( len) + dSecsStr;
|
||||
}
|
||||
|
||||
String d1000sStr = String.valueOf( milliseconds % 1000);
|
||||
int len = d1000sStr.length();
|
||||
return dSecsStr + "." + "000".substring(len) + d1000sStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print time since start of app, and time since the last time this method
|
||||
* was called.
|
||||
* Call this with a msg you want printed, and a filter. Then
|
||||
* run with -jsdebug filter and all these timing msgs will come out.
|
||||
*/
|
||||
static public synchronized void printTime( String msg, String aFilter) {
|
||||
if ( filters != null && filters.contains( aFilter)) {
|
||||
long curTime = System.currentTimeMillis();
|
||||
System.out.println( formatTimeAligned( curTime -
|
||||
initializationTime) + " " +
|
||||
formatTimeAligned( curTime - lastTime) + " " +
|
||||
msg);
|
||||
System.out.flush();
|
||||
lastTime = curTime;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String containing the hexadecimal hashCode of the passed in object,
|
||||
* of the form: "0x0000"
|
||||
*/
|
||||
static public synchronized String getHashCode(Object anObject) {
|
||||
String returnValue;
|
||||
|
||||
if (anObject == null) {
|
||||
returnValue = "0x0000";
|
||||
} else {
|
||||
returnValue = "0x"+Integer.toHexString(anObject.hashCode());
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String containing the class name and hexadecimal hashCode of the
|
||||
* passed in object, of the form: "fully.qualified.ClassName[0x0000]"
|
||||
*/
|
||||
static public synchronized String getNameAndHashCode(Object anObject) {
|
||||
String returnValue;
|
||||
|
||||
if (anObject == null) {
|
||||
returnValue = "<null>[0x0000]";
|
||||
} else {
|
||||
returnValue = anObject.getClass().getName()+"["+Debug.getHashCode(anObject)+"]";
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
} // End of class Debug
|
||||
229
mozilla/java/util/classes/org/mozilla/util/Log.java
Normal file
229
mozilla/java/util/classes/org/mozilla/util/Log.java
Normal file
@@ -0,0 +1,229 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <P>
|
||||
* <B>Log</B>
|
||||
* </P>
|
||||
* @author Keith Bernstein
|
||||
* @version $Id: Log.java,v 1.1 1999-07-30 01:02:57 edburns%acm.org Exp $
|
||||
*/
|
||||
|
||||
public class Log extends Object {
|
||||
static String applicationName = "APPLICATION NAME UNKNOWN [call setApplicationName() from main]";
|
||||
static String applicationVersion = "APPLICATION VERSION UNKNOWN [call setApplicationVersion() from main]";
|
||||
static String applicationVersionDate = "APPLICATION VERSION DATE UNKNOWN [call setApplicationVersionDate() from main]";
|
||||
static int showTimestampPrefix = 1;
|
||||
|
||||
/**
|
||||
* This string will be prepended to all output from this class.
|
||||
*
|
||||
* This string is usually the application name, e.g. "JavaPlan".
|
||||
*
|
||||
* It is useful because it includes a timestamp and the base string (usually the
|
||||
* application name), so that if two apps are launched from the same commandline
|
||||
* (for example), with "&", or one app invokes another, it is clear who the message
|
||||
* is comming from. The time stamp can help see how long operations took, etc.
|
||||
*
|
||||
* If you don't want your messages prefixed with anything (no time stamp or name), you must
|
||||
* pass "null" for "applicationName".
|
||||
*
|
||||
* If this method is never called, "applicationName" will default to:
|
||||
* "APPLICATION NAME UNKNOWN [call setApplicationName from main]"
|
||||
*
|
||||
*/
|
||||
static public synchronized void setApplicationName(String newApplicationName) {
|
||||
// It's really unfortunate that we can't discover this dynamically.
|
||||
applicationName = newApplicationName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the applicationName set by "setApplicationName()"
|
||||
*
|
||||
*/
|
||||
static public synchronized String getApplicationName() {
|
||||
// It's really unfortunate that we can't discover this dynamically.
|
||||
return applicationName;
|
||||
}
|
||||
|
||||
static public synchronized void setApplicationVersion(String newApplicationVersion) {
|
||||
// It's really unfortunate that we can't discover this dynamically.
|
||||
applicationVersion = newApplicationVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the applicationVersion set by "setApplicationVersion()"
|
||||
*
|
||||
*/
|
||||
static public synchronized String getApplicationVersion() {
|
||||
// It's really unfortunate that we can't discover this dynamically.
|
||||
return applicationVersion;
|
||||
}
|
||||
|
||||
static public synchronized void setApplicationVersionDate(String newApplicationVersionDate) {
|
||||
// It's really unfortunate that we can't discover this dynamically.
|
||||
applicationVersionDate = newApplicationVersionDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the applicationVersion set by "setApplicationVersionDate()"
|
||||
*
|
||||
*/
|
||||
static public synchronized String getApplicationVersionDate() {
|
||||
// It's really unfortunate that we can't discover this dynamically.
|
||||
return applicationVersionDate;
|
||||
}
|
||||
|
||||
static protected synchronized Object applicationNameWithTimeStamp() {
|
||||
String timestampPrefix;
|
||||
|
||||
if (showTimestampPrefix > 0) {
|
||||
timestampPrefix = "["+new Date()+"] ";
|
||||
} else {
|
||||
timestampPrefix = "";
|
||||
}
|
||||
if (applicationName != null) {
|
||||
return timestampPrefix+Log.getApplicationName();
|
||||
} else {
|
||||
return timestampPrefix;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Incrememnts or decrements whether or not to prefix logged messages with a timestamp.
|
||||
* Two (or "n") calls with a value of "false" must be followed by two (or "n") calls
|
||||
* with a value of "true" to reenable timestamp prefixes.
|
||||
*/
|
||||
static public synchronized void enableTimestampPrefix(boolean enable) {
|
||||
if (enable) {
|
||||
showTimestampPrefix++;
|
||||
} else {
|
||||
showTimestampPrefix--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes "infoMessage" to stdout, prefixed by the string "ApplicationName: "
|
||||
*/
|
||||
static public synchronized void log(Object infoMessage) {
|
||||
System.out.println(Log.applicationNameWithTimeStamp()+": "+infoMessage);
|
||||
System.out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes "errorMessage" to stderr, prefixed by the string "ApplicationName error: "
|
||||
*/
|
||||
static public synchronized void logError(Object errorMessage) {
|
||||
System.err.println(Log.applicationNameWithTimeStamp()+" error: "+errorMessage);
|
||||
System.err.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes "errorMessage" to stderr, prefixed by the string "ApplicationName: "
|
||||
*/
|
||||
static public synchronized void logErrorMessage(Object errorMessage) {
|
||||
System.err.println(Log.applicationNameWithTimeStamp()+": "+errorMessage);
|
||||
System.err.flush();
|
||||
}
|
||||
|
||||
static private synchronized void logErrorMessage(String baseNameSuffixString, Object errorMessage) {
|
||||
System.err.println(Log.applicationNameWithTimeStamp()+baseNameSuffixString+": "+errorMessage);
|
||||
System.err.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Funnel-point method for printing debug messages.
|
||||
*
|
||||
* Writes "debugMessage" to stderr, prefixed by the string "ApplicationName:"
|
||||
*
|
||||
* This method only works if the debugFilter string is found in Debug's list of
|
||||
* filter strings (which you can normally set on the commandline, see JDApplication),
|
||||
* or if the "ALL" filter has been set into Debug's list of filters, or if the passed
|
||||
* in filter is "", which is considered to always be "set", and will print a line with
|
||||
* "[DEBUG]" listed as the filter.
|
||||
*
|
||||
* This method may be called with a "null" debugMessage. A debugMessage is sometimes
|
||||
* unneccesary since the matched filter string is printed with the output anyway, and that
|
||||
* is frequently enough information.
|
||||
*/
|
||||
static public synchronized void logDebugMessage(Object debugMessage, String debugFilter) {
|
||||
if (Debug.containsFilter(debugFilter)) {
|
||||
String filterString;
|
||||
|
||||
if (debugFilter == null) {
|
||||
if (Debug.containsFilter(Debug.ALL_FILTER_STRING)) {
|
||||
filterString = Debug.ALL_FILTER_STRING;
|
||||
} else {
|
||||
// Quiets compiler... If we're here, we should always be able to set it below.
|
||||
filterString = "UNMATCHED FILTER";
|
||||
}
|
||||
} else if (debugFilter.equals("")) {
|
||||
filterString = "DEBUG";
|
||||
} else {
|
||||
filterString = debugFilter;
|
||||
}
|
||||
Log.logErrorMessage("["+filterString+"]", debugMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to calling "logDebugMessage(debugMessage, "ALL")".
|
||||
*/
|
||||
static public synchronized void logDebugMessage(Object debugMessage) {
|
||||
logDebugMessage(debugMessage, "ALL");
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message when "aCondition" is true, otherwise be silent.
|
||||
* Equivalent to calling "if (aCondition) logDebugMessage(debugMessage, "")".
|
||||
*/
|
||||
static public synchronized void logDebugMessage(Object debugMessage, boolean aCondition) {
|
||||
if (aCondition) {
|
||||
Log.logDebugMessage(debugMessage, "");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a message when "aCondition" is true, otherwise be silent.
|
||||
*/
|
||||
static public synchronized void logDebugMessage(Object anInstance, Object debugMessage, boolean aCondition) {
|
||||
if (aCondition) {
|
||||
Log.logDebugMessage(Debug.getNameAndHashCode(anInstance)+": "+debugMessage, "");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to calling "logDebugMessage(anInstance, debugMessage, "ALL")".
|
||||
*/
|
||||
static public synchronized void logDebugMessage(Object anInstance, Object debugMessage) {
|
||||
Log.logDebugMessage(anInstance, debugMessage, "ALL");
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes "debugMessage" to stderr, prefixed by the string "ApplicationName: ClassName[0xhashCode]: " *
|
||||
*/
|
||||
static public synchronized void logDebugMessage(Object anInstance, Object debugMessage, String debugFilter) {
|
||||
Log.logDebugMessage(Debug.getNameAndHashCode(anInstance)+": "+debugMessage, debugFilter);
|
||||
}
|
||||
} // End of class Log
|
||||
372
mozilla/java/util/classes/org/mozilla/util/ParameterCheck.java
Normal file
372
mozilla/java/util/classes/org/mozilla/util/ParameterCheck.java
Normal file
@@ -0,0 +1,372 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
/**
|
||||
* ParameterCheck provides convenient means for parameter
|
||||
* checking. Class methods in the ParameterCheck class can be
|
||||
* used to verify that method parameters meet certain conditions and to
|
||||
* raise exceptions if the conditions are not met.<p>
|
||||
*
|
||||
* Typical usage:
|
||||
* <BLOCKQUOTE><TT>
|
||||
* ParameterCheck.nonNull(myParameter);
|
||||
* </TT></BLOCKQUOTE><P>
|
||||
*
|
||||
* This verifies that <TT>myParameter</TT> is not null, throwing an
|
||||
* IllegalArgumentException if it is.<P>
|
||||
*
|
||||
* ParameterCheck is intended specifically for checking parameters;
|
||||
* for general condition and invariant testing, use Assert.
|
||||
*
|
||||
* @version $Id: ParameterCheck.java,v 1.1 1999-07-30 01:02:58 edburns%acm.org Exp $
|
||||
* @author The LFC Team (Rob Davis, Paul Kim, Alan Chung, Ray Ryan, etc)
|
||||
* @author David-John Burrowes (he moved it to the AU package)
|
||||
* @see Assert
|
||||
*/
|
||||
final public class ParameterCheck extends Object {
|
||||
/**
|
||||
* The RCSID for this class.
|
||||
*/
|
||||
private static final String RCSID =
|
||||
"$Id: ParameterCheck.java,v 1.1 1999-07-30 01:02:58 edburns%acm.org Exp $";
|
||||
|
||||
/**
|
||||
* Private constructor prevents instances of this class from being
|
||||
* created.
|
||||
*/
|
||||
private ParameterCheck() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws IllegalArgumentException if 'anObject' is null; otherwise, does
|
||||
* nothing.
|
||||
*
|
||||
* @param anObject value to verify as non-null
|
||||
* @exception IllegalArgumentException if 'anObject' is null
|
||||
*/
|
||||
static public void nonNull(Object anObject) {
|
||||
if (anObject == null) {
|
||||
throw new IllegalArgumentException("Object is null");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws IllegalArgumentException if 'aString' is null or if
|
||||
* 'aString' is an empty string; otherwise, does nothing.
|
||||
*
|
||||
* @param aString string to verify as non-null and not empty
|
||||
* @exception IllegalArgumentException if 'aString' is null or empty
|
||||
*/
|
||||
static public void notEmpty(String aString) {
|
||||
if (aString == null) {
|
||||
throw new IllegalArgumentException("String is null");
|
||||
}
|
||||
else if (aString.equals("")) {
|
||||
throw new IllegalArgumentException("String is empty");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is less than 'minimum'; otherwise,
|
||||
* does nothing.
|
||||
*
|
||||
* @param anInt value to verify as not less than 'minimum'
|
||||
* @param minimum smallest acceptable value for 'anInt'
|
||||
* @exception RangeException if 'anInt' is less than 'minimum'
|
||||
*/
|
||||
static public void noLessThan(int anInt, int minimum) {
|
||||
if (anInt < minimum) {
|
||||
throw new RangeException("Value " + anInt +
|
||||
" is out of range. It is should be no less than " + minimum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is not greater than 'minimum';
|
||||
* otherwise, does nothing.
|
||||
*
|
||||
* @param anInt value to verify as greater than 'minimum'
|
||||
* @param minimum largest unacceptable value for 'anInt'
|
||||
* @exception RangeException if 'anInt' is not greater than 'minimum'
|
||||
*/
|
||||
static public void greaterThan(int anInt, int minimum) {
|
||||
if (anInt <= minimum) {
|
||||
throw new RangeException ("Value " + anInt +
|
||||
" is out of range. It should be greater than " + minimum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is greater than 'maximum'; otherwise,
|
||||
* does nothing.
|
||||
*
|
||||
* @param anInt value to verify as not greater than 'maximum'
|
||||
* @param maximum largest acceptable value for 'anInt'
|
||||
* @exception RangeException if 'anInt' is greater than 'maximum'
|
||||
*/
|
||||
static public void noGreaterThan(int anInt, int maximum) {
|
||||
if (anInt > maximum) {
|
||||
throw new RangeException ("Value " + anInt + "is out of " +
|
||||
"range. It should be no greater than " + maximum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is not less than 'maximum';
|
||||
* otherwise, does nothing.
|
||||
*
|
||||
* @param anInt value to verify as less than 'minimum'
|
||||
* @param maximum smallest unacceptable value for 'anInt'
|
||||
* @exception RangeException if 'anInt' is not less than 'maximum'
|
||||
*/
|
||||
static public void lessThan(int anInt, int maximum) {
|
||||
if (anInt >= maximum) {
|
||||
throw new RangeException ("Value " + anInt +
|
||||
" is out of range. " +
|
||||
"It should be less than " + maximum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aDouble' is less than 'minimum'; otherwise,
|
||||
* does nothing.
|
||||
*
|
||||
* @param aDouble value to verify as not less than 'minimum'
|
||||
* @param minimum smallest acceptable value for 'aDouble'
|
||||
* @exception RangeException if 'aDouble' is less than 'minimum'
|
||||
*/
|
||||
static public void noLessThan(double aDouble, double minimum) {
|
||||
if (aDouble < minimum) {
|
||||
throw new RangeException ("Value " + aDouble +
|
||||
" is out of range. " +
|
||||
"It should be no less than " + minimum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aDouble' is not greater than 'minimum';
|
||||
* otherwise, does nothing.
|
||||
*
|
||||
* @param aDouble value to verify as greater than 'minimum'
|
||||
* @param minimum largest unacceptable value for 'aDouble'
|
||||
* @exception RangeException if 'aDouble' is not greater than 'minimum'
|
||||
*/
|
||||
static public void greaterThan(double aDouble, double minimum) {
|
||||
if (aDouble <= minimum) {
|
||||
throw new RangeException ("Value " + aDouble +
|
||||
" is out of range. " +
|
||||
"It should be greater than " + minimum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aDouble' is greater than
|
||||
* 'maximum'; otherwise, does nothing.
|
||||
*
|
||||
* @param aDouble value to verify as not greater than 'maximum'
|
||||
* @param maximum largest acceptable value for 'aDouble'
|
||||
* @exception RangeException if 'aDouble' is greater than 'maximum'
|
||||
*/
|
||||
static public void noGreaterThan(double aDouble, double maximum) {
|
||||
if (aDouble > maximum) {
|
||||
throw new RangeException ("Value " + aDouble +
|
||||
" is out of range. " +
|
||||
"It should be no greater than " + maximum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aDouble' is not less than 'maximum';
|
||||
* otherwise, does nothing.
|
||||
*
|
||||
* @param aDouble value to verify as less than 'minimum'
|
||||
* @param maximum smallest unacceptable value for 'aDouble'
|
||||
* @exception RangeException if 'aDouble' is not less than 'maximum'
|
||||
*/
|
||||
static public void lessThan(double aDouble, double maximum) {
|
||||
if (aDouble >= maximum) {
|
||||
throw new RangeException ("Value " + aDouble +
|
||||
" is out of range. It should be less than " + maximum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is less than 'minimum' or greater
|
||||
* than 'maximum'; otherwise, does nothing.
|
||||
*
|
||||
* @param anInt value to verify as not greater than 'maximum' and
|
||||
* not less than 'minimum'
|
||||
* @param minimum smallest acceptable value for 'anInt'
|
||||
* @param maximum largest acceptable value for 'anInt'
|
||||
* @exception RangeException if 'anInt' is less than 'minimum' or
|
||||
* greater than 'maximum'
|
||||
*/
|
||||
static public void withinRange(int anInt, int minimum, int maximum) {
|
||||
ParameterCheck.noLessThan(anInt, minimum);
|
||||
ParameterCheck.noGreaterThan(anInt, maximum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aDouble' is less than 'minimum' or greater
|
||||
* than 'maximum'; otherwise, does nothing.
|
||||
*
|
||||
* @param aDouble value to verify as not greater than 'maximum' and
|
||||
* not less than 'minimum'
|
||||
* @param minimum smallest acceptable value for 'aDouble'
|
||||
* @param maximum largest acceptable value for 'aDouble'
|
||||
* @exception RangeException if 'aDouble' is less than 'minimum' or
|
||||
* greater than 'maximum'
|
||||
*/
|
||||
static public void withinRange(double aDouble, double minimum,
|
||||
double maximum) {
|
||||
ParameterCheck.noLessThan(aDouble, minimum);
|
||||
ParameterCheck.noGreaterThan(aDouble, maximum);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is not within 'aRange'; otherwise,
|
||||
* does nothing.
|
||||
*
|
||||
* @param anInt value to verify as within 'aRange'
|
||||
* @param aRange acceptable range for 'anInt'
|
||||
* @exception RangeException if 'anInt' is not within 'aRange'
|
||||
*/
|
||||
static public void withinRange(int anInt, Range aRange) {
|
||||
if (!aRange.containsIndex(anInt)) {
|
||||
throw new RangeException ("Value " + anInt +
|
||||
" should be in range " + aRange);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'anInt' is not within a sequence that starts
|
||||
* at 0 and has a length of 'count'; otherwise does nothing.
|
||||
*
|
||||
* (e.g. withinCount(myInt, array.length) will verify that 'myInt' is not
|
||||
* less than 0 and not greater than 'array.length'-1)
|
||||
*
|
||||
* @param anInt value to verify as between 0 and 'count' - 1, inclusive
|
||||
* @param count length of sequence
|
||||
* @exception RangeException if 'anInt' is out of bounds
|
||||
*/
|
||||
static public void withinCount(int anInt, int count) {
|
||||
ParameterCheck.withinRange(anInt, 0, count-1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aRange' is not completely between 'minimum'
|
||||
* and 'maximum', inclusive; otherwise, does nothing.
|
||||
*
|
||||
* @param aRange range to verify as between 'minimum' and 'maximum'
|
||||
* @param minimum smallest acceptable index in 'aRange'
|
||||
* @param maximum largest acceptable index in 'aRange'
|
||||
* @exception RangeException if 'aRange' is out of bounds
|
||||
*/
|
||||
static public void rangeWithinBounds(Range aRange, int minimum,
|
||||
int maximum) {
|
||||
if (aRange.getStart() < minimum) {
|
||||
throw new RangeException ("Range " + aRange +
|
||||
" should not contain indices less than " +
|
||||
minimum);
|
||||
}
|
||||
|
||||
if (aRange.getEnd() > maximum) {
|
||||
throw new RangeException ("Range " + aRange +
|
||||
" should not contain indices greater than "
|
||||
+ maximum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws RangeException if 'aRange' is not completely within a sequence
|
||||
* that starts at 0 and has a length of 'count'; otherwise does nothing.
|
||||
*
|
||||
* (e.g. rangeWithinCount(range, array.length) will verify that 'range'
|
||||
* does not start before 0 and does not end after 'array.length'-1).
|
||||
*
|
||||
* @param aRange range to verify as between 0 and 'count' - 1, inclusive
|
||||
* @param count length of sequence
|
||||
* @exception RangeException if 'aRange' is out of bounds
|
||||
*/
|
||||
static public void rangeWithinCount(Range aRange, int count) {
|
||||
ParameterCheck.rangeWithinBounds(aRange, 0, count-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a string and a range which is intended to indicate a substring.
|
||||
* Throws IllegalArgumentException if either argument is null. Throws
|
||||
* RangeException if <I>aRange</I> does not indicate a valid substring of
|
||||
* <I>aString</I>.
|
||||
*
|
||||
* @param aRange A range which must be contained within
|
||||
* the string.
|
||||
* @param aString A string to use for verifying the range.
|
||||
* @exception IllegalArgumentException if either argument is null
|
||||
* @exception RangeException if the range is not contained
|
||||
* within the string
|
||||
*/
|
||||
static public void rangeWithinString(Range aRange, String aString) {
|
||||
ParameterCheck.nonNull(aString);
|
||||
ParameterCheck.nonNull(aRange);
|
||||
|
||||
if (aRange.getStart() < 0) {
|
||||
throw new RangeException("Range has negative start");
|
||||
}
|
||||
if(aRange.getMax() > aString.length()) {
|
||||
throw new RangeException("Range extends beyond end of String");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws IllegalArgumentException if 'generalTest' is false; otherwise,
|
||||
* does nothing. 'message' will be the message of the exception. 'message'
|
||||
* may be null, but use of a meaningful message is recommended.
|
||||
*
|
||||
* @param generalTest value to verify as true
|
||||
* @param message message describing the failure
|
||||
* @exception IllegalArgumentException if 'generalTest' is false
|
||||
* @see #isFalse(boolean generalTest, String message)
|
||||
*/
|
||||
static public void isTrue(boolean generalTest, String message) {
|
||||
if (!generalTest) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to isTrue, except the test is inverted.
|
||||
*
|
||||
* @param generalTest value to verify as false
|
||||
* @param message message describing the failure
|
||||
* @exception IllegalArgumentException if 'generalTest' is false
|
||||
* @see #isTrue(boolean generalTest, String message)
|
||||
*/
|
||||
static public void isFalse(boolean generalTest, String message) {
|
||||
if (generalTest) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
} // End of class ParameterCheck
|
||||
467
mozilla/java/util/classes/org/mozilla/util/Range.java
Normal file
467
mozilla/java/util/classes/org/mozilla/util/Range.java
Normal file
@@ -0,0 +1,467 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.Cloneable;
|
||||
import java.lang.Math;
|
||||
|
||||
/**
|
||||
* An Range is an object representing a range of integer values. A
|
||||
* range has a start index and a count. The count must be greater than or
|
||||
* equal to 0. Instances of this class are immutable, but subclasses may
|
||||
* introduce mutability. A mutable range will return true from
|
||||
* isMutable().
|
||||
*
|
||||
* @version $Id: Range.java,v 1.1 1999-07-30 01:02:58 edburns%acm.org Exp $
|
||||
* @author The LFC Team (Rob Davis, Paul Kim, Alan Chung, Ray Ryan, etc)
|
||||
* @author David-John Burrowes (he moved it to the AU package)
|
||||
* @see Range#isMutable()
|
||||
* <!-- see MutableRange -->
|
||||
*/
|
||||
|
||||
public class Range extends Object implements Cloneable, Serializable {
|
||||
|
||||
//
|
||||
// STATIC VARIABLES
|
||||
//
|
||||
|
||||
/**
|
||||
* The RCSID for this class.
|
||||
*/
|
||||
private static final String RCSID =
|
||||
"$Id: Range.java,v 1.1 1999-07-30 01:02:58 edburns%acm.org Exp $";
|
||||
|
||||
/**
|
||||
* A zero range
|
||||
*/
|
||||
static public final Range ZeroRange = new Range(-1, 0);
|
||||
|
||||
//
|
||||
// Instance Variables
|
||||
//
|
||||
|
||||
/**
|
||||
* The start index of the range
|
||||
*/
|
||||
protected int start;
|
||||
|
||||
/**
|
||||
* The length of the range
|
||||
*/
|
||||
protected int count;
|
||||
|
||||
//
|
||||
// Constructors
|
||||
//
|
||||
|
||||
/**
|
||||
* Creates an instance of Range from another Range object
|
||||
* otherRange which must be non-null.
|
||||
*
|
||||
* @param range the reference range to create this range from
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*/
|
||||
public Range(Range otherRange) {
|
||||
//
|
||||
// Do our own construction rather than calling Range(int,int)
|
||||
// because we want to be able to do the parameter check. There is
|
||||
// another way that allows us to use the other constructor, but
|
||||
// this class is too simple to be worth the exta work.
|
||||
//
|
||||
super();
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
|
||||
this.start = otherRange.getStart();
|
||||
this.count = otherRange.getCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of Range with a start index newStart and
|
||||
* an extent newCount. newCount must be greater than or equal to
|
||||
* 0.
|
||||
*
|
||||
* @param newStart the start index of the range
|
||||
* @param newCount the number of elements in the range
|
||||
* @exception RangeException if newCount is less than 0.
|
||||
*/
|
||||
public Range(int newStart, int newCount) {
|
||||
super();
|
||||
|
||||
this.start = newStart;
|
||||
this.count = newCount;
|
||||
}
|
||||
|
||||
//
|
||||
// PROPERTY ACCESS
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns the start index of the range.
|
||||
*
|
||||
* @return the start index of this range
|
||||
*/
|
||||
public synchronized int getStart() {
|
||||
return this.start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this range.
|
||||
*
|
||||
* @return the number of elements in this range
|
||||
*/
|
||||
public synchronized int getCount() {
|
||||
return this.count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the the last index contained within the range.
|
||||
*
|
||||
* @return the end index of this range
|
||||
*/
|
||||
public synchronized int getEnd() {
|
||||
return this.getStart() + this.getCount() - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the max index (the index at start + count).
|
||||
*
|
||||
* @return the max index.
|
||||
*/
|
||||
public synchronized int getMax() {
|
||||
return this.getStart() + this.getCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a number guaranteed to be within this range, including
|
||||
* endpoints
|
||||
*
|
||||
* @return a number within the range, including endpoints.
|
||||
*/
|
||||
protected int getConstrainedInt(int number) {
|
||||
int rc;
|
||||
|
||||
if (number < this.getStart()) {
|
||||
rc = this.getStart();
|
||||
}
|
||||
else {
|
||||
if (number > this.getEnd()) {
|
||||
rc = this.getEnd();
|
||||
} else {
|
||||
rc = number;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
//
|
||||
// METHODS
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns true if and only if index lies within this range.
|
||||
* Note that if the receiver is of zero length, then this method will return
|
||||
* false.
|
||||
*
|
||||
* @return true if and only if and only if index lies within this
|
||||
* range
|
||||
*/
|
||||
public synchronized boolean containsIndex(int index) {
|
||||
if (this.getCount() == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return (index >= this.getStart()) && (index <= this.getEnd());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if the start index of this range is
|
||||
* greater than index.
|
||||
*
|
||||
* @return true if and only if the start index of this range is
|
||||
* greater than index
|
||||
*/
|
||||
public synchronized boolean isAfterIndex(int index){
|
||||
return (this.getStart() > index);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return true if and only if the end index of this range is less than
|
||||
* index.
|
||||
*
|
||||
* @return true if and only if the end index of this range is less than
|
||||
* index
|
||||
*/
|
||||
public synchronized boolean isBeforeIndex(int index)
|
||||
{
|
||||
return (this.getEnd() < index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if every element in otherRange is contained
|
||||
* in this range. If the receiver is a zero length range, then
|
||||
* this method will return false. Note that containment can apply to
|
||||
* zero length ranges; a non-zero-length range contains any zero-length
|
||||
* range. Contrast with range intersection.
|
||||
*
|
||||
* @see #intersectsWithRange(Range otherRange)
|
||||
* @return true if and only if every element in otherRange is
|
||||
* contained in this range
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*
|
||||
*/
|
||||
public synchronized boolean containsRange (Range otherRange)
|
||||
{
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
|
||||
if (this.getCount() == 0) {
|
||||
return false;
|
||||
} else if (otherRange.getCount() == 0) {
|
||||
return true;
|
||||
}
|
||||
return ((this.getStart() <= otherRange.getStart()) &&
|
||||
(this.getEnd() >= otherRange.getEnd()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if otherRange intersects with this range. A
|
||||
* zero-length range intersects with no range.
|
||||
*
|
||||
* @return true if and only if otherRange intersects with this range
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*
|
||||
*/
|
||||
public synchronized boolean intersectsWithRange(Range otherRange) {
|
||||
return this.overlapWithRange(otherRange) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements that are in both this range and
|
||||
* otherRange. A zero-length range has no overlapping elements
|
||||
* with any range.
|
||||
*
|
||||
* @param a range to check overlaps with this range
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
* @return number of elements in both this range and otherRange
|
||||
*/
|
||||
public synchronized int overlapWithRange(Range otherRange) {
|
||||
if (this.getCount() == 0 || otherRange.getCount() == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return Math.max(0, (Math.min(this.getEnd(), otherRange.getEnd()) -
|
||||
Math.max(this.getStart(),
|
||||
otherRange.getStart())) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if otherRange is adjacent to this range;
|
||||
* two ranges are adjacent if the max of one range is equal to the start of
|
||||
* the other. A zero length range is adjacent to no range.
|
||||
*
|
||||
* @return true if and only if otherRange is adjacent to this range
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*/
|
||||
public synchronized boolean isAdjacentToRange(Range otherRange) {
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
|
||||
if (this.getCount() == 0 || otherRange.getCount() == 0) {
|
||||
return false;
|
||||
}
|
||||
return (Math.max(this.getStart(), otherRange.getStart()) ==
|
||||
(Math.min(this.getEnd(), otherRange.getEnd()) + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns true if and only if this range is before otherRange
|
||||
* and the two ranges do not overlap.
|
||||
*
|
||||
* @return true if and only if this range is before otherRange
|
||||
* and the two ranges do not overlap.
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*/
|
||||
public synchronized boolean isBeforeRange(Range otherRange) {
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
|
||||
if (this.getEnd() < otherRange.getStart()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns true if this range is after otherRange and the two
|
||||
* ranges do not overlap.
|
||||
*
|
||||
* @return true if this range is after otherRange and the two
|
||||
* ranges do not overlap.
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*/
|
||||
protected boolean isAfterRange(Range otherRange) {
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
|
||||
return otherRange.isBeforeRange(this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the intersection of this range and otherRange. If the
|
||||
* ranges do not intersect then Range.ZeroRange is returned.
|
||||
* Note that a zero length range has no intersection with a non-zero
|
||||
* length range.
|
||||
*
|
||||
* @return the intersection of this range and otherRange
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*/
|
||||
public synchronized Range rangeFromIntersection(Range otherRange) {
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
|
||||
if (this.intersectsWithRange(otherRange)) {
|
||||
int newStart = Math.max(this.getStart(), otherRange.getStart());
|
||||
int newEnd = Math.min(this.getEnd(), otherRange.getEnd());
|
||||
return new Range(newStart, newEnd - newStart + 1);
|
||||
}
|
||||
return Range.ZeroRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the union of this range and otherRange.
|
||||
* Returns the other range if one of them are zero length range.
|
||||
* Returns Range.ZeroRange if both are zero length range.Note that the
|
||||
* union of a non-zero length range with a zero length range is merely the
|
||||
* non-zero length range.
|
||||
*
|
||||
* @return the union of this range and otherRange if both are
|
||||
* non-zero range
|
||||
* @exception IllegalArgumentException if otherRange is null
|
||||
*/
|
||||
public synchronized Range rangeFromUnion(Range otherRange) {
|
||||
ParameterCheck.nonNull(otherRange);
|
||||
Range retRange;
|
||||
|
||||
int thisRangeCount = this.getCount();
|
||||
int otherRangeCount = otherRange.getCount();
|
||||
|
||||
if (thisRangeCount == 0 && otherRangeCount == 0) {
|
||||
retRange = ZeroRange;
|
||||
} else if (thisRangeCount == 0) {
|
||||
retRange = new Range(otherRange);
|
||||
} else if (otherRangeCount == 0) {
|
||||
retRange = new Range(this);
|
||||
} else {
|
||||
int newStart;
|
||||
int newEnd;
|
||||
|
||||
newStart = Math.min(this.getStart(), otherRange.getStart());
|
||||
newEnd = Math.max(this.getEnd(), otherRange.getEnd());
|
||||
return new Range(newStart, newEnd - newStart + 1);
|
||||
}
|
||||
return retRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this range, with its start shifted by offset.
|
||||
*
|
||||
* @return this range, with its start offset by offset.
|
||||
*/
|
||||
public synchronized Range rangeShiftedByOffset(int offset) {
|
||||
return new Range(this.getStart() + offset, this.getCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String representation of this Range.
|
||||
*
|
||||
* @return string representation of this range
|
||||
*/
|
||||
public synchronized String toString() {
|
||||
return "start = " + this.getStart() + ", count = " + this.getCount();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true only if this instance can change after it is created.<P>
|
||||
*
|
||||
* The default implementation returns false because instances of this
|
||||
* class can't change; subclasses that introduce mutability should
|
||||
* override this method to return true.
|
||||
*
|
||||
* @return true if and only if the range may change
|
||||
*/
|
||||
public boolean isMutable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates and returns an Range that is identical to this one.
|
||||
*
|
||||
* @return a reference to the immutable instance or
|
||||
* a copy of the mutable instance
|
||||
*/
|
||||
public synchronized Object clone() {
|
||||
if (this.isMutable()) {
|
||||
Range newRange;
|
||||
|
||||
try {
|
||||
newRange = (Range)super.clone();
|
||||
}
|
||||
catch (CloneNotSupportedException e) {
|
||||
// Won't happen because we implement Cloneable
|
||||
throw new InternalError(e.toString());
|
||||
}
|
||||
return newRange;
|
||||
}
|
||||
else {
|
||||
// No reason to clone an immutable object
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if and only if otherRange is an Range
|
||||
* with the same start and count as this range.
|
||||
*
|
||||
* @return true if and only if otherRange equals this range
|
||||
*/
|
||||
public synchronized boolean equals(Object otherRange) {
|
||||
return ((otherRange == this) ||
|
||||
((otherRange != null) &&
|
||||
(otherRange instanceof Range) &&
|
||||
(this.getStart() == ((Range)otherRange).getStart()) &&
|
||||
(this.getEnd() == ((Range)otherRange).getEnd())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden because equals() is overridden. Returns a hash code
|
||||
* that is based on the start and count of this range.
|
||||
*
|
||||
* @return a hash code based on the start index and count of this range
|
||||
*/
|
||||
public synchronized int hashCode() {
|
||||
return Math.round(this.getStart()) ^ Math.round(this.getCount());
|
||||
}
|
||||
|
||||
} // End of class Range
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
import java.lang.RuntimeException;
|
||||
|
||||
/**
|
||||
* An RangeException is an exception that is thrown when a value
|
||||
* is outside of its acceptable range.
|
||||
*
|
||||
* @version $Id: RangeException.java,v 1.1 1999-07-30 01:02:58 edburns%acm.org Exp $
|
||||
* @author The LFC Team (Rob Davis, Paul Kim, Alan Chung, Ray Ryan, etc)
|
||||
* @author David-John Burrowes (he moved it to the AU package)
|
||||
*/
|
||||
public class RangeException extends RuntimeException {
|
||||
|
||||
//
|
||||
// STATIC VARIABLES
|
||||
//
|
||||
|
||||
/**
|
||||
* The RCSID for this class.
|
||||
*/
|
||||
private static final String RCSID =
|
||||
"$Id: RangeException.java,v 1.1 1999-07-30 01:02:58 edburns%acm.org Exp $";
|
||||
|
||||
/**
|
||||
* Constructs an exception with no error string.
|
||||
*/
|
||||
public RangeException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an exception with an error string.
|
||||
*
|
||||
* @param message A message that will hopefully tell someone about the cause
|
||||
* of this exception.
|
||||
*/
|
||||
public RangeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
285
mozilla/java/util/classes/org/mozilla/util/Utilities.java
Normal file
285
mozilla/java/util/classes/org/mozilla/util/Utilities.java
Normal file
@@ -0,0 +1,285 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License
|
||||
* Version 1.0 (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
||||
* the License for the specific language governing rights and limitations
|
||||
* under the License.
|
||||
*
|
||||
* The original code is "The Lighthouse Foundation Classes (LFC)"
|
||||
*
|
||||
* The Initial Developer of the Original Code is Sun Microsystems,
|
||||
* Inc. Portions created by Sun are Copyright (C) 1997, 1998, 1999 Sun
|
||||
* Microsystems, Inc. All Rights Reserved.
|
||||
*/
|
||||
|
||||
|
||||
package org.mozilla.util;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.MissingResourceException;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Container;
|
||||
|
||||
|
||||
public class Utilities extends Object {
|
||||
// PENDING(kbern) NOTE: These Vector methods should eventually move into a file
|
||||
// called "VectorUtilities", must like the already-existing "HashtableUtilities".
|
||||
/**
|
||||
* Take the given string and chop it up into a series
|
||||
* of strings on "delimeter" boundries. This is useful
|
||||
* for trying to get an array of strings out of the
|
||||
* resource file.
|
||||
*/
|
||||
static public Vector vectorFromString(String input, String delimeter) {
|
||||
Vector aVector = new Vector();
|
||||
StringTokenizer aTokenizer = new StringTokenizer(input, delimeter);
|
||||
|
||||
while (aTokenizer.hasMoreTokens())
|
||||
aVector.addElement(aTokenizer.nextToken());
|
||||
return aVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a String by combining the elements of aVector.
|
||||
* after each element it will insert the String "delimeter".
|
||||
* If no "delimeter" is desired, the "delimeter" parameter should be ""
|
||||
*/
|
||||
static public String stringFromVector(Vector aVector, String delimeter) {
|
||||
String returnString = null;
|
||||
|
||||
if (aVector != null) {
|
||||
Enumeration vectorEnumerator = aVector.elements();
|
||||
|
||||
if (delimeter == null) {
|
||||
delimeter = ""; // Might as well be nice to sloppy caller!
|
||||
}
|
||||
while (vectorEnumerator.hasMoreElements()) {
|
||||
if (returnString == null) {
|
||||
returnString = "";
|
||||
} else {
|
||||
returnString += delimeter;
|
||||
}
|
||||
returnString += vectorEnumerator.nextElement();
|
||||
}
|
||||
}
|
||||
return (returnString == null) ? "" : returnString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an Array into a Vector. Can you *believe* that there is no Vector constructor
|
||||
* which takes an Array!!! &()*&$#(*&$
|
||||
*/
|
||||
static public Vector vectorFromArray(Object[] anArray) {
|
||||
Vector returnVector;
|
||||
|
||||
if ((anArray != null) && (anArray.length > 0)) {
|
||||
returnVector = new Vector(anArray.length);
|
||||
for (int i = anArray.length - 1; i >= 0; i--) {
|
||||
returnVector.addElement(anArray[i]);
|
||||
}
|
||||
} else {
|
||||
returnVector = new Vector(0);
|
||||
}
|
||||
return returnVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Amazing that "Vector" does not override Object's "equals()" method to do this
|
||||
* itself!
|
||||
*/
|
||||
static public boolean vectorsAreEqual(Vector vectorOne, Vector vectorTwo) {
|
||||
boolean returnValue = vectorOne.equals(vectorTwo); // Give "Object" a chance.
|
||||
|
||||
if (!returnValue && (vectorOne != null) && (vectorTwo != null)) {
|
||||
int vectorSize = vectorOne.size();
|
||||
|
||||
if (vectorSize == vectorTwo.size()) {
|
||||
int index;
|
||||
|
||||
// "Object" says, "No", but maybe they really are... let's do the long check...
|
||||
for (index = 0; index < vectorSize; index++) {
|
||||
if (!(vectorOne.elementAt(index).equals(vectorTwo.elementAt(index)))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index == vectorSize) {
|
||||
// We made it to the end without "break"ing! They must be equal...
|
||||
returnValue = true;
|
||||
}
|
||||
} // else, they're definately not equal!
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns "true" if the passed in array contains the passed in element.
|
||||
* Checks for equality using ".equals()".
|
||||
* Returns "false" if "anArray" is null.
|
||||
*/
|
||||
static public boolean arrayContainsElement(Object[] anArray, Object anElement) {
|
||||
boolean returnValue = false;
|
||||
|
||||
if (anArray != null) {
|
||||
for (int index = 0; (!returnValue && (index < anArray.length)); index++) {
|
||||
if (anArray[index].equals(anElement)) {
|
||||
returnValue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes leading, trailing, and internal whitespace from the passed-in
|
||||
* string.
|
||||
* Returns a new string without any whitespace.
|
||||
*/
|
||||
static public String removeAllWhitespace(String aString) {
|
||||
String returnString = aString;
|
||||
|
||||
if (aString != null) {
|
||||
returnString = aString.trim();
|
||||
Vector stringComponents = Utilities.vectorFromString(aString, " ");
|
||||
|
||||
returnString = Utilities.stringFromVector(stringComponents, "");
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can return a string of the form "5:35:09pm", as opposed to "17:35:09"
|
||||
* If "useTwentyFourHourTime" is "true", returns time in the form "17:35:09"
|
||||
* If "showAMPMIndicator" is "true" it will include the "am" or "pm" text,
|
||||
* otherwise it won't.
|
||||
* Note that the "showAMPMIndicator" field is ignored if "useTwentyFourHourTime" is "true"
|
||||
* since it provides redundant information in that case.
|
||||
*/
|
||||
static public String currentTimeString(boolean useTwentyFourHourTime, boolean showAMPMIndicator) {
|
||||
// "ugly" is of the form, "17:35:09"
|
||||
String returnString = new Date().toString().substring(11, 19);
|
||||
|
||||
if (!useTwentyFourHourTime) {
|
||||
Vector dateComponents = Utilities.vectorFromString(returnString, ":");
|
||||
try {
|
||||
String ampmString;
|
||||
int hourField = Integer.parseInt((String)(dateComponents.elementAt(0)));
|
||||
|
||||
// Make it on 12 hour clock, with am and pm...
|
||||
if (hourField > 12) {
|
||||
// More common than == 12
|
||||
ampmString = "pm";
|
||||
hourField -= 12;
|
||||
dateComponents.setElementAt(Integer.toString(hourField), 0);
|
||||
returnString = Utilities.stringFromVector(dateComponents, ":");
|
||||
} else if (hourField == 12) {
|
||||
ampmString = "pm";
|
||||
} else {
|
||||
ampmString = "am";
|
||||
}
|
||||
if (showAMPMIndicator) {
|
||||
returnString += ampmString;
|
||||
}
|
||||
} catch (NumberFormatException anException) {
|
||||
}
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses "getParent()" to find this Component's top-level ancestor.
|
||||
*
|
||||
* If this Component has no ancestors, this method will return the Component itself.
|
||||
*/
|
||||
static public Component getTopLevelParent(Component aComponent) {
|
||||
Component returnComponent = aComponent;
|
||||
|
||||
if (aComponent != null) {
|
||||
Component testComponent = aComponent;
|
||||
|
||||
do {
|
||||
if (testComponent instanceof Container) {
|
||||
returnComponent = testComponent;
|
||||
testComponent = ((Container)testComponent).getParent();
|
||||
} else {
|
||||
testComponent = null;
|
||||
}
|
||||
} while (testComponent != null);
|
||||
}
|
||||
return returnComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param aClass the class whose package name should be returned
|
||||
*
|
||||
* @return the fully qualified package name of the given class, null
|
||||
* if not found
|
||||
*/
|
||||
|
||||
static public String getPackageName(Class aClass) {
|
||||
if (null == aClass) {
|
||||
return null;
|
||||
}
|
||||
String baseName = aClass.getName();
|
||||
int index = baseName.lastIndexOf('.');
|
||||
return (index < 0 ? "" : baseName.substring(0, index+1));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
||||
* This method is a simpler alternative to
|
||||
* ResourceLoader.loadResourceBundle. Instead of returning an
|
||||
* PropertyResourceBundle, as ResourceLoader.loadResourceBundle
|
||||
* does, it must returns a java.util.ResourceBundle
|
||||
|
||||
*
|
||||
* @param baseName the fully qualified name of the resource bundle,
|
||||
* sans "<CODE>.properties</CODE>" suffix. For example, a
|
||||
* valid value for baseName would be
|
||||
* <CODE>com.sun.jag.apps.spex.util.SUResources</CODE> when the
|
||||
* properties file <CODE>SUResources.properties</CODE> is in the
|
||||
* classpath under the directory
|
||||
* <CODE>com/sun/jag/apps/spex/util</CODE>.
|
||||
*
|
||||
* @return the actual ResourceBundle instance, or null if not found.
|
||||
|
||||
* <!-- see ResourceLoader#loadResourceBundle -->
|
||||
|
||||
*/
|
||||
|
||||
static public ResourceBundle getResourceBundle(String baseName) {
|
||||
ResourceBundle resourceBundle = null;
|
||||
try {
|
||||
resourceBundle = ResourceBundle.getBundle(baseName);
|
||||
}
|
||||
catch (MissingResourceException e) {
|
||||
Log.logError("Missing resource bundle: " + baseName);
|
||||
}
|
||||
return resourceBundle;
|
||||
}
|
||||
|
||||
/**
|
||||
Case insensitive String.endsWith()
|
||||
*/
|
||||
public static boolean endsWithIgnoringCase(String aString, String possibleEnding) {
|
||||
int endingLength;
|
||||
if (aString == null || possibleEnding == null)
|
||||
return false;
|
||||
endingLength = possibleEnding.length();
|
||||
if (aString.length() < endingLength)
|
||||
return false;
|
||||
return aString.regionMatches(true, aString.length() - endingLength,
|
||||
possibleEnding, 0, endingLength);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user