Author: niallp
Date: Wed Feb 3 00:20:01 2010
New Revision: 905846
URL: http://svn.apache.org/viewvc?rev=905846&view=rev
Log:
LANG-506 Entities - missing final modifiers; thread-safety issues - thanks to
Sebb for the patch
Modified:
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/Entities.java
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesPerformanceTest.java
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesTest.java
Modified:
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/Entities.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/Entities.java?rev=905846&r1=905845&r2=905846&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/Entities.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/Entities.java
Wed Feb 3 00:20:01 2010
@@ -445,6 +445,7 @@
/**
* {...@inheritdoc}
*/
+ // TODO not thread-safe as there is a window between changing the two
maps
public void add(String name, int value) {
mapNameToValue.put(name, new Integer(value));
mapValueToName.put(value, name);
@@ -470,9 +471,20 @@
}
static abstract class MapIntMap implements Entities.EntityMap {
- protected Map mapNameToValue;
+ protected final Map mapNameToValue;
- protected Map mapValueToName;
+ protected final Map mapValueToName;
+
+ /**
+ * Construct a new instance with specified maps.
+ *
+ * @param nameToValue name to value map
+ * @param valueToName value to namee map
+ */
+ MapIntMap(Map nameToValue, Map valueToName){
+ mapNameToValue = nameToValue;
+ mapValueToName = valueToName;
+ }
/**
* {...@inheritdoc}
@@ -506,8 +518,7 @@
* Constructs a new instance of <code>HashEntityMap</code>.
*/
public HashEntityMap() {
- mapNameToValue = new HashMap();
- mapValueToName = new HashMap();
+ super(new HashMap(), new HashMap());
}
}
@@ -516,15 +527,15 @@
* Constructs a new instance of <code>TreeEntityMap</code>.
*/
public TreeEntityMap() {
- mapNameToValue = new TreeMap();
- mapValueToName = new TreeMap();
+ super(new TreeMap(), new TreeMap());
}
}
static class LookupEntityMap extends PrimitiveEntityMap {
+ // TODO this class is not thread-safe
private String[] lookupTable;
- private int LOOKUP_TABLE_SIZE = 256;
+ private final int LOOKUP_TABLE_SIZE = 256;
/**
* {...@inheritdoc}
@@ -564,7 +575,8 @@
}
static class ArrayEntityMap implements EntityMap {
- protected int growBy = 100;
+ // TODO this class is not thread-safe
+ protected final int growBy;
protected int size = 0;
@@ -576,6 +588,7 @@
* Constructs a new instance of <code>ArrayEntityMap</code>.
*/
public ArrayEntityMap() {
+ this.growBy = 100;
names = new String[growBy];
values = new int[growBy];
}
@@ -648,6 +661,8 @@
static class BinaryEntityMap extends ArrayEntityMap {
+ // TODO - not thread-safe, because parent is not. Also references size.
+
/**
* Constructs a new instance of <code>BinaryEntityMap</code>.
*/
@@ -722,8 +737,16 @@
}
}
+ private final EntityMap map;
+
+ public Entities(){
+ map = new Entities.LookupEntityMap();
+ }
+
// package scoped for testing
- EntityMap map = new Entities.LookupEntityMap();
+ Entities(EntityMap emap){
+ map = emap;
+ }
/**
* <p>
Modified:
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesPerformanceTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesPerformanceTest.java?rev=905846&r1=905845&r2=905846&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesPerformanceTest.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesPerformanceTest.java
Wed Feb 3 00:20:01 2010
@@ -108,9 +108,7 @@
}
private Entities build(Entities.EntityMap intMap) {
- Entities entities;
- entities = new Entities();
- entities.map = intMap;
+ Entities entities = new Entities(intMap);
Entities.fillWithHtml40Entities(entities);
return entities;
}
Modified:
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesTest.java?rev=905846&r1=905845&r2=905846&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesTest.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/EntitiesTest.java
Wed Feb 3 00:20:01 2010
@@ -177,8 +177,7 @@
public void testHtml40Nbsp() throws Exception
{
assertEquals(" ", Entities.HTML40.escape("\u00A0"));
- Entities e = new Entities();
- e.map = new Entities.PrimitiveEntityMap();
+ Entities e = new Entities(new Entities.PrimitiveEntityMap());
Entities.fillWithHtml40Entities(e);
assertEquals(" ", e.escape("\u00A0"));
}