freehep-util-2.0.2/ 0000755 0120103 0120103 00000000000 11273054221 015067 5 ustar mascellani mascellani freehep-util-2.0.2/src/ 0000755 0120103 0120103 00000000000 11273054221 015656 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/ 0000755 0120103 0120103 00000000000 11273054221 016635 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/ 0000755 0120103 0120103 00000000000 11273054221 017556 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/ 0000755 0120103 0120103 00000000000 11273054221 020345 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/freehep/ 0000755 0120103 0120103 00000000000 11273054221 021763 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/freehep/util/ 0000755 0120103 0120103 00000000000 11273054221 022740 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/freehep/util/template/ 0000755 0120103 0120103 00000000000 11273054221 024553 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/freehep/util/template/test/ 0000755 0120103 0120103 00000000000 11273054221 025532 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/freehep/util/template/test/Test.java 0000644 0120103 0120103 00000002417 10466735775 027347 0 ustar mascellani mascellani package org.freehep.util.template.test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.freehep.util.template.Template;
import org.freehep.util.template.TemplateEngine;
/**
*
* @author tonyj
*/
public class Test
{
public static void main(String[] args) throws IOException
{
Template store = new Template();
store.set("title", "Simple Book Template Example");
store.set("name", "JavaBY");
Template book1 = new Template();
book1.set("author", "Alexey Popov");
book1.set("title", "Patterns of using JavaBY Template Engine.");
book1.set("year", "2002");
store.append("book", book1);
Template book2 = new Template();
book2.set("author", "Garmash Viacheslav");
book2.set("title", "Creating web shop using JavaBY Template Engine.");
book2.set("year", "2002");
store.append("book", book2);
TemplateEngine engine = new TemplateEngine();
engine.addValueProvider(store);
Reader in = new InputStreamReader(Test.class.getResourceAsStream("test.html"));
Reader out = engine.filter(in);
for (;;)
{
int c = out.read();
if (c < 0) break;
System.out.print((char) c);
}
}
}
freehep-util-2.0.2/src/test/java/org/freehep/util/test/ 0000755 0120103 0120103 00000000000 11273054221 023717 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/java/org/freehep/util/test/VersionComparatorTest.java 0000644 0120103 0120103 00000002756 10466735775 031140 0 ustar mascellani mascellani package org.freehep.util.test;
import junit.framework.*;
import org.freehep.util.VersionComparator;
/**
*
* @author Tony Johnson
* @version $Id: VersionComparatorTest.java 8584 2006-08-10 23:06:37Z duns $
*/
public class VersionComparatorTest extends TestCase
{
public VersionComparatorTest(java.lang.String testName)
{
super(testName);
}
public static Test suite()
{
TestSuite suite = new TestSuite(VersionComparatorTest.class);
return suite;
}
/**
* Test of versionNumberCompare method, of class org.freehep.util.VersionComparator.
*/
public void testVersionNumberCompare()
{
VersionComparator v = new VersionComparator();
assertTrue(v.compare("1", "2") < 0);
assertTrue(v.compare("2", "1") > 0);
assertTrue(v.compare("2", "2") == 0);
assertTrue(v.compare("2.1", "2.1") == 0);
assertTrue(v.compare("2.....1", "2.1") == 0);
assertTrue(v.compare("2.1.0", "2.1") == 0); // Correct?
assertTrue(v.compare("2.1.1", "2.1") > 0);
assertTrue(v.compare("2.1.1", "2.1.2") < 0);
assertTrue(v.compare("2.1.1rc1", "2.1.1") < 0);
assertTrue(v.compare("2.1.1rc1", "2.1.1rc2") < 0);
assertTrue(v.compare("2.1.1rc1", "2.1.1rc1") == 0);
assertTrue(v.compare("2.1.1beta1", "2.1.1rc1") < 0);
assertTrue(v.compare("2.1.1alpha1", "2.1.1beta1") < 0);
}
public static void main(java.lang.String[] args)
{
junit.textui.TestRunner.run(suite());
}
}
freehep-util-2.0.2/src/test/java/org/freehep/util/test/UtilTestSuite.java 0000644 0120103 0120103 00000001507 10466735775 027403 0 ustar mascellani mascellani package org.freehep.util.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
*
* @author Tony Johnson
* @version $Id: UtilTestSuite.java 8584 2006-08-10 23:06:37Z duns $
*
*/
public class UtilTestSuite extends TestCase
{
private TestSuite suite;
public UtilTestSuite(java.lang.String testName)
{
super(testName);
suite = (TestSuite) suite();
}
protected TestSuite getSuite()
{
return suite;
}
public static Test suite()
{
TestSuite suite = new TestSuite();
// Add all the test suites here
suite.addTestSuite( DoubleHashtableTest.class );
suite.addTestSuite( VersionComparatorTest.class );
suite.addTestSuite( ScientificFormatTest.class );
return suite;
}
}
freehep-util-2.0.2/src/test/java/org/freehep/util/test/ScientificFormatTest.java 0000644 0120103 0120103 00000011615 10466735775 030706 0 ustar mascellani mascellani package org.freehep.util.test;
import java.util.Random;
import junit.framework.*;
import org.freehep.util.ScientificFormat;
import org.freehep.util.DoubleWithError;
/**
*
* @author Mark Donszelmann
* @version $Id: ScientificFormatTest.java 8584 2006-08-10 23:06:37Z duns $
*/
public class ScientificFormatTest extends TestCase
{
public ScientificFormatTest(java.lang.String testName)
{
super(testName);
}
public static Test suite()
{
TestSuite suite = new TestSuite(ScientificFormatTest.class);
return suite;
}
/**
* Test of versionNumberCompare method, of class org.freehep.util.VersionComparator.
*/
public void testScientificFormatCompare()
{
ScientificFormat f = new ScientificFormat();
assertEquals("1.0000", f.format(1.0));
assertEquals(".10000", f.format(1E-1));
assertEquals(".010000", f.format(1E-2));
assertEquals(".0010000", f.format(1E-3));
assertEquals("1.0000E-4", f.format(1E-4));
assertEquals("1.0000E-5", f.format(1E-5));
assertEquals("1.0000E-6", f.format(1E-6));
assertEquals("10.000", f.format(1E1));
assertEquals("100.00", f.format(1E2));
assertEquals("1000.0", f.format(1E3));
assertEquals("10000", f.format(1E4));
assertEquals("100000", f.format(1E5));
assertEquals("1000000", f.format(1E6));
assertEquals("10000000", f.format(1E7));
assertEquals("1.0000E8", f.format(1E8));
assertEquals("1000000", f.format(999999));
assertEquals("99999", f.format(99999));
assertEquals("9999.0", f.format(9999));
assertEquals("999.00", f.format(999));
assertEquals("99.000", f.format(99));
assertEquals("9.0000", f.format(9));
assertEquals(".90000", f.format(.9));
assertEquals(".090000", f.format(.09));
assertEquals("1000.0", f.format(999.999));
assertEquals("100000", f.format(100000.000001));
assertEquals("100000", f.format(100000.0000001));
assertEquals("100000", f.format(100000.00000001));
assertEquals("100000", f.format(100000.000000001));
assertEquals("100000", f.format(100000.0000000001));
assertEquals("100000", f.format(100000.00000000001));
assertEquals("-1.0000", f.format(-1.0));
assertEquals("-.10000", f.format(-1E-1));
assertEquals("-.010000", f.format(-1E-2));
assertEquals("-.0010000", f.format(-1E-3));
assertEquals("-1.0000E-4", f.format(-1E-4));
assertEquals("-1.0000E-5", f.format(-1E-5));
assertEquals("-1.0000E-6", f.format(-1E-6));
assertEquals("-10.000", f.format(-1E1));
assertEquals("-100.00", f.format(-1E2));
assertEquals("-1000.0", f.format(-1E3));
assertEquals("-10000", f.format(-1E4));
assertEquals("-100000", f.format(-1E5));
assertEquals("-1000000", f.format(-1E6));
assertEquals("-10000000", f.format(-1E7));
assertEquals("-1.0000E8", f.format(-1E8));
assertEquals("-1000000", f.format(-999999));
assertEquals("-99999", f.format(-99999));
assertEquals("-9999.0", f.format(-9999));
assertEquals("-999.00", f.format(-999));
assertEquals("-99.000", f.format(-99));
assertEquals("-9.0000", f.format(-9));
assertEquals("-.90000", f.format(-.9));
assertEquals("-.090000", f.format(-.09));
assertEquals("-1000.0", f.format(-999.999));
assertEquals("-100000", f.format(-100000.000001));
assertEquals("-100000", f.format(-100000.0000001));
assertEquals("-100000", f.format(-100000.00000001));
assertEquals("-100000", f.format(-100000.000000001));
assertEquals("-100000", f.format(-100000.0000000001));
assertEquals("-100000", f.format(-100000.00000000001));
assertEquals("0.0000", f.format(-0));
Random r = new Random();
for (int i=0; i<10000; i++)
{
double d = r.nextDouble();
String sd = f.format(d);
double d2 = Double.parseDouble(sd);
assertEquals(d,d2,d*1e-4);
}
for (int i=0; i<10000; i++)
{
long l = r.nextLong();
double d = Double.longBitsToDouble(l);
String sd = f.format(d);
double d2 = Double.parseDouble(sd);
if (Double.isNaN(d)) assertTrue(Double.isNaN(d2));
else if (Double.isInfinite(d)) assertTrue(Double.isInfinite(d2));
else assertEquals(d,d2,Math.abs(d)*1e-4);
}
}
public void testDoubleWitheErrorCompare()
{
ScientificFormat f = new ScientificFormat();
char plusorminus = '\u00b1';
DoubleWithError de = new DoubleWithError(1,.01);
assertEquals("1.0000"+plusorminus+".0100",f.format(de));
}
public static void main(java.lang.String[] args)
{
junit.textui.TestRunner.run(suite());
}
}
freehep-util-2.0.2/src/test/java/org/freehep/util/test/DoubleHashtableTest.java 0000644 0120103 0120103 00000004545 10466735775 030507 0 ustar mascellani mascellani package org.freehep.util.test;
import java.util.Enumeration;
import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.freehep.util.DoubleHashtable;
/**
*
* @author duns
* @version $Id: DoubleHashtableTest.java 8584 2006-08-10 23:06:37Z duns $
*/
public class DoubleHashtableTest extends TestCase
{
public DoubleHashtableTest(java.lang.String testName)
{
super(testName);
}
public static Test suite()
{
TestSuite suite = new TestSuite(DoubleHashtableTest.class);
return suite;
}
public void testTable()
{
DoubleHashtable table = new DoubleHashtable();
assertTrue(table.isEmpty());
table.put("Donszelmann", "Mark", "CERN");
table.put("Donszelmann", "Mark", "SLAC");
table.put("Donszelmann", "Niels", "Knoworries");
table.put("Johnson", "Tony", "SLAC");
table.put(null, "Mark", "Family");
table.put("Donszelmann", null, "Family");
table.put(null, null, "Family");
table.put("Perl", "Joseph", null);
assertFalse(table.isEmpty());
assertNotNull(table.get("Donszelmann"));
assertNotNull(table.get("Donszelmann", "Mark"));
assertEquals(table.get("Donszelmann", "Mark"),"SLAC");
assertNotNull(table.get("Donszelmann", null));
assertEquals(table.get("Donszelmann", null),"Family");
assertNotNull(table.get(null, null));
assertNotNull(table.get(null, "Mark"));
assertTrue(table.containsKey("Perl", "Joseph"));
assertNull(table.get("Perl", "Joseph"));
table.remove("Johnson", "Tony");
assertNull(table.get("Johnson", "Tony"));
int count = 0;
for (Enumeration e=table.elements(); e.hasMoreElements(); )
{
e.nextElement();
count++;
}
assertEquals(count,table.size());
for (Iterator i=table.iterator(); i.hasNext(); )
{
String value = (String)i.next();
if ((value != null) && value.equals("SLAC"))
{
i.remove();
}
}
assertNull(table.get("Donszelmann","Mark"));
table.clear();
assertTrue(table.isEmpty());
}
public static void main(java.lang.String[] args)
{
TestRunner.run(suite());
}
}
freehep-util-2.0.2/src/test/resources/ 0000755 0120103 0120103 00000000000 11273054221 020647 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/resources/org/ 0000755 0120103 0120103 00000000000 11273054221 021436 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/resources/org/freehep/ 0000755 0120103 0120103 00000000000 11273054221 023054 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/resources/org/freehep/util/ 0000755 0120103 0120103 00000000000 11273054221 024031 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/resources/org/freehep/util/template/ 0000755 0120103 0120103 00000000000 11273054221 025644 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/resources/org/freehep/util/template/test/ 0000755 0120103 0120103 00000000000 11273054221 026623 5 ustar mascellani mascellani freehep-util-2.0.2/src/test/resources/org/freehep/util/template/test/test.html 0000644 0120103 0120103 00000000624 10470537377 030512 0 ustar mascellani mascellani
{v:title}
Welcome {v:name} to Simple Template Example
freehep-util-2.0.2/src/site/ 0000755 0120103 0120103 00000000000 11273054221 016622 5 ustar mascellani mascellani freehep-util-2.0.2/src/site/site.xml 0000644 0120103 0120103 00000003150 10502105024 020277 0 ustar mascellani mascellani
FreeHEP Util
http://java.freehep.org/freehep-util
FreeHEP
http://java.freehep.org/images/sm-freehep.gif
http://java.freehep.org/
freehep-util-2.0.2/src/site/apt/ 0000755 0120103 0120103 00000000000 11273054221 017406 5 ustar mascellani mascellani freehep-util-2.0.2/src/site/apt/index.apt 0000644 0120103 0120103 00000000167 10502113066 021224 0 ustar mascellani mascellani ---
FreeHEP Util
---
---
Mark Donszelmann
---
Introduction
General utility classes for FreeHEP.
freehep-util-2.0.2/src/main/ 0000755 0120103 0120103 00000000000 11273054221 016602 5 ustar mascellani mascellani freehep-util-2.0.2/src/main/java/ 0000755 0120103 0120103 00000000000 11273054221 017523 5 ustar mascellani mascellani freehep-util-2.0.2/src/main/java/org/ 0000755 0120103 0120103 00000000000 11273054221 020312 5 ustar mascellani mascellani freehep-util-2.0.2/src/main/java/org/freehep/ 0000755 0120103 0120103 00000000000 11273054221 021730 5 ustar mascellani mascellani freehep-util-2.0.2/src/main/java/org/freehep/util/ 0000755 0120103 0120103 00000000000 11273054221 022705 5 ustar mascellani mascellani freehep-util-2.0.2/src/main/java/org/freehep/util/OptionParser.java 0000644 0120103 0120103 00000001342 10466735775 026224 0 ustar mascellani mascellani package org.freehep.util;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
*
* @author The FreeHEP team @ SLAC
*
*/
public abstract class OptionParser
{
public static Map parseOptions( String options )
{
Map hashValues = new HashMap();
if (options != null)
{
StringTokenizer st = new StringTokenizer(options,",;");
while ( st.hasMoreTokens() )
{
String tk = st.nextToken().toLowerCase().trim();
int pos = tk.indexOf('=');
if (pos < 0) hashValues.put(tk,"true");
else hashValues.put(tk.substring(0,pos).trim(),tk.substring(pos+1).trim());
}
}
return hashValues;
}
}
freehep-util-2.0.2/src/main/java/org/freehep/util/DoubleWithError.java 0000644 0120103 0120103 00000004074 10466735775 026664 0 ustar mascellani mascellani package org.freehep.util;
/**
* A class that encapsulates a value and its error.
* Primarily for use with ScientificFormat
*
* @see ScientificFormat
*
* @author Tony Johnson
* @author Mark Donszelmann
* @version $Id: DoubleWithError.java 8584 2006-08-10 23:06:37Z duns $
*/
public class DoubleWithError
{
public DoubleWithError(double value, double error)
{
this.value = value;
this.error = error;
this.asymmetricError = false;
}
public DoubleWithError(double value, double plusError, double minError)
{
this.value = value;
this.error = plusError;
this.minError = minError;
this.asymmetricError = true;
}
public void setError(double error)
{
this.error = error;
this.asymmetricError = false;
}
public void setError(double plusError, double minError)
{
this.error = plusError;
this.minError = minError;
this.asymmetricError = true;
}
public double getError()
{
// FIXME: what do we return here if this has an asymmetric error
return error;
}
public double getPlusError()
{
return error;
}
public double getMinError()
{
return (asymmetricError) ? minError : error;
}
public boolean hasAsymmetricError()
{
return asymmetricError;
}
public void setValue(double value)
{
this.value = value;
}
public double getValue()
{
return value;
}
public String toString()
{
if (asymmetricError)
{
return String.valueOf(value)+plus+error+minus+minError;
}
else
{
return String.valueOf(value)+plusorminus+error;
}
}
// Not private because used by scientific format
final static char plusorminus = '\u00b1';
final static char plus = '+';
final static char minus = '-';
private double value;
private double error;
private boolean asymmetricError;
private double minError;
}
freehep-util-2.0.2/src/main/java/org/freehep/util/ScientificFormat.java 0000644 0120103 0120103 00000017764 10466735775 027047 0 ustar mascellani mascellani package org.freehep.util;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Locale;
/**
* This code formats numbers in Scientific Notation. The input Number object is returned
* as a ScientificFormated string. There are two output styles: Pure and Standard scientific
* notation. Pure formatted numbers have precisely the number of digits specified by the
* significant digits (sigDig) parameter and always specify a Base 10 Exponential(E).
* Standard formated numbers have the number of digits specified by the significant
* digits (sigDig) parameter but will not have a Base 10 Exponential(E) if the number of digits
* in the mantissa <= maxWidth.
*
* @author Paul Spence
* @author Mark Donszelmann
* @version $Id: ScientificFormat.java 8584 2006-08-10 23:06:37Z duns $
*/
public class ScientificFormat extends Format
{
/**
* The number of significant digits the number is formatted to is recorded by sigDigit.
* The maximum width allowed for the returned String is recorded by MaxWidth
*/
private int sigDigit = 5;
private int maxWidth = 8;
private boolean sciNote = false; //set to true for pure Scientific Notation
private DecimalFormat decimalFormat;
private static final long serialVersionUID = -1182686857248711235L;
public ScientificFormat()
{
}
/**
* Sets the significant digits, maximum allowable width and number formatting style
* (SciNote == true for Pure formatting).
*/
public ScientificFormat(int sigDigit, int maxWidth, boolean SciNote)
{
setSigDigits(sigDigit);
setMaxWidth(maxWidth);
setScientificNotationStyle(SciNote);
}
/**
* Implementation of inherited abstract method. Checks to see if object to be formatted
* is of type Number. If so casts the Number object to double and calls the format method.
* Returns the result.
*/
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
{
if (obj instanceof Number)
{
String result = format(((Number) obj).doubleValue());
return toAppendTo.append(result);
}
else if (obj instanceof DoubleWithError)
{
DoubleWithError dwe = (DoubleWithError) obj;
toAppendTo.append(format(dwe.getValue()));
if (dwe.hasAsymmetricError())
{
toAppendTo.append(DoubleWithError.plus);
int errorSigDigit = resolveErrorSigDigit(dwe.getValue(),dwe.getPlusError());
toAppendTo.append(format(dwe.getPlusError(),errorSigDigit));
toAppendTo.append(DoubleWithError.minus);
errorSigDigit = resolveErrorSigDigit(dwe.getValue(),dwe.getMinError());
toAppendTo.append(format(dwe.getMinError(),errorSigDigit));
}
else
{
toAppendTo.append(DoubleWithError.plusorminus);
int errorSigDigit = resolveErrorSigDigit(dwe.getValue(),dwe.getError());
toAppendTo.append(format(dwe.getError(),errorSigDigit));
}
return toAppendTo;
}
else throw new IllegalArgumentException("Cannot format given Object as a Number");
}
/**Dummy implementation of inherited abstract method.
*/
public Object parseObject(String source, ParsePosition pos)
{
return null;
}
/**
* Returns the number of significant digits
*/
public int getSigDigits()
{
return sigDigit;
}
/**
* Returns the maximum allowable width of formatted number excluding any exponentials
*/
public int getMaxWidth()
{
return maxWidth;
}
/**
* Returns the formatting style: True means Pure scientific formatting, False means standard.
*/
public boolean getScientificNotationStyle()
{
return sciNote;
}
/**
* Sets the number of significant digits for the formatted number
*/
public void setSigDigits(int SigDigit)
{
if (SigDigit < 1) throw new IllegalArgumentException("sigDigit");
sigDigit = SigDigit;
decimalFormat = null;
}
/**
* Sets the maximum allowable length of the formattted number mantissa before exponential notation
* is used.
*/
public void setMaxWidth(int mWidth)
{
if (mWidth < 3) throw new IllegalArgumentException("maxWidth");
maxWidth = mWidth;
}
/**
* Sets the format style used.
* There are two output styles: Pure and Standard scientific
* notation. Pure formatted numbers have precisely the number of digits specified by the
* significant digits (sigDig) parameter and always specify a Base 10 Exponential(E).
* Standard formated numbers have the number of digits specified by the significant
* digits (sigDig) parameter but will not have a Base 10 Exponential(E) if the number of digits
* in the mantissa <= maxWidth.
*/
public void setScientificNotationStyle(boolean sciNote)
{
this.sciNote = sciNote;
}
//simplify method for taking log base 10 of x
private final static double k = 1/Math.log(10);
private double Log10(double x)
{
if (x==0) return 0;
else return Math.log(x)*k;
}
private int resolveErrorSigDigit(double x, double dx)
{
//dx should never be negative
dx = Math.abs(dx);
//make x +ve cause negative doesn't effect sigdigits
x=Math.abs(x);
//these circumstances errorsigdit does equal sigdigit, excluding infinity and Nan which are handled by format
if(dx == 0 || Double.isInfinite(dx) || Double.isNaN(dx) || dx >= x) return sigDigit;
//fail cases for log, method fails to handle
if(x==0||Double.isInfinite(x) || Double.isNaN(x))return sigDigit;
//otherwise solve for cases when dxmaxWidth) return preliminaryResult;
if (exponent<-maxWidth+sigDig+1) return preliminaryResult;
// We need to fix up the result
int sign = preliminaryResult.charAt(0)=='-' ? 1 : 0;
StringBuffer result = new StringBuffer(preliminaryResult.substring(sign,sign+1)+preliminaryResult.substring(sign+2,ePos));
if (exponent >= sigDig)
{
for (int i=sigDig; i 0) result.insert(0,'-');
return result.toString();
}
// /**
// * Format a number plus error using scientific notation
// */
// public String formatError(double d,double dx)
// {
// return format(dx, resolveErrorSigDigit(d, dx));
// }
} freehep-util-2.0.2/src/main/java/org/freehep/util/DoubleHashtable.java 0000644 0120103 0120103 00000017076 10466735775 026640 0 ustar mascellani mascellani // Copyright 2002, SLAC, Stanford, U.S.A.
package org.freehep.util;
import java.io.*;
import java.util.*;
/**
* Stores a hashtable of hashtables, which can be indexed by a key and a subkey.
* Keys and Values can be null.
*
* @author Mark Donszelmann
* @version $Id: DoubleHashtable.java 8584 2006-08-10 23:06:37Z duns $
*/
public class DoubleHashtable extends AbstractCollection implements Serializable {
/**
*
*/
private static final long serialVersionUID = -545653328241864972L;
private Hashtable table;
/**
* creates a hashtable of hashtables
*/
public DoubleHashtable() {
table = new Hashtable();
}
/**
* removes all entries and sub-tables
*/
public void clear() {
table.clear();
}
/**
* removes all entries from a subtable
*/
public void clear(Object key) {
Hashtable subtable = get(key);
if (subtable != null) {
subtable.clear();
}
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("DoubleHashtable.clone() is not (yet) supported.");
}
/**
* @return true if value exists in some sub-table
*/
public boolean contains(Object value) {
if (value == null) value = this;
for (Enumeration e=table.keys(); e.hasMoreElements(); ) {
Hashtable subtable = get(e.nextElement());
if (subtable.contains(value)) return true;
}
return false;
}
/**
* @return true if sub-table exists for key
*/
public boolean containsKey(Object key) {
if (key == null) key = this;
return table.containsKey(key);
}
/**
* @return true if value exists for key and subkey
*/
public boolean containsKey(Object key, Object subKey) {
if (subKey == null) subKey = this;
Hashtable subtable = get(key);
return (subtable != null) ? subtable.containsKey(subKey) : false;
}
/**
* @return enumeration over all values in all sub-tables
*/
public Enumeration elements() {
return new Enumeration() {
private Enumeration subtableEnumeration = table.elements();
private Enumeration valueEnumeration;
private Object nullValue = DoubleHashtable.this;
public boolean hasMoreElements() {
if ((valueEnumeration == null) || (!valueEnumeration.hasMoreElements())) {
if (!subtableEnumeration.hasMoreElements()) {
return false;
}
valueEnumeration = ((Hashtable)subtableEnumeration.nextElement()).elements();
}
return true;
}
public Object nextElement() {
hasMoreElements();
Object value = valueEnumeration.nextElement();
return (value == nullValue) ? null : value;
}
};
}
/**
* @return iterator over all values in all sub-tables
*/
public Iterator iterator() {
return new Iterator() {
private Iterator subtableIterator = table.entrySet().iterator();
private Map subtable;
private Iterator valueIterator;
private Object nullValue = DoubleHashtable.this;
public boolean hasNext() {
if ((valueIterator == null) || (!valueIterator.hasNext())) {
if (!subtableIterator.hasNext()) {
return false;
}
Map.Entry entry = (Map.Entry)subtableIterator.next();
subtable = (Map)entry.getValue();
valueIterator = subtable.entrySet().iterator();
}
return true;
}
public Object next() {
hasNext();
Map.Entry entry = (Map.Entry)valueIterator.next();
Object value = entry.getValue();
return (value == nullValue) ? null : value;
}
public void remove() {
valueIterator.remove();
if (subtable.isEmpty()) {
subtableIterator.remove();
}
}
};
}
/**
* @return sub-table for key
*/
public Hashtable get(Object key) {
if (key == null) key = this;
return (Hashtable)table.get(key);
}
/**
* @return value for key and subkey, null in non-existent or null value was stored
*/
public Object get(Object key, Object subKey) {
if (subKey == null) subKey = this;
Hashtable table = get(key);
Object value = (table==null) ? null : table.get(subKey);
return (value == this) ? null : value;
}
/**
* @return true if table is empty
*/
public boolean isEmpty() {
return table.isEmpty();
}
/**
* @return enumeration of keys in table
*/
public Enumeration keys() {
return table.keys();
}
/**
* @return enumeration in subkeys of sub-table pointed by key, and empty if sub-table does not exist
*/
public Enumeration keys(Object key) {
final Hashtable subtable = get(key);
return new Enumeration() {
private Enumeration subkeys = (subtable == null) ? null : subtable.keys();
private Object nullKey = DoubleHashtable.this;
public boolean hasMoreElements() {
return (subkeys == null) ? false : subkeys.hasMoreElements();
}
public Object nextElement() {
if (subkeys == null) {
throw new NoSuchElementException();
}
Object subkey = subkeys.nextElement();
return (subkey == nullKey) ? null : subkey;
}
};
}
/**
* puts a value in sub-table specified by key and subkey.
*
* @return previous value
*/
public Object put(Object key, Object subKey, Object value) {
// Make sure there exists a subtable
Hashtable subtable = get(key);
if (subtable == null) {
subtable = new Hashtable();
if (key == null) key = this;
table.put(key, subtable);
}
// add entry and handle nulls
if (subKey == null) subKey = this;
if (value == null) value = this;
Object old = subtable.get(subKey);
subtable.put(subKey, value);
// return previous entry
return (old == this) ? null : old;
}
/**
* removes value from sub-table specified by key and subkey.
*
* @return previous value
*/
public Object remove(Object key, Object subKey) {
// look for subtable
Hashtable subtable = get(key);
if (subtable == null) return null;
// remove from subtable
if (subKey == null) subKey = this;
Object old = subtable.remove(subKey);
// remove subtable if needed
if (subtable.isEmpty()) {
if (key == null) key = this;
table.remove(key);
}
// return old value
return (old == this) ? null : old;
}
/**
* @return size of all tables
*/
public int size() {
int size =0;
for (Enumeration e = table.keys(); e.hasMoreElements(); ) {
Object key = e.nextElement();
Hashtable subtable = get(key);
size += subtable.size();
}
return size;
}
/**
* @return a string representation of the table
*/
public String toString() {
return "DoubleHashtable@"+hashCode();
}
}
freehep-util-2.0.2/src/main/java/org/freehep/util/Value.java 0000644 0120103 0120103 00000033261 10513737007 024637 0 ustar mascellani mascellani /*
* Value.java
*
* Created on January 4, 2001, 11:37 AM
*/
package org.freehep.util;
import java.lang.reflect.Constructor;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
/**
* A class that can represent any Java object or primitive. Unlike the
* built-in primitive proxies (Double, Integer etc) it is mutable. It is
* used to allow values to be used without needing overloaded methods for
* each primitive type, and without the overhead of object creation/deletion.
*
* When a value is returned by an Object method it should be assumed to be valid
* only until the next method call to that Object. The use of Value should be
* avoided in multi-threaded environments.
*
* @author tonyj
* @version $Id: Value.java 9133 2006-10-13 16:25:43Z turri $
*/
public class Value {
private int intValue;
private short shortValue;
private long longValue;
private float floatValue;
private double doubleValue;
private boolean boolValue;
private byte byteValue;
private char charValue;
private Object obj;
private Class type;
public final static Class TYPE_INTEGER = Integer.TYPE;
public final static Class TYPE_SHORT = Short.TYPE;
public final static Class TYPE_LONG = Long.TYPE;
public final static Class TYPE_FLOAT = Float.TYPE;
public final static Class TYPE_DOUBLE = Double.TYPE;
public final static Class TYPE_BOOLEAN = Boolean.TYPE;
public final static Class TYPE_BYTE = Byte.TYPE;
public final static Class TYPE_CHAR = Character.TYPE;
public final static Class TYPE_STRING = String.class;
public final static Class TYPE_DATE = Date.class;
public Value() {}
public Value( Value v ) {
setValue(v);
}
public Value setValue(Value v) {
this.type = v.getType();
this.intValue = v.intValue;
this.shortValue = v.shortValue;
this.longValue = v.longValue;
this.floatValue = v.floatValue;
this.doubleValue = v.doubleValue;
this.boolValue = v.boolValue;
this.byteValue = v.byteValue;
this.charValue = v.charValue;
this.obj = v.obj;
return this;
}
/**
* Get the Value's type
* @return The Class of this Value.
*
*/
public Class getType() {
return type;
}
/**
* Set the Value's internal value to an integer.
* @param val The integer value.
* @return The Value object with the given internal value.
*
*/
public Value set(int val) { intValue = val; type = TYPE_INTEGER; return this; }
/**
* Set the Value's internal value to a short.
* @param val The short value.
* @return The Value object with the given internal value.
*
*/
public Value set(short val) { shortValue = val; type = TYPE_SHORT; return this; }
/**
* Set the Value's internal value to a long.
* @param val The long value.
* @return The Value object with the given internal value.
*
*/
public Value set(long val) { longValue = val; type = TYPE_LONG; return this; }
/**
* Set the Value's internal value to a float.
* @param val The float value.
* @return The Value object with the given internal value.
*
*/
public Value set(float val) { floatValue = val; type = TYPE_FLOAT; return this; }
/**
* Set the Value's internal value to a double.
* @param val The double value.
* @return The Value object with the given internal value.
*
*/
public Value set(double val) { doubleValue = val; type = TYPE_DOUBLE; return this; }
/**
* Set the Value's internal value to a boolean.
* @param val The boolean value.
* @return The Value object with the given internal value.
*
*/
public Value set(boolean val){ boolValue = val; type = TYPE_BOOLEAN; return this; }
/**
* Set the Value's internal value to a byte.
* @param val The byte value.
* @return The Value object with the given internal value.
*
*/
public Value set(byte val) { byteValue = val; type = TYPE_BYTE; return this; }
/**
* Set the Value's internal value to a char.
* @param val The char value.
* @return The Value object with the given internal value.
*
*/
public Value set(char val) { charValue = val; type = TYPE_CHAR; return this; }
/**
* Set the Value's internal value to a String.
* @param val The String value.
* @return The Value object with the given internal value.
*
*/
public Value set(String val) { obj = val; type = TYPE_STRING; return this; }
/**
* Set the Value's internal value to a Date.
* @param val The Date value.
* @return The Value object with the given internal value.
*
*/
public Value set(Date val) { obj = val; type = TYPE_DATE; return this; }
/**
* Set the Value's internal value to an Object.
* @param val The Object value.
* @return The Value object with the given internal value.
*
*/
public Value set(Object val) { obj = val; type = obj == null ? Object.class : obj.getClass(); return this; }
/**
* Get the integer value.
* @return The int value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public int getInt() {
if (type == TYPE_INTEGER) return intValue;
else if (type == TYPE_SHORT) return shortValue;
else if (type == TYPE_BYTE) return byteValue;
else throw new ClassCastException( "getInt cannot be called for type "+type.toString());
}
/**
* Get the short value.
* @return The short value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public short getShort() {
if (type == TYPE_SHORT) return shortValue;
else if (type == TYPE_BYTE) return byteValue;
else throw new ClassCastException( "getShort cannot be called for type "+type.toString());
}
/**
* Get the long value.
* @return The long value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public long getLong() {
if (type == TYPE_LONG) return longValue;
else if (type == TYPE_INTEGER) return intValue;
else if (type == TYPE_SHORT) return shortValue;
else if (type == TYPE_BYTE) return byteValue;
else throw new ClassCastException( "getLong cannot be called for type "+type.toString());
}
/**
* Get the float value.
* @return The float value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public float getFloat() {
if (type == TYPE_FLOAT) return floatValue;
else if (type == TYPE_INTEGER) return intValue;
else if (type == TYPE_SHORT) return shortValue;
else if (type == TYPE_LONG) return longValue;
else if (type == TYPE_BYTE) return byteValue;
else throw new ClassCastException( "getFloat cannot be called for type "+type.toString());
}
/**
* Get the double value.
* @return The double value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public double getDouble() {
if (type == TYPE_DOUBLE) return doubleValue;
else if (type == TYPE_INTEGER) return intValue;
else if (type == TYPE_SHORT) return shortValue;
else if (type == TYPE_LONG) return longValue;
else if (type == TYPE_FLOAT) return floatValue;
else if (type == TYPE_BYTE) return byteValue;
else if (type == TYPE_DATE) return ((Date)obj).getTime();
else throw new ClassCastException( "getDouble cannot be called for type "+type.toString());
}
/**
* Get the boolean value.
* @return The boolean value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public boolean getBoolean() {
if (type == TYPE_BOOLEAN) return boolValue;
else throw new ClassCastException( "getBoolean cannot be called for type "+type.toString());
}
/**
* Get the byte value.
* @return The byte value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public byte getByte() {
if (type == TYPE_BYTE) return byteValue;
else throw new ClassCastException( "getByte cannot be called for type "+type.toString());
}
/**
* Get the char value.
* @return The char value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public char getChar() {
if (type == TYPE_CHAR) return charValue;
else throw new ClassCastException( "getChar cannot be called for type "+type.toString());
}
/**
* Get the String value.
* @return The String representation of the internal value.
*
*/
public String getString() {
if (type == TYPE_STRING) return (String)obj;
else if (type == TYPE_INTEGER) return String.valueOf(intValue);
else if (type == TYPE_SHORT) return String.valueOf(shortValue);
else if (type == TYPE_LONG) return String.valueOf(longValue);
else if (type == TYPE_FLOAT) return String.valueOf(floatValue);
else if (type == TYPE_DOUBLE) return String.valueOf(doubleValue);
else if (type == TYPE_BOOLEAN) return String.valueOf(boolValue);
else if (type == TYPE_BYTE) return String.valueOf(byteValue);
else if (type == TYPE_CHAR) return String.valueOf(charValue);
else if (type == TYPE_DATE) return ((Date)obj).toString();
else return obj != null ? obj.toString(): "null";
}
/**
* Get the Date value.
* @return The Date value.
* @exception A ClassCastException is thrown if this Value has incompatible type.
*
*/
public Date getDate() {
if (type == TYPE_DATE) return (Date)obj;
else throw new ClassCastException( "getDate cannot be called for type "+type.toString());
}
/**
* Get the Object value.
* @return The Object value.
*
*/
public Object getObject() {
if (obj != null) return obj;
else if (type == TYPE_INTEGER) return new Integer(intValue);
else if (type == TYPE_SHORT) return new Short(shortValue);
else if (type == TYPE_LONG) return new Long(longValue);
else if (type == TYPE_FLOAT) return new Float(floatValue);
else if (type == TYPE_DOUBLE) return new Double(doubleValue);
else if (type == TYPE_BOOLEAN) return new Boolean(boolValue);
else if (type == TYPE_BYTE) return new Byte(byteValue);
else if (type == TYPE_CHAR) return new Character(charValue);
else return null;
}
/**
* Get the String value.
* @return The String representation of the internal value.
*
*/
public String toString() {
return getString();
}
/**
* Returns an external representation of this value
*/
public String toExternal() {
// FIXME, does not work for arrays...
return type.getName()+":"+getString();
}
/**
* Set to value from the external respresentation
*/
public Value fromExternal(String external) throws IllegalArgumentException {
String[] part = external.split(":", 2);
if (part.length != 2)
throw new IllegalArgumentException(getClass()+": External '"+external+
"'does not contain ':' to separate type from value.");
if (part[0].equals(TYPE_STRING.getName())) {
return set(part[1]);
} else if (part[0].equals(TYPE_SHORT.getName())) {
return set(Short.parseShort(part[1]));
} else if (part[0].equals(TYPE_LONG.getName())) {
return set(Long.parseLong(part[1]));
} else if (part[0].equals(TYPE_FLOAT.getName())) {
return set(Float.parseFloat(part[1]));
} else if (part[0].equals(TYPE_DOUBLE.getName())) {
return set(Double.parseDouble(part[1]));
} else if (part[0].equals(TYPE_BOOLEAN.getName())) {
return set(Boolean.getBoolean(part[1]));
} else if (part[0].equals(TYPE_BYTE.getName())) {
return set(Byte.parseByte(part[1]));
} else if (part[0].equals(TYPE_CHAR.getName())) {
return set(part[1].charAt(0));
} else if (part[0].equals(TYPE_INTEGER.getName())) {
return set(Integer.parseInt(part[1]));
} else if (part[0].equals(TYPE_DATE.getName())) {
try {
return set(new SimpleDateFormat().parse(part[1]));
} catch (ParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
} else if (part[0].equals(Object.class.getName()) && part[1].equals("null")) {
return set((Object)null);
} else {
// FIXME will not work for arrays, which are encoded as "[Lpackagename.classname;"
try {
Class cls = Class.forName(part[0]);
Constructor ctor = cls.getDeclaredConstructor(new Class[] { String.class });
ctor.setAccessible(true);
return set(ctor.newInstance(new Object[] { part[1] }));
} catch (Exception e) {
throw new IllegalArgumentException(getClass()+": Cannot reconstruct value from type: "+part[0]+", "
+"and value "+part[1]+", due to "+e.getMessage());
}
}
}
}
freehep-util-2.0.2/src/main/java/org/freehep/util/StringUtilities.java 0000644 0120103 0120103 00000003377 10466735775 026753 0 ustar mascellani mascellani // Copyright Freehep 2006.
package org.freehep.util;
import java.util.regex.Pattern;
/**
*
* @author Mark Donszelmann
* @version $Id: StringUtilities.java 8584 2006-08-10 23:06:37Z duns $
*/
public class StringUtilities {
private StringUtilities() {
// static methods only
}
public static String replace(CharSequence target, CharSequence replacement, String string) {
return Pattern.compile(quote(target.toString()) /* Pattern.LITERAL jdk 1.4 */).matcher(
string).replaceAll(/* Matcher. jdk 1.4 */ quoteReplacement(replacement.toString()));
}
/* for jdk 1.4 */
private static String quote(String s) {
int slashEIndex = s.indexOf("\\E");
if (slashEIndex == -1)
return "\\Q" + s + "\\E";
StringBuffer sb = new StringBuffer(s.length() * 2);
sb.append("\\Q");
slashEIndex = 0;
int current = 0;
while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
sb.append(s.substring(current, slashEIndex));
current = slashEIndex + 2;
sb.append("\\E\\\\E\\Q");
}
sb.append(s.substring(current, s.length()));
sb.append("\\E");
return sb.toString();
}
/* for jdk 1.4 */
private static String quoteReplacement(String s) {
if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
return s;
StringBuffer sb = new StringBuffer();
for (int i=0; i