You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oro-dev@jakarta.apache.org by df...@apache.org on 2006/04/01 19:31:07 UTC
svn commit: r390702 -
/jakarta/oro/trunk/src/java/org/apache/oro/text/perl/Perl5Util.java
Author: dfs
Date: Sat Apr 1 09:31:03 2006
New Revision: 390702
URL: http://svn.apache.org/viewcvs?rev=390702&view=rev
Log:
Made more match-associated stated thread-local.
Modified:
jakarta/oro/trunk/src/java/org/apache/oro/text/perl/Perl5Util.java
Modified: jakarta/oro/trunk/src/java/org/apache/oro/text/perl/Perl5Util.java
URL: http://svn.apache.org/viewcvs/jakarta/oro/trunk/src/java/org/apache/oro/text/perl/Perl5Util.java?rev=390702&r1=390701&r2=390702&view=diff
==============================================================================
--- jakarta/oro/trunk/src/java/org/apache/oro/text/perl/Perl5Util.java (original)
+++ jakarta/oro/trunk/src/java/org/apache/oro/text/perl/Perl5Util.java Sat Apr 1 09:31:03 2006
@@ -135,8 +135,26 @@
private Perl5Matcher __matcher;
/** The compiled match expression parsing regular expression. */
private Pattern __matchPattern;
- /** The last match from a successful call to a matching method. */
- private ThreadLocal __lastMatch;
+
+ class ThreadState {
+ /** The last match from a successful call to a matching method. */
+ MatchResult lastMatch = null;
+
+ /**
+ * Keeps track of the original input (for postMatch() and preMatch())
+ * methods. This will be discarded if the preMatch() and postMatch()
+ * methods are moved into the MatchResult interface.
+ */
+ Object originalInput = null;
+
+ /**
+ * Keeps track of the begin and end offsets of the original input for
+ * the postMatch() and preMatch() methods.
+ */
+ private int inputBeginOffset = 0, inputEndOffset = 0;
+ }
+
+ private ThreadLocal __threadState;
/**
* A container for temporarily holding the results of a split before
@@ -144,19 +162,6 @@
*/
private ArrayList __splitList;
- /**
- * Keeps track of the original input (for postMatch() and preMatch())
- * methods. This will be discarded if the preMatch() and postMatch()
- * methods are moved into the MatchResult interface.
- */
- private Object __originalInput;
-
- /**
- * Keeps track of the begin and end offsets of the original input for
- * the postMatch() and preMatch() methods.
- */
- private int __inputBeginOffset, __inputEndOffset;
-
/** Used for default return value of post and pre Match() */
private static final String __nullString = "";
@@ -188,7 +193,11 @@
public Perl5Util(PatternCache cache) {
__splitList = new ArrayList();
__matcher = new Perl5Matcher();
- __lastMatch = new ThreadLocal();
+ __threadState = new ThreadLocal() {
+ protected synchronized Object initialValue() {
+ return new ThreadState();
+ }
+ };
__patternCache = cache;
__expressionCache = new CacheLRU(cache.capacity());
__compilePatterns();
@@ -205,6 +214,15 @@
this(new PatternCacheLRU());
}
+
+ /**
+ * A convenience method to take care of typecasting. Wouldn't be
+ * necessary if we we could use Java Generics.
+ **/
+ private ThreadState __getState() {
+ return (ThreadState)__threadState.get();
+ }
+
/**
* Compiles the patterns (currently only the match expression) used to
* parse Perl5 expressions. Right now it initializes __matchPattern.
@@ -330,10 +348,11 @@
result = __matcher.contains(input, __parseMatchExpression(pattern));
if(result) {
- __lastMatch.set(__matcher.getMatch());
- __originalInput = input;
- __inputBeginOffset = 0;
- __inputEndOffset = input.length;
+ ThreadState state = __getState();
+ state.lastMatch = __matcher.getMatch();
+ state.originalInput = input;
+ state.inputBeginOffset = 0;
+ state.inputEndOffset = input.length;
}
return result;
@@ -425,10 +444,11 @@
result = __matcher.contains(input, __parseMatchExpression(pattern));
if(result) {
- __lastMatch.set(__matcher.getMatch());
- __originalInput = input.getInput();
- __inputBeginOffset = input.getBeginOffset();
- __inputEndOffset = input.getEndOffset();
+ ThreadState state = __getState();
+ state.lastMatch = __matcher.getMatch();
+ state.originalInput = input.getInput();
+ state.inputBeginOffset = input.getBeginOffset();
+ state.inputEndOffset = input.getEndOffset();
}
return result;
@@ -447,7 +467,7 @@
* last match found.
*/
public synchronized MatchResult getMatch() {
- return (MatchResult)__lastMatch.get();
+ return __getState().lastMatch;
}
@@ -561,7 +581,7 @@
Util.substitute(result, __matcher, entry._pattern, entry._substitution,
input, entry._numSubstitutions);
- __lastMatch.set(__matcher.getMatch());
+ __getState().lastMatch = __matcher.getMatch();
return subCount;
}
@@ -667,7 +687,7 @@
Util.substitute(result, __matcher, compiledPattern, substitution,
input, numSubstitutions);
- __lastMatch.set(__matcher.getMatch());
+ __getState().lastMatch = __matcher.getMatch();
return subCount;
}
@@ -818,7 +838,7 @@
__splitList.clear();
// Just for the sake of completeness
- __lastMatch.set(currentResult);
+ __getState().lastMatch = currentResult;
}
/**
@@ -955,8 +975,7 @@
* @return The length of the last match found.
*/
public synchronized int length() {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.length();
+ return getMatch().length();
}
/**
@@ -966,8 +985,7 @@
* the entire match itself.
*/
public synchronized int groups() {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.groups();
+ return getMatch().groups();
}
@@ -983,8 +1001,7 @@
* of length 0.
*/
public synchronized String group(int group) {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.group(group);
+ return getMatch().group(group);
}
/**
@@ -1000,8 +1017,7 @@
* use the offset to index an array or String.
*/
public synchronized int begin(int group) {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.begin(group);
+ return getMatch().begin(group);
}
@@ -1016,8 +1032,7 @@
* string will return its start offset.
*/
public synchronized int end(int group) {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.end(group);
+ return getMatch().end(group);
}
@@ -1032,8 +1047,7 @@
* not exist, returns -1.
*/
public synchronized int beginOffset(int group) {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.beginOffset(group);
+ return getMatch().beginOffset(group);
}
/**
@@ -1048,8 +1062,7 @@
* string will return its start offset.
*/
public synchronized int endOffset(int group) {
- MatchResult match = (MatchResult)__lastMatch.get();
- return match.endOffset(group);
+ return getMatch().endOffset(group);
}
/**
@@ -1058,7 +1071,7 @@
* @return A string containing the entire match.
*/
public synchronized String toString() {
- MatchResult match = (MatchResult)__lastMatch.get();
+ MatchResult match = getMatch();
if(match == null)
return null;
return match.toString();
@@ -1071,38 +1084,38 @@
* @return The part of the input following the last match found.
*/
public synchronized String preMatch() {
- int begin;
+ ThreadState state = __getState();
- if(__originalInput == null)
+ if(state.originalInput == null)
return __nullString;
- begin = beginOffset(0);
+ int begin = beginOffset(0);
if(begin <= 0)
return __nullString;
- if(__originalInput instanceof char[]) {
+ if(state.originalInput instanceof char[]) {
char[] input;
- input = (char[])__originalInput;
+ input = (char[])state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(begin > input.length)
begin = input.length;
- return new String(input, __inputBeginOffset, begin);
- } else if(__originalInput instanceof String) {
+ return new String(input, state.inputBeginOffset, begin);
+ } else if(state.originalInput instanceof String) {
String input;
- input = (String)__originalInput;
+ input = (String)state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(begin > input.length())
begin = input.length();
- return input.substring(__inputBeginOffset, begin);
+ return input.substring(state.inputBeginOffset, begin);
}
return __nullString;
@@ -1115,37 +1128,37 @@
* @return The part of the input following the last match found.
*/
public synchronized String postMatch() {
- int end;
+ ThreadState state = __getState();
- if(__originalInput == null)
+ if(state.originalInput == null)
return __nullString;
- end = endOffset(0);
+ int end = endOffset(0);
if(end < 0)
return __nullString;
- if(__originalInput instanceof char[]) {
+ if(state.originalInput instanceof char[]) {
char[] input;
- input = (char[])__originalInput;
+ input = (char[])state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(end >= input.length)
return __nullString;
- return new String(input, end, __inputEndOffset - end);
- } else if(__originalInput instanceof String) {
+ return new String(input, end, state.inputEndOffset - end);
+ } else if(state.originalInput instanceof String) {
String input;
- input = (String)__originalInput;
+ input = (String)state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(end >= input.length())
return __nullString;
- return input.substring(end, __inputEndOffset);
+ return input.substring(end, state.inputEndOffset);
}
return __nullString;
@@ -1162,10 +1175,11 @@
* length array.
*/
public synchronized char[] preMatchCharArray() {
+ ThreadState state = __getState();
int begin;
char[] result = null;
- if(__originalInput == null)
+ if(state.originalInput == null)
return null;
begin = beginOffset(0);
@@ -1173,30 +1187,31 @@
if(begin <= 0)
return null;
- if(__originalInput instanceof char[]) {
+ if(state.originalInput instanceof char[]) {
char[] input;
- input = (char[])__originalInput;
+ input = (char[])state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(begin >= input.length)
begin = input.length;
- result = new char[begin - __inputBeginOffset];
- System.arraycopy(input, __inputBeginOffset, result, 0, result.length);
- } else if(__originalInput instanceof String) {
+ result = new char[begin - state.inputBeginOffset];
+ System.arraycopy(input, state.inputBeginOffset, result, 0,
+ result.length);
+ } else if(state.originalInput instanceof String) {
String input;
- input = (String)__originalInput;
+ input = (String)state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(begin >= input.length())
begin = input.length();
- result = new char[begin - __inputBeginOffset];
- input.getChars(__inputBeginOffset, begin, result, 0);
+ result = new char[begin - state.inputBeginOffset];
+ input.getChars(state.inputBeginOffset, begin, result, 0);
}
return result;
@@ -1213,10 +1228,11 @@
* length array.
*/
public synchronized char[] postMatchCharArray() {
+ ThreadState state = __getState();
int end;
char[] result = null;
- if(__originalInput == null)
+ if(state.originalInput == null)
return null;
end = endOffset(0);
@@ -1224,31 +1240,31 @@
if(end < 0)
return null;
- if(__originalInput instanceof char[]) {
+ if(state.originalInput instanceof char[]) {
int length;
char[] input;
- input = (char[])__originalInput;
+ input = (char[])state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
if(end >= input.length)
return null;
- length = __inputEndOffset - end;
+ length = state.inputEndOffset - end;
result = new char[length];
System.arraycopy(input, end, result, 0, length);
- } else if(__originalInput instanceof String) {
+ } else if(state.originalInput instanceof String) {
String input;
- input = (String)__originalInput;
+ input = (String)state.originalInput;
// Just in case we make sure begin offset is in bounds. It should
// be but we're paranoid.
- if(end >= __inputEndOffset)
+ if(end >= state.inputEndOffset)
return null;
- result = new char[__inputEndOffset - end];
- input.getChars(end, __inputEndOffset, result, 0);
+ result = new char[state.inputEndOffset - end];
+ input.getChars(end, state.inputEndOffset, result, 0);
}
return result;
---------------------------------------------------------------------
To unsubscribe, e-mail: oro-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: oro-dev-help@jakarta.apache.org