You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/09/06 14:27:05 UTC
[13/18] temporarily added a patched version of javolution with fast
collections, because the released version has several bugs (see
https://java.net/jira/browse/JAVOLUTION-106 and
https://java.net/jira/browse/JAVOLUTION-105)
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/test/Perfometer.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/test/Perfometer.java b/commons/marmotta-commons/src/ext/java/javolution/test/Perfometer.java
new file mode 100644
index 0000000..bd9707d
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/test/Perfometer.java
@@ -0,0 +1,271 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.test;
+
+import javolution.context.LogContext;
+import javolution.lang.Configurable;
+import javolution.lang.MathLib;
+import javolution.text.TextBuilder;
+import javolution.util.FastTable;
+
+/**
+ * <p> Utility class to measure the worst case execution time and average
+ * execution time with high precision. Here an example measuring the
+ * worst case execution time of {@link java.util.List#add(int, Object)}
+ * for diverse list implementations.</p>
+ * [code]
+ * Perfometer<Class<? extends List>> insertPerf = new Perfometer<>("java.util.List#add(int, Object)") {
+ * List<Object> list;
+ * Random random;
+ * protected void initialize() throws Exception {
+ * list = getInput().newInstance();
+ * random = new Random(-1); // Use seed to ensure same execution path.
+ * }
+ * protected void run(boolean measure) {
+ * Object obj = new Object();
+ * int i = random.nextInt(list.size() + 1);
+ * if (measure) list.add(i, obj);
+ * }
+ * protected void validate() { // Optional.
+ * assert list.size() == getNbrOfIterations();
+ * }
+ * }
+ * ...
+ * public void testExecutionTime() {
+ * insertPerf.measure(java.util.ArrayList.class, 10000).print();
+ * insertPerf.measure(java.util.LinkedList.class, 10000).print();
+ * insertPerf.measure(javolution.util.FastTable.class, 10000).print();
+ * }
+ * ...
+ * > [INFO] java.util.List#add(int, Object) (10000) for java.util.ArrayList: 590.21450 ns (avg), 8443.0000 ns (wcet#9369)
+ * > [INFO] java.util.List#add(int, Object) (10000) for java.util.LinkedList: 4849.8313 ns (avg), 26536.000 ns (wcet#9863)
+ * > [INFO] java.util.List#add(int, Object) (10000) for javolution.util.FastTable: 217.26300 ns (avg), 534.00000 ns (wcet#8864)
+ * [/code]
+ *
+ * @param <T> the perfometer input type.
+ */
+public abstract class Perfometer<T> {
+
+ /**
+ * Hold the measurement duration in milliseconds (default 1000 ms).
+ */
+ public static final Configurable<Integer> DURATION_MS = new Configurable<Integer>() {
+
+ @Override
+ public String getName() { // Requires since there are multiple configurable fields.
+ return this.getClass().getEnclosingClass().getName()
+ + "#DURATION_MS";
+ }
+
+ @Override
+ protected Integer getDefault() {
+ return 1000;
+ }
+
+ };
+ /**
+ * Indicates if perfometer measurements should be skipped (
+ * e.g. {@code -Djavolution.test.Perfometer#SKIP=true} to skip
+ * performance measurements).
+ * When skipped, {@link #measure} and {@link #print} don't do anything.
+ */
+ public static final Configurable<Boolean> SKIP = new Configurable<Boolean>() {
+
+ @Override
+ public String getName() { // Requires since there are multiple configurable fields.
+ return this.getClass().getEnclosingClass().getName() + "#SKIP";
+ }
+
+ @Override
+ protected Boolean getDefault() {
+ return false;
+ }
+ };
+
+ private final String description;
+ private T input;
+ private long[] times; // Nano-Seconds.
+
+ /**
+ * Creates a perfometer having the specified description.
+ *
+ * @param description the description of the code being measured.
+ */
+ public Perfometer(String description) {
+ this.description = description;
+ }
+
+ /**
+ * Returns the average execution time in seconds.
+ */
+ public double getAvgTimeInSeconds() {
+ if (times == null) return Double.NaN;
+ long sum = 0;
+ for (long time : times) {
+ sum += time;
+ }
+ return sum / 1e9 / times.length;
+ }
+
+ /**
+ * Returns this perfometer description.
+ */
+ public String getDescription() {
+ return description;
+ }
+
+ /**
+ * Returns this perfometer current inputs.
+ */
+ public T getInput() {
+ return input;
+ }
+
+ /**
+ * Returns this perfometer current number of iterations performed.
+ */
+ public int getNbrOfIterations() {
+ return (times != null) ? times.length : 0;
+ }
+
+ /**
+ * Returns the execution times in seconds.
+ */
+ public double[] getTimesInSeconds() {
+ if (times == null) return new double[0];
+ double[] timesSec = new double[times.length];
+ for (int i=0; i < times.length; i++) {
+ timesSec[i] = times[i] / 1e9;
+ }
+ return timesSec;
+ }
+
+ /**
+ * Measures the worst case execution time and average execution time.
+ *
+ * @param input the test input.
+ * @param nbrOfIterations the number of iterations performed on which
+ * the average will be calculated.
+ */
+ public Perfometer<T> measure(T input, int nbrOfIterations) {
+ if (SKIP.get()) return this; // Skip.
+ this.input = input;
+ this.times = new long[nbrOfIterations];
+ long[] calibrations = longArray(nbrOfIterations, Long.MAX_VALUE);
+ long[] measures = longArray(nbrOfIterations, Long.MAX_VALUE);
+ try {
+ long exitTime = System.currentTimeMillis() + DURATION_MS.get();
+ do {
+ // Calibration.
+ initialize();
+ for (int i = 0; i < nbrOfIterations; i++) {
+ long start = System.nanoTime();
+ run(false);
+ long time = System.nanoTime() - start;
+ calibrations[i] = MathLib.min(calibrations[i], time);
+ }
+ // Measurement.
+ initialize();
+ for (int i = 0; i < nbrOfIterations; i++) {
+ long start = System.nanoTime();
+ run(true);
+ long time = System.nanoTime() - start;
+ measures[i] = MathLib.min(measures[i], time);
+ }
+ } while (System.currentTimeMillis() < exitTime);
+ for (int i = 0; i < nbrOfIterations; i++) {
+ times[i] = measures[i] - calibrations[i];
+ }
+ return this;
+ } catch (Exception error) {
+ throw new RuntimeException("Perfometer Exception", error);
+ }
+ }
+
+ /**
+ * Outputs the result.
+ */
+ public void print() {
+ if (Perfometer.SKIP.get()) return;
+ TextBuilder txt = new TextBuilder();
+ txt.append(description).append(" (").append(getNbrOfIterations())
+ .append(") for ").append(input).append(": ");
+ while (txt.length() < 80)
+ txt.append(' ');
+ txt.append(getAvgTimeInSeconds() * 1E9, 8, false, true); // Nano-Seconds.
+ txt.append(" ns (avg), ");
+ txt.append(getWCETinSeconds() * 1E9, 8, false, true); // Nano-Seconds.
+ txt.append(" ns (wcet#").append(getWorstCaseNumber()).append(")");
+ LogContext.info(txt);
+ }
+
+ /**
+ * Outputs the measurements in nanoseconds.
+ */
+ public void printDetails() {
+ if (Perfometer.SKIP.get()) return;
+ FastTable<Long> measurements = new FastTable<Long>();
+ for (long time : times)
+ measurements.add(time);
+ LogContext.debug(measurements);
+ }
+
+ /**
+ * Returns the worst case execution time in seconds.
+ */
+ public double getWCETinSeconds() {
+ if (times == null) return Double.NaN;
+ long wcet = 0;
+ for (long time : times) {
+ if (time > wcet) wcet = time;
+ }
+ return wcet / 1e9;
+ }
+
+ /**
+ * Returns the iteration number having the slowest execution time.
+ */
+ public int getWorstCaseNumber() {
+ if (times == null) return -1;
+ long wcet = 0;
+ int j = -1;
+ for (int i=0; i < times.length; i++) {
+ if (times[i] > wcet) {
+ wcet = times[i];
+ j = i;
+ }
+ }
+ return j;
+ }
+
+ /**
+ * Performs the initialization.
+ */
+ protected abstract void initialize() throws Exception;
+
+ /**
+ * Runs the code being benchmarked.
+ *
+ * @param measure {@code false} when calibration is performed;
+ * {@code true} otherwise.
+ */
+ protected abstract void run(boolean measure) throws Exception;
+
+ /**
+ * Validates the final result (after all iterations are completed).
+ */
+ protected void validate() {}
+
+ private long[] longArray(int length, long initialValue) {
+ long[] array = new long[length];
+ for (int i = 0; i < length; i++)
+ array[i] = initialValue;
+ return array;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/test/package-info.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/test/package-info.java b/commons/marmotta-commons/src/ext/java/javolution/test/package-info.java
new file mode 100644
index 0000000..be22b58
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/test/package-info.java
@@ -0,0 +1,5 @@
+/**
+<p> Testing tools for validation and performance.</p>
+ */
+package javolution.test;
+
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/text/CharArray.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/text/CharArray.java b/commons/marmotta-commons/src/ext/java/javolution/text/CharArray.java
new file mode 100644
index 0000000..320bd00
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/text/CharArray.java
@@ -0,0 +1,393 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.text;
+
+import javolution.util.function.Equalities;
+
+/**
+ * <p> A {@link CharSequence} backed up by a <code>char</code> array.
+ * Instances of this class are
+ * typically used/reused to provide <code>CharSequence</code> views
+ * over existing character buffers.</p>
+ *
+ * <p> Instances of this classes have the following properties:<ul>
+ *
+ * <li> They support equality or lexical comparison with any
+ * <code>CharSequence</code> (e.g. <code>String</code>).</li>
+ *
+ * <li> They have the same hashcode than <code>String</code> and can be
+ * used to retrieve data from maps for which the keys are
+ * <code>String</code> instances.</li>
+ *
+ * <li> They support fast conversions to primitive types
+ * (e.g. {@link #toBoolean() Boolean}, {@link #toInt int}).</li>
+ *
+ * </ul></p>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 5.3, January 10, 2007
+ */
+public final class CharArray implements CharSequence, Comparable<CharSequence> {
+
+ /**
+ * Holds the character array.
+ */
+ private char[] _array;
+
+ /**
+ * Holds the index of the first character.
+ */
+ private int _offset;
+
+ /**
+ * Holds the length of char sequence.
+ */
+ private int _length;
+
+ /**
+ * Default constructor (empty character array).
+ */
+ public CharArray() {
+ _array = NO_CHAR;
+ }
+
+ private static final char[] NO_CHAR = new char[0];
+
+ /**
+ * Creates a character array of specified default capacity.
+ *
+ * @param capacity the backing array default capacity.
+ */
+ public CharArray(int capacity) {
+ _array = new char[capacity];
+ }
+
+ /**
+ * Creates a character array from the specified String.
+ *
+ * @param string the string source.
+ */
+ public CharArray(String string) {
+ _array = string.toCharArray();
+ _length = string.length();
+ }
+
+ /**
+ * Returns the underlying array.
+ *
+ * @return the underlying array.
+ */
+ public char[] array() {
+ return _array;
+ }
+
+ /**
+ * Returns the length of this character sequence.
+ *
+ * @return the number of characters (16-bits Unicode).
+ */
+ public int length() {
+ return _length;
+ }
+
+ /**
+ * Returns the offset of the first character in the underlying array.
+ *
+ * @return the offset of the first character.
+ */
+ public int offset() {
+ return _offset;
+ }
+
+ /**
+ * Sets the underlying array of this CharArray.
+ *
+ * @param offset the new offset.
+ * @param array the new underlying array.
+ * @param length the new length.
+ * @return <code>this</code>
+ */
+ public CharArray setArray(char[] array, int offset, int length) {
+ _array = array;
+ _offset = offset;
+ _length = length;
+ return this;
+ }
+
+ /**
+ * Returns the index within this character sequence of the first occurrence
+ * of the specified characters sequence searching forward.
+ *
+ * @param csq a character sequence searched for.
+ * @return the index of the specified character sequence in the range
+ * <code>[0, length()[</code>
+ * or <code>-1</code> if the character sequence is not found.
+ */
+ public final int indexOf(java.lang.CharSequence csq) {
+ final char c = csq.charAt(0);
+ final int csqLength = csq.length();
+ for (int i = _offset, end = _offset + _length - csqLength + 1; i < end; i++) {
+ if (_array[i] == c) { // Potential match.
+ boolean match = true;
+ for (int j = 1; j < csqLength; j++) {
+ if (_array[i + j] != csq.charAt(j)) {
+ match = false;
+ break;
+ }
+ }
+ if (match) { return i - _offset; }
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Returns the index within this character sequence of the first occurrence
+ * of the specified character searching forward.
+ *
+ * @param c the character to search for.
+ * @return the indext of the specified character in the range
+ * <code>[0, length()[</code>
+ * or <code>-1</code> if the character is not found.
+ */
+ public final int indexOf(char c) {
+ for (int i = _offset, end = _offset + _length; i < end; i++) {
+ if (_array[i] == c)
+ return i - _offset;
+ }
+ return -1;
+ }
+
+ /**
+ * Returns the <code>String</code> corresponding to this character
+ * sequence. The <code>String</code> returned is always allocated on the
+ * heap and can safely be referenced elsewhere.
+ *
+ * @return the <code>java.lang.String</code> for this character sequence.
+ */
+ @Override
+ public String toString() {
+ return new String(_array, _offset, _length);
+ }
+
+ /**
+ * Returns the hash code for this {@link CharArray}.
+ *
+ * <p> Note: Returns the same hashCode as <code>java.lang.String</code>
+ * (consistent with {@link #equals})</p>
+ * @return the hash code value.
+ */
+ @Override
+ public int hashCode() {
+ int h = 0;
+ for (int i = 0, j = _offset; i < _length; i++) {
+ h = 31 * h + _array[j++];
+ }
+ return h;
+ }
+
+ /**
+ * Compares this character sequence against the specified object
+ * (<code>String</code> or <code>CharSequence</code>).
+ *
+ * @param that the object to compare with.
+ * @return <code>true</code> if both objects represent the same sequence;
+ * <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object that) {
+ if (that instanceof String) {
+ return equals((String) that);
+ } else if (that instanceof CharArray) {
+ return equals((CharArray) that);
+ } else if (that instanceof java.lang.CharSequence) {
+ return equals((java.lang.CharSequence) that);
+ } else {
+ return false;
+ }
+ }
+
+ // Do not make public or String instances may not use equals(String)
+ private boolean equals(java.lang.CharSequence chars) {
+ if (chars == null)
+ return false;
+ if (this._length != chars.length())
+ return false;
+ for (int i = _length, j = _offset + _length; --i >= 0;) {
+ if (_array[--j] != chars.charAt(i))
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Compares this character array against the specified {@link CharArray}.
+ *
+ * @param that the character array to compare with.
+ * @return <code>true</code> if both objects represent the same sequence;
+ * <code>false</code> otherwise.
+ */
+ public boolean equals(CharArray that) {
+ if (this == that)
+ return true;
+ if (that == null)
+ return false;
+ if (this._length != that._length)
+ return false;
+ final char[] thatArray = that._array;
+ for (int i = that._offset + _length, j = _offset + _length; --j >= _offset;) {
+ if (_array[j] != thatArray[--i])
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Compares this character array against the specified String.
+ * In case of equality, the CharArray keeps a reference to the
+ * String for future comparisons.
+ *
+ * @param str the string to compare with.
+ * @return <code>true</code> if both objects represent the same sequence;
+ * <code>false</code> otherwise.
+ */
+ public boolean equals(String str) {
+ if (str == null)
+ return false;
+ if (_length != str.length())
+ return false;
+ for (int i = _length, j = _offset + _length; --i >= 0;) {
+ if (_array[--j] != str.charAt(i))
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Compares this character array with the specified character
+ * sequence lexicographically.
+ *
+ * @param seq the character sequence to be compared.
+ * @return <code>{@link Equalities#LEXICAL}.compare(this, seq)</code>
+ * @throws ClassCastException if the specifed object is not a
+ * <code>CharSequence</code>.
+ */
+ public int compareTo(CharSequence seq) {
+ return Equalities.LEXICAL.compare(this, seq);
+ }
+
+ /**
+ * Returns the <code>boolean</code> represented by this character array.
+ *
+ * @return the corresponding <code>boolean</code> value.
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>boolean</code>.
+ */
+ public boolean toBoolean() {
+ return TypeFormat.parseBoolean(this);
+ }
+
+ /**
+ * Returns the decimal <code>int</code> represented by this character array.
+ *
+ * @return <code>toInt(10)</code>
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>int</code>.
+ */
+ public int toInt() {
+ return TypeFormat.parseInt(this);
+ }
+
+ /**
+ * Returns the <code>int</code> represented by this character array
+ * in the specified radix.
+ *
+ * @param radix the radix (e.g. <code>16</code> for hexadecimal).
+ * @return the corresponding <code>int</code> value.
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>int</code>.
+ */
+ public int toInt(int radix) {
+ return TypeFormat.parseInt(this, radix);
+ }
+
+ /**
+ * Returns the decimal <code>long</code> represented by this character
+ * array.
+ *
+ * @return the corresponding <code>long</code> value.
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>long</code>.
+ */
+ public long toLong() {
+ return TypeFormat.parseLong(this);
+ }
+
+ /**
+ * Returns the decimal <code>long</code> represented by this character
+ * array in the specified radix.
+ *
+ * @param radix the radix (e.g. <code>16</code> for hexadecimal).
+ * @return the corresponding <code>long</code> value.
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>long</code>.
+ */
+ public long toLong(int radix) {
+ return TypeFormat.parseLong(this, radix);
+ }
+
+ /**
+ * Returns the <code>float</code> represented by this character array.
+ *
+ * @return the corresponding <code>float</code> value.
+ * @return <code>TypeFormat.parseFloat(this)</code>
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>float</code>.
+ */
+ public float toFloat() {
+ return TypeFormat.parseFloat(this);
+ }
+
+ /**
+ * Returns the <code>double</code> represented by this character array.
+ *
+ * @return the corresponding <code>double</code> value.
+ * @throws NumberFormatException if this character sequence
+ * does not contain a parsable <code>double</code>.
+ */
+ public double toDouble() {
+ return TypeFormat.parseDouble(this);
+ }
+
+ // Implements CharSequence
+ public char charAt(int index) {
+ if ((index < 0) || (index >= _length))
+ throw new IndexOutOfBoundsException("index: " + index);
+ return _array[_offset + index];
+ }
+
+ // Implements CharSequence
+ public java.lang.CharSequence subSequence(int start, int end) {
+ if ((start < 0) || (end < 0) || (start > end) || (end > this.length()))
+ throw new IndexOutOfBoundsException();
+ CharArray chars = new CharArray();
+ chars._array = _array;
+ chars._offset = _offset + start;
+ chars._length = end - start;
+ return chars;
+ }
+
+ // Implements CharSequence
+ public void getChars(int start, int end, char dest[], int destPos) {
+ if ((start < 0) || (end < 0) || (start > end) || (end > _length))
+ throw new IndexOutOfBoundsException();
+ System.arraycopy(_array, start + _offset, dest, destPos, end - start);
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/text/CharSet.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/text/CharSet.java b/commons/marmotta-commons/src/ext/java/javolution/text/CharSet.java
new file mode 100644
index 0000000..414d758
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/text/CharSet.java
@@ -0,0 +1,327 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.text;
+
+import javolution.lang.Immutable;
+import javolution.lang.MathLib;
+
+/**
+ * <p> A set of characters (typically used for parsing purpose where it is
+ * significantly faster than regular expressions for simple patterns).
+ * For example:
+ * [code]
+ * // Integration with Text.
+ * Text number;
+ * int exponentIndex = num.indexOfAny(CharSet.valueOf('e', 'E'));
+ *
+ * // Integration with TextFormat.
+ * public List<Integer> parse(CharSequence csq, Cursor cursor) {
+ * FastTable<Integer> numbers = FastTable.newInstance();
+ * while (cursor.skip(CharSet.WHITESPACES, csq)) {
+ * numbers.add(TypeFormat.parseInt(csq, cursor));
+ * }
+ * return numbers;
+ * } [/code]</p>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 3.7, January 1, 2006
+ */
+public final class CharSet implements Immutable<CharSet> {
+
+ /**
+ * Represents an empty character set.
+ */
+ public static final CharSet EMPTY = new CharSet(new long[0]);
+
+ /**
+ * Represents white spaces characters according to Java
+ * (see {@link Character#isWhitespace(char)}).
+ */
+ public static final CharSet WHITESPACES = CharSet.valueOf(new char[] { 0x9,
+ 0xA, 0xB, 0xC, 0xD, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x1680, 0x180E,
+ 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2008,
+ 0x2009, 0x200A, 0x200B, 0x2028, 0x2029, 0x205F, 0x3000 });
+
+ /**
+ * Represents spaces characters according to Java
+ * (see {@link Character#isSpaceChar(char)}).
+ */
+ public static final CharSet SPACES = CharSet.valueOf(new char[] { 0x20,
+ 0xA0, 0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004,
+ 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x200B, 0x2028,
+ 0x2029, 0x202F, 0x205F, 0x3000 });
+
+ /**
+ * Represents ISO control characters according to Java
+ * (see {@link Character#isISOControl(char)}).
+ */
+ public static final CharSet ISO_CONTROLS = CharSet.valueOf(new char[] {
+ 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC,
+ 0xD, 0xE, 0xF, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x7F, 0x80, 0x81,
+ 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C,
+ 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
+ 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F });
+
+ /**
+ * Holds the containment mapping.
+ */
+ private final long[] _mapping;
+
+ /**
+ * Creates a character set with the specified mapping.
+ *
+ * @param mapping the character set mapping.
+ */
+ private CharSet(long[] mapping) {
+ _mapping = mapping;
+ }
+
+ /**
+ * Returns the character set holding the specified characters.
+ *
+ * @param chars the characters contained by this character set.
+ * @return the corresponding character set.
+ */
+ public static CharSet valueOf(char... chars) {
+ int maxChar = 0;
+ for (int i = chars.length; --i >= 0;) {
+ if (chars[i] > maxChar) {
+ maxChar = chars[i];
+ }
+ }
+ CharSet charSet = new CharSet(new long[(maxChar >> 6) + 1]);
+ for (int i = chars.length; --i >= 0;) {
+ char c = chars[i];
+ charSet._mapping[c >> 6] |= 1L << (c & 63);
+ }
+ return charSet;
+ }
+
+ /**
+ * Returns the character set holding the characters in the specified
+ * range.
+ *
+ * @param first the first character.
+ * @param last the last character.
+ * @return the corresponding character set.
+ * @throws IllegalArgumentException if <code>first > last</code>
+ */
+ public static CharSet rangeOf(char first, char last) {
+ if (first > last)
+ throw new IllegalArgumentException(
+ "first should be less or equal to last");
+ CharSet charSet = new CharSet(new long[(last >> 6) + 1]);
+ for (char c = first; c <= last; c++) {
+ charSet._mapping[c >> 6] |= 1L << (c & 63);
+ }
+ return charSet;
+
+ }
+
+ /**
+ * Indicates if the specified character is contained by this character set.
+ *
+ * @param c the character to test.
+ * @return <code>true</code> if this character set contains the specified
+ * character; <code>false</code> otherwise.
+ */
+ public boolean contains(char c) {
+ final int i = c >> 6;
+ return i < _mapping.length ? (_mapping[i] & (1L << (c & 63))) != 0
+ : false;
+ }
+
+ /**
+ * Equivalent to
+ * {@link #indexIn(java.lang.CharSequence, int) indexIn(csq, 0)}
+ *
+ * @param csq the character sequence to be searched.
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int indexIn(CharSequence csq) {
+ return indexIn(csq, 0);
+ }
+
+ /**
+ * Returns the first index in the specified character sequence of
+ * one of the character of this set.
+ *
+ * @param csq the character sequence to be searched.
+ * @param fromIndex the index to search from.
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int indexIn(CharSequence csq, int fromIndex) {
+ for (int i = fromIndex, n = csq.length(); i < n; i++) {
+ if (contains(csq.charAt(i)))
+ return i;
+ }
+ return -1;
+ }
+
+ /**
+ * Equivalent to {@link #indexIn(char[], int) indexIn(chars, 0)}
+ *
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int indexIn(char[] chars) {
+ return indexIn(chars, 0);
+ }
+
+ /**
+ * Returns the first index in the specified character array of
+ * one of the character of this set.
+ *
+ * @param chars the character array to be searched.
+ * @param fromIndex the index to search from.
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int indexIn(char[] chars, int fromIndex) {
+ for (int i = fromIndex, n = chars.length; i < n; i++) {
+ if (contains(chars[i]))
+ return i;
+ }
+ return -1;
+ }
+
+ /**
+ * Equivalent to
+ * {@link #lastIndexIn(java.lang.CharSequence, int)
+ * lastIndexIn(csq, csq.length()-1)}
+ *
+ * @param csq the character sequence to be searched.
+ * @return the last index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int lastIndexIn(CharSequence csq) {
+ return lastIndexIn(csq, csq.length() - 1);
+ }
+
+ /**
+ * Returns the last index in the specified character sequence of
+ * one of the character of this set.
+ *
+ * @param csq the character sequence to be searched.
+ * @param fromIndex the index to search from (backward).
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int lastIndexIn(CharSequence csq, int fromIndex) {
+ for (int i = fromIndex; i >= 0; --i) {
+ if (contains(csq.charAt(i)))
+ return i;
+ }
+ return -1;
+ }
+
+ /**
+ * Equivalent to {@link #lastIndexIn(char[], int)
+ * lastIndexIn(chars, chars.length-1)}
+ *
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int lastIndexIn(char[] chars) {
+ return lastIndexIn(chars, chars.length - 1);
+ }
+
+ /**
+ * Returns the last index in the specified character array of
+ * one of the character of this set.
+ *
+ * @param chars the character array to be searched.
+ * @param fromIndex the index to search from (backward).
+ * @return the index in the specified character sequence or
+ * <code>-1</code> if none found.
+ */
+ public int lastIndexIn(char[] chars, int fromIndex) {
+ for (int i = fromIndex; i >= 0; i--) {
+ if (contains(chars[i]))
+ return i;
+ }
+ return -1;
+ }
+
+ /**
+ * Returns the character set containing the characters from this
+ * character set plus the characters from the character set specified.
+ *
+ * @param that the set containing the characters to be added.
+ * @return <code>this + that</code>
+ */
+ public CharSet plus(CharSet that) {
+ if (that._mapping.length > this._mapping.length)
+ return that.plus(this);
+ CharSet result = this.copy();
+ for (int i = that._mapping.length; --i >= 0;) {
+ result._mapping[i] |= that._mapping[i];
+ }
+ return result;
+ }
+
+ /**
+ * Returns the character set containing the characters from this
+ * character minus the characters from the character set specified.
+ *
+ * @param that the set containing the character to be removed.
+ * @return <code>this - that</code>
+ */
+ public CharSet minus(CharSet that) {
+ CharSet result = this.copy();
+ for (int i = MathLib.min(this._mapping.length, that._mapping.length); --i >= 0;) {
+ result._mapping[i] &= ~that._mapping[i];
+ }
+ return result;
+ }
+
+ /**
+ * Returns the textual representation of this character set.
+ *
+ * @return the textual representation.
+ */
+ @Override
+ public String toString() {
+ TextBuilder tb = new TextBuilder();
+ tb.append('{');
+ int length = _mapping.length << 6;
+ for (int i = 0; i < length; i++) {
+ if (this.contains((char) i)) {
+ if (tb.length() > 1) {
+ tb.append(',');
+ tb.append(' ');
+ }
+ tb.append('\'');
+ tb.append((char) i);
+ tb.append('\'');
+ }
+ }
+ tb.append('}');
+ return tb.toString();
+ }
+
+ /**
+ * Returns a copy of this character set.
+ */
+ private CharSet copy() {
+ CharSet charSet = new CharSet(new long[this._mapping.length]);
+ for (int i = _mapping.length; --i >= 0;) {
+ charSet._mapping[i] = _mapping[i];
+ }
+ return charSet;
+ }
+
+ @Override
+ public CharSet value() {
+ return this;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/text/Cursor.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/text/Cursor.java b/commons/marmotta-commons/src/ext/java/javolution/text/Cursor.java
new file mode 100644
index 0000000..b27a9b4
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/text/Cursor.java
@@ -0,0 +1,397 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.text;
+
+/**
+ * <p> A parsing cursor over the characters read. Cursor
+ * allows for token iterations over any {@link CharSequence}.
+ * [code]
+ * String str = "this is a test";
+ * Cursor cursor = new Cursor();
+ * for (CharSequence token; (token=cursor.nextToken(str, ' '))!= null;)
+ * System.out.println(token);
+ * [/code]
+ * Prints the following output:<pre>
+ * this
+ * is
+ * a
+ * test</pre>
+ * Cursors are typically used with {@link TextFormat} instances.
+ * [code]
+ * // Parses decimal number (e.g. "xxx.xxxxxExx" or "NaN")
+ * public Decimal parse(CharSequence csq, Cursor cursor) throws IllegalArgumentException {
+ * TextFormat<LargeInteger> largeIntegerFormat = TextContext.getFormat(LargeInteger.class);
+ * if (cursor.skip("NaN", csq))
+ * return Decimal.NaN;
+ * LargeInteger significand = LargeIntegerFormat.parse(csq, cursor);
+ * LargeInteger fraction = cursor.skip('.', csq) ? largeIntegerFormat.parse(csq, cursor) : LargeInteger.ZERO;
+ * int exponent = cursor.skip(CharSet.valueOf('E', 'e'), csq) ? TypeFormat.parseInt(csq, cursor) : 0;
+ * int fractionDigits = fraction.digitLength();
+ * return Decimal.valueOf(significand.E(fractionDigits).plus(fraction), exponent - fractionDigits);
+ * }
+ * [/code]
+ * </p>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 5.4, November 19, 2009
+ */
+public class Cursor {
+
+ /**
+ * Holds the index.
+ */
+ private int index;
+
+ /**
+ * Default constructor.
+ */
+ public Cursor() {}
+
+ /**
+ * Returns this cursor index.
+ *
+ * @return the index of the next character to parse.
+ */
+ public final int getIndex() {
+ return index;
+ }
+
+ /**
+ * Sets the cursor current index.
+ *
+ * @param i the index of the next character to parse.
+ */
+ public final void setIndex(int i) {
+ index = i;
+ }
+
+ /**
+ * Indicates if this cursor points to the end of the specified
+ * character sequence.
+ *
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>getIndex() >= csq.length()</code>
+ */
+ public final boolean atEnd(CharSequence csq) {
+ return index >= csq.length();
+ }
+
+ /**
+ * Indicates if this cursor points to the specified character in the
+ * specified character sequence.
+ *
+ * @param c the character to test.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>csq.charAt(this.getIndex()) == c</code>
+ */
+ public final boolean at(char c, CharSequence csq) {
+ return index < csq.length() ? csq.charAt(index) == c : false;
+ }
+
+ /**
+ * Indicates if this cursor points to any of the specified character in the
+ * specified character sequence.
+ *
+ * @param charSet any of the character to test.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>csq.charAt(this.getIndex()) == c</code>
+ */
+ public final boolean at(CharSet charSet, CharSequence csq) {
+ return index < csq.length() ? charSet.contains(csq.charAt(index))
+ : false;
+ }
+
+ /**
+ * Indicates if this cursor points to the specified characters in the
+ * specified sequence.
+ *
+ * @param str the characters to test.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>true</code> if this cursor points to the specified
+ * characters; <code>false</code> otherwise.
+ */
+ public final boolean at(String str, CharSequence csq) {
+ int i = index;
+ int length = csq.length();
+ for (int j = 0; j < str.length();) {
+ if ((i >= length) || (str.charAt(j++) != csq.charAt(i++)))
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Returns the current character at this cursor position.
+ *
+ * @param csq the character sequence iterated by this cursor.
+ * @return the current character this cursor points to.
+ * @throws IndexOutOfBoundsException if {@link #atEnd this.atEnd(csq)}
+ */
+ public final char currentChar(CharSequence csq) {
+ return csq.charAt(index);
+ }
+
+ /**
+ * Returns the next character at this cursor position.The cursor
+ * position is incremented by one.
+ *
+ * @param csq the character sequence iterated by this cursor.
+ * @return the next character this cursor points to.
+ * @throws IndexOutOfBoundsException if {@link #atEnd this.atEnd(csq)}
+ */
+ public final char nextChar(CharSequence csq) {
+ return csq.charAt(index++);
+ }
+
+ /**
+ * Moves this cursor forward until it points to a character
+ * different from the specified character.
+ *
+ * @param c the character to skip.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>true</code> if this cursor has skipped at least one
+ * character;<code>false</code> otherwise (e.g. end of sequence
+ * reached).
+ */
+ public final boolean skipAny(char c, CharSequence csq) {
+ int i = index;
+ int n = csq.length();
+ for (; (i < n) && (csq.charAt(i) == c); i++) {}
+ if (i == index)
+ return false; // Cursor did not moved.
+ index = i;
+ return true;
+ }
+
+ /**
+ * Moves this cursor forward until it points to a character
+ * different from any of the character in the specified set.
+ * For example: [code]
+ * // Reads numbers separated by tabulations or spaces.
+ * FastTable<Integer> numbers = new FastTable<Integer>();
+ * while (cursor.skipAny(CharSet.SPACE_OR_TAB, csq)) {
+ * numbers.add(TypeFormat.parseInt(csq, cursor));
+ * }[/code]
+ *
+ * @param charSet the character to skip.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>true</code> if this cursor has skipped at least one
+ * character;<code>false</code> otherwise (e.g. end of sequence
+ * reached).
+ */
+ public final boolean skipAny(CharSet charSet, CharSequence csq) {
+ int i = index;
+ int n = csq.length();
+ for (; (i < n) && charSet.contains(csq.charAt(i)); i++) {}
+ if (i == index)
+ return false; // Cursor did not moved.
+ index = i;
+ return true;
+ }
+
+ /**
+ * Moves this cursor forward only if at the specified character.
+ * This method is equivalent to:
+ * [code]
+ * if (at(c, csq))
+ * increment();
+ * [/code]
+ *
+ * @param c the character to skip.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>true</code> if this cursor has skipped the specified
+ * character;<code>false</code> otherwise.
+ */
+ public final boolean skip(char c, CharSequence csq) {
+ if (this.at(c, csq)) {
+ index++;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Moves this cursor forward only if at any of the specified character.
+ * This method is equivalent to:
+ * [code]
+ * if (at(charSet, csq))
+ * increment();
+ * [/code]
+ *
+ * @param charSet holding the characters to skip.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>true</code> if this cursor has skipped any the specified
+ * character;<code>false</code> otherwise.
+ */
+ public final boolean skip(CharSet charSet, CharSequence csq) {
+ if (this.at(charSet, csq)) {
+ index++;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Moves this cursor forward only if at the specified string.
+ * This method is equivalent to:
+ * [code]
+ * if (at(str, csq))
+ * increment(str.length());
+ * [/code]
+ *
+ * @param str the string to skip.
+ * @param csq the character sequence iterated by this cursor.
+ * @return <code>true</code> if this cursor has skipped the specified
+ * string;<code>false</code> otherwise (e.g. end of sequence
+ * reached).
+ */
+ public final boolean skip(String str, CharSequence csq) {
+ if (this.at(str, csq)) {
+ index += str.length();
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns the subsequence from the specified cursor position not holding
+ * the specified character. For example:[code]
+ * CharSequence csq = "This is a test";
+ * for (CharSequence token; (token=cursor.nextToken(csq, ' '))!= null;) {
+ * System.out.println(token); // Prints one word at a time.
+ * }[/code]
+ *
+ * @param csq the character sequence iterated by this cursor.
+ * @param c the character being skipped.
+ * @return the subsequence not holding the specified character or
+ * <code>null</code> if none.
+ */
+ public final CharSequence nextToken(CharSequence csq, char c) {
+ int n = csq.length();
+ for (int i = index; i < n; i++) {
+ if (csq.charAt(i) != c) {
+ int j = i;
+ for (; (++j < n) && (csq.charAt(j) != c);) {
+ // Loop until j at the end of sequence or at specified character.
+ }
+ index = j;
+ return csq.subSequence(i, j);
+ }
+ }
+ index = n;
+ return null;
+ }
+
+ /**
+ * Returns the subsequence from the specified cursor position not holding
+ * any of the characters specified. For example:[code]
+ * CharSequence csq = "This is a test";
+ * for (CharSequence token; (token=cursor.nextToken(csq, CharSet.WHITESPACE))!= null;) {
+ * System.out.println(token); // Prints one word at a time.
+ * }[/code]
+ *
+ * @param csq the character sequence iterated by this cursor.
+ * @param charSet the characters being skipped.
+ * @return the subsequence not holding the specified character or
+ * <code>null</code> if none.
+ */
+ public final CharSequence nextToken(CharSequence csq, CharSet charSet) {
+ int n = csq.length();
+ for (int i = index; i < n; i++) {
+ if (!charSet.contains(csq.charAt(i))) {
+ int j = i;
+ for (; (++j < n) && !charSet.contains(csq.charAt(j));) {
+ // Loop until j at the end of sequence or at specified characters.
+ }
+ index = j;
+ return csq.subSequence(i, j);
+ }
+ }
+ index = n;
+ return null;
+ }
+
+ /**
+ * Returns the head of the specified character sequence until
+ * this cursor position.
+ *
+ * @return the corresponding sub-sequence.
+ */
+ public final CharSequence head(CharSequence csq) {
+ return csq.subSequence(0, index);
+ }
+
+ /**
+ * Returns the tail of the specified character sequence starting at
+ * this cursor position.
+ *
+ * @return the corresponding sub-sequence.
+ */
+ public final CharSequence tail(CharSequence csq) {
+ return csq.subSequence(index, csq.length());
+ }
+
+ /**
+ * Increments the cursor index by one.
+ *
+ * @return <code>this</code>
+ */
+ public final Cursor increment() {
+ return increment(1);
+ }
+
+ /**
+ * Increments the cursor index by the specified value.
+ *
+ * @param i the increment value.
+ * @return <code>this</code>
+ */
+ public final Cursor increment(int i) {
+ index += i;
+ return this;
+ }
+
+ /**
+ * Returns the string representation of this cursor.
+ *
+ * @return the index value as a string.
+ */
+ @Override
+ public String toString() {
+ return "Cursor: " + index;
+ }
+
+ /**
+ * Indicates if this cursor is equals to the specified object.
+ *
+ * @return <code>true</code> if the specified object is a cursor
+ * at the same index; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null)
+ return false;
+ if (!(obj instanceof Cursor))
+ return false;
+ return index == ((Cursor) obj).index;
+ }
+
+ /**
+ * Returns the hash code for this cursor.
+ *
+ * @return the hash code value for this object
+ */
+ @Override
+ public int hashCode() {
+ return index;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/text/DefaultTextFormat.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/text/DefaultTextFormat.java b/commons/marmotta-commons/src/ext/java/javolution/text/DefaultTextFormat.java
new file mode 100644
index 0000000..98cdc87
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/text/DefaultTextFormat.java
@@ -0,0 +1,60 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.text;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * <p> Specifies the default text format of a class (for parsing/formatting).
+ * The default format is typically used by the {@link Object#toString()}
+ * method and can be locally overridden in the scope of a
+ * {@link javolution.text.TextContext TextContext}.</p>
+ *
+ * [code]
+ * @DefaultTextFormat(Complex.Cartesian.class)
+ * public class Complex {
+ * public String toString() { // Uses the default format unless locally overridden.
+ * return TextContext.toString(this);
+ * }
+ * public static Complex valueOf(CharSequence csq) {
+ * return TextContext.parse(csq, Complex.class);
+ * }
+ * public static class Cartesian extends TextFormat<Complex> { ... }
+ * public static class Polar extends TextFormat<Complex> { ... }
+ * }
+ * ...
+ * TextContext ctx = TextContext.enter(); // Enters a local textual context.
+ * try {
+ * ctx.setFormat(Complex.class, new Complex.Polar()); // Configure the local context.
+ * System.out.println(complexMatrix); // Displays complex numbers in polar coordinates.
+ * } finally {
+ * ctx.exit(); // Exits local context (reverts to previous Cartesian format).
+ * }[/code]
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+@Documented
+@Inherited
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface DefaultTextFormat {
+
+ /**
+ * Returns the default text format of the annotated class.
+ */
+ Class<? extends TextFormat<?>> value();
+
+}