Sebb pointed out that the implementation for LANG-507 was not thread safe. Rewriting to pass parameters in to the constructor, but doing so in an experimental way - comments very much desired on whether this makes for a nice API or not

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@826514 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-10-18 20:14:30 +00:00
parent b3cec0c2c3
commit 0fd4b3afe1
2 changed files with 15 additions and 7 deletions

View File

@ -19,6 +19,9 @@ package org.apache.commons.lang.text.translate;
import java.io.IOException;
import java.io.Writer;
import java.util.EnumSet;
import java.util.Arrays;
/**
* Translates escaped unicode values of the form \\u+\d\d\d\d back to
* unicode.
@ -26,13 +29,18 @@ import java.io.Writer;
*/
public class UnicodeUnescaper extends CharSequenceTranslator {
private boolean escapingPlus = false;
public static enum PARAM { escapePlus };
public void setEscapingPlus(boolean b) {
this.escapingPlus = b;
private EnumSet<PARAM> params;
public UnicodeUnescaper(PARAM... params) {
if(params.length > 0) {
this.params = EnumSet.copyOf(Arrays.asList(params));
}
}
public boolean isEscapingPlus() {
return this.escapingPlus;
public boolean isSet(PARAM p) {
return (params == null) ? false : params.contains(p);
}
/**
@ -50,7 +58,7 @@ public class UnicodeUnescaper extends CharSequenceTranslator {
}
// consume + symbol in \\u+0045
if(isEscapingPlus()) {
if(isSet(PARAM.escapePlus)) {
if( (index + i < input.length()) && (input.charAt(index + i) == '+') ) {
i++;
}

View File

@ -36,7 +36,7 @@ public class UnicodeUnescaperTest extends TestCase {
// expected
}
uu.setEscapingPlus(true);
uu = new UnicodeUnescaper(UnicodeUnescaper.PARAM.escapePlus);
assertEquals("Failed to unescape unicode characters with 'u+' notation", "G", uu.translate(input));
}