You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2008/08/03 04:59:30 UTC
svn commit: r682096 - in
/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util:
collections/FlexibleMapAccessor.java
collections/FlexibleServletAccessor.java collections/MapComparator.java
string/FlexibleStringExpander.java
Author: doogie
Date: Sat Aug 2 19:59:30 2008
New Revision: 682096
URL: http://svn.apache.org/viewvc?rev=682096&view=rev
Log:
Final set of generics markup for base; base no longer has any
'unchecked' warnings.
Modified:
ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleMapAccessor.java
ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleServletAccessor.java
ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/MapComparator.java
ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java
Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleMapAccessor.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleMapAccessor.java?rev=682096&r1=682095&r2=682096&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleMapAccessor.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleMapAccessor.java Sat Aug 2 19:59:30 2008
@@ -27,6 +27,10 @@
import javolution.util.FastMap;
import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.UtilGenerics;
+import static org.ofbiz.base.util.UtilGenerics.cast;
+import static org.ofbiz.base.util.UtilGenerics.checkList;
+import static org.ofbiz.base.util.UtilGenerics.checkMap;
import org.ofbiz.base.util.UtilMisc;
/**
@@ -35,7 +39,7 @@
* list elements. See individual Map operations for more information.
*
*/
-public class FlexibleMapAccessor implements Serializable {
+public class FlexibleMapAccessor<T> implements Serializable {
public static final String module = FlexibleMapAccessor.class.getName();
protected String original;
@@ -111,7 +115,7 @@
* @param base
* @return
*/
- public Object get(Map base) {
+ public T get(Map<String, ? extends Object> base) {
return get(base, null);
}
@@ -122,16 +126,16 @@
* @param locale Optional locale parameter, if null will see if the base Map contains a "locale" key
* @return
*/
- public Object get(Map base, Locale locale) {
+ public T get(Map<String, ? extends Object> base, Locale locale) {
if (base == null) {
return null;
}
// so we can keep the passed context
- Map newBase = null;
+ Map<String, ? extends Object> newBase = null;
if (this.subMapAccessor != null) {
try {
- newBase = this.subMapAccessor.getSubMap(base, false);
+ newBase = this.subMapAccessor.getSubMap(base);
} catch (Exception e) {
String errMsg = "Error getting map accessor sub-map [" + this.subMapAccessor.extName + "] as part of [" + this.original + "]: " + e.toString();
Debug.logError(e, errMsg, module);
@@ -143,9 +147,9 @@
}
try {
- Object ret = null;
+ T ret = null;
if (this.isListReference) {
- List lst = (List) newBase.get(this.extName);
+ List<T> lst = checkList(newBase.get(this.extName));
if (lst == null) {
return null;
}
@@ -170,12 +174,12 @@
}
}
- protected Object getByLocale(String name, Map base, Map sub, Locale locale) {
+ protected T getByLocale(String name, Map<String, ? extends Object> base, Map<String, ? extends Object> sub, Locale locale) {
if (sub == null) {
return null;
}
if (sub instanceof LocalizedMap) {
- LocalizedMap locMap = (LocalizedMap) sub;
+ LocalizedMap<T> locMap = cast(sub);
if (locale != null) {
return locMap.get(name, locale);
} else if (base.containsKey("locale")) {
@@ -184,7 +188,7 @@
return locMap.get(name, Locale.getDefault());
}
} else {
- Object getReturn = sub.get(name);
+ T getReturn = UtilGenerics.<T>cast(sub.get(name));
return getReturn;
}
}
@@ -197,19 +201,19 @@
* @param base
* @param value
*/
- public void put(Map base, Object value) {
+ public void put(Map<String, Object> base, T value) {
if (base == null) {
throw new IllegalArgumentException("Cannot put a value in a null base Map");
}
if (this.subMapAccessor != null) {
- Map subBase = this.subMapAccessor.getSubMap(base, true);
+ Map<String, Object> subBase = this.subMapAccessor.getOrCreateSubMap(base);
if (subBase == null) {
return;
}
base = subBase;
}
if (this.isListReference) {
- List lst = (List) base.get(this.extName);
+ List<Object> lst = checkList(base.get(this.extName));
if (lst == null) {
lst = FastList.newInstance();
base.put(this.extName, lst);
@@ -236,15 +240,16 @@
* @param base the Map to remove from
* @return the object removed
*/
- public Object remove(Map base) {
+ public T remove(Map<String, ? extends Object> base) {
if (this.subMapAccessor != null) {
- base = this.subMapAccessor.getSubMap(base, false);
+ base = this.subMapAccessor.getSubMap(base);
}
+ if (base == null) return null;
if (this.isListReference) {
- List lst = (List) base.get(this.extName);
- return lst.remove(this.listIndex);
+ List<Object> lst = checkList(base.get(this.extName));
+ return UtilGenerics.<T>cast(lst.remove(this.listIndex));
} else {
- return base.remove(this.extName);
+ return UtilGenerics.<T>cast(base.remove(this.extName));
}
}
@@ -295,35 +300,62 @@
}
}
- public Map getSubMap(Map base, boolean forPut) {
+ public <V> Map<String, V> getSubMap(Map<String, V> base) {
if (base == null) return null;
if (this.extName == null) return null;
if (this.subMapAccessor != null) {
- base = this.subMapAccessor.getSubMap(base, forPut);
+ base = this.subMapAccessor.getSubMap(base);
+ }
+ if (base == null) return null;
+ Object namedObj = base.get(this.extName);
+ if (this.isListReference && (namedObj == null || namedObj instanceof List)) {
+ List<? extends Object> lst = checkList(namedObj);
+ if (lst == null) return null;
+
+ Map<String, V> extMap = null;
+ if (lst.size() > this.listIndex) {
+ extMap = checkMap(lst.get(this.listIndex));
+ }
+ if (extMap == null) return null;
+
+ return extMap;
+ } else if (namedObj instanceof Map) {
+ Map<String, V> extMap = checkMap(namedObj);
+ return extMap;
+ } else {
+ return null;
+ }
+ }
+
+ public Map<String, Object> getOrCreateSubMap(Map<String, Object> base) {
+ if (base == null) return null;
+ if (this.extName == null) return null;
+ if (this.subMapAccessor != null) {
+ base = this.subMapAccessor.getOrCreateSubMap(base);
}
Object namedObj = base.get(this.extName);
if (this.isListReference && (namedObj == null || namedObj instanceof List)) {
- List lst = (List) base.get(this.extName);
+ List<Object> lst = checkList(namedObj);
if (lst == null) {
lst = FastList.newInstance();
base.put(this.extName, lst);
}
- Map extMap = null;
+ Map<String, Object> extMap = null;
if (lst.size() > this.listIndex) {
- extMap = (Map) lst.get(this.listIndex);
+ extMap = checkMap(lst.get(this.listIndex));
}
- if (forPut && extMap == null) {
+ if (extMap == null) {
extMap = FastMap.newInstance();
lst.add(this.listIndex, extMap);
}
return extMap;
} else if (namedObj == null || namedObj instanceof Map) {
- Map extMap = (Map) namedObj;
+ Map<String, Object> extMap = checkMap(namedObj);
// this code creates a new Map if none is missing, but for a get or remove this is a bad idea...
- if (forPut && extMap == null) {
+ if (extMap == null) {
extMap = FastMap.newInstance();
base.put(this.extName, extMap);
}
Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleServletAccessor.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleServletAccessor.java?rev=682096&r1=682095&r2=682096&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleServletAccessor.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/FlexibleServletAccessor.java Sat Aug 2 19:59:30 2008
@@ -34,11 +34,11 @@
* list elements. See individual Map operations for more information.
*
*/
-public class FlexibleServletAccessor implements Serializable {
+public class FlexibleServletAccessor<T> implements Serializable {
protected String name;
protected String attributeName;
- protected FlexibleMapAccessor fma;
+ protected FlexibleMapAccessor<T> fma;
protected boolean needsExpand;
protected boolean empty;
@@ -59,7 +59,7 @@
if (name == null || name.length() == 0) {
empty = true;
needsExpand = false;
- fma = new FlexibleMapAccessor(name);
+ fma = new FlexibleMapAccessor<T>(name);
attributeName = name;
} else {
empty = false;
@@ -72,7 +72,7 @@
int dotIndex = name.indexOf('.');
if (dotIndex != -1) {
attributeName = name.substring(0, dotIndex);
- fma = new FlexibleMapAccessor(name.substring(dotIndex+1));
+ fma = new FlexibleMapAccessor<T>(name.substring(dotIndex+1));
} else {
attributeName = name;
fma = null;
@@ -92,8 +92,8 @@
* @param expandContext the context to use for name expansion
* @return the object corresponding to this getter class
*/
- public Object get(ServletRequest request, Map<String, Object> expandContext) {
- AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
+ public T get(ServletRequest request, Map<String, Object> expandContext) {
+ AttributeAccessor<T> aa = new AttributeAccessor<T>(name, expandContext, this.attributeName, this.fma, this.needsExpand);
return aa.get(request);
}
@@ -102,8 +102,8 @@
* @param expandContext
* @return
*/
- public Object get(HttpSession session, Map<String, Object> expandContext) {
- AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
+ public T get(HttpSession session, Map<String, Object> expandContext) {
+ AttributeAccessor<T> aa = new AttributeAccessor<T>(name, expandContext, this.attributeName, this.fma, this.needsExpand);
return aa.get(session);
}
@@ -116,8 +116,8 @@
* @param value
* @param expandContext
*/
- public void put(ServletRequest request, Object value, Map<String, Object> expandContext) {
- AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
+ public void put(ServletRequest request, T value, Map<String, Object> expandContext) {
+ AttributeAccessor<T> aa = new AttributeAccessor<T>(name, expandContext, this.attributeName, this.fma, this.needsExpand);
aa.put(request, value);
}
@@ -130,8 +130,8 @@
* @param value
* @param expandContext
*/
- public void put(HttpSession session, Object value, Map<String, Object> expandContext) {
- AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
+ public void put(HttpSession session, T value, Map<String, Object> expandContext) {
+ AttributeAccessor<T> aa = new AttributeAccessor<T>(name, expandContext, this.attributeName, this.fma, this.needsExpand);
aa.put(session, value);
}
@@ -140,8 +140,8 @@
* @param expandContext
* @return
*/
- public Object remove(ServletRequest request, Map<String, Object> expandContext) {
- AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
+ public T remove(ServletRequest request, Map<String, Object> expandContext) {
+ AttributeAccessor<T> aa = new AttributeAccessor<T>(name, expandContext, this.attributeName, this.fma, this.needsExpand);
return aa.remove(request);
}
@@ -150,8 +150,8 @@
* @param expandContext
* @return
*/
- public Object remove(HttpSession session, Map<String, Object> expandContext) {
- AttributeAccessor aa = new AttributeAccessor(name, expandContext, this.attributeName, this.fma, this.needsExpand);
+ public T remove(HttpSession session, Map<String, Object> expandContext) {
+ AttributeAccessor<T> aa = new AttributeAccessor<T>(name, expandContext, this.attributeName, this.fma, this.needsExpand);
return aa.remove(session);
}
@@ -189,10 +189,10 @@
return this.name;
}
- protected static class AttributeAccessor implements Serializable {
+ protected static class AttributeAccessor<T> implements Serializable {
protected Map<String, Object> expandContext;
protected String attributeName;
- protected FlexibleMapAccessor fma;
+ protected FlexibleMapAccessor<T> fma;
protected boolean isListReference;
protected boolean isAddAtIndex;
protected boolean isAddAtEnd;
@@ -200,7 +200,7 @@
protected int openBrace;
protected int closeBrace;
- public AttributeAccessor(String origName, Map<String, Object> expandContext, String defAttributeName, FlexibleMapAccessor defFma, boolean needsExpand) {
+ public AttributeAccessor(String origName, Map<String, Object> expandContext, String defAttributeName, FlexibleMapAccessor<T> defFma, boolean needsExpand) {
attributeName = defAttributeName;
fma = defFma;
@@ -209,7 +209,7 @@
int dotIndex = name.indexOf('.');
if (dotIndex != -1) {
attributeName = name.substring(0, dotIndex);
- fma = new FlexibleMapAccessor(name.substring(dotIndex+1));
+ fma = new FlexibleMapAccessor<T>(name.substring(dotIndex+1));
} else {
attributeName = name;
fma = null;
@@ -242,7 +242,7 @@
}
- public Object get(ServletRequest request) {
+ public T get(ServletRequest request) {
Object theValue = null;
if (isListReference) {
List lst = (List) request.getAttribute(attributeName);
@@ -252,13 +252,13 @@
}
if (fma != null) {
- return fma.get((Map) theValue);
+ return fma.get(UtilGenerics.<String, Object>checkMap(theValue));
} else {
- return theValue;
+ return UtilGenerics.<T>cast(theValue);
}
}
- public Object get(HttpSession session) {
+ public T get(HttpSession session) {
Object theValue = null;
if (isListReference) {
List lst = (List) session.getAttribute(attributeName);
@@ -268,13 +268,13 @@
}
if (fma != null) {
- return fma.get((Map) theValue);
+ return fma.get(UtilGenerics.<String, Object>checkMap(theValue));
} else {
- return theValue;
+ return UtilGenerics.<T>cast(theValue);
}
}
- protected <T> void putInList(List<T> lst, T value) {
+ protected void putInList(List<T> lst, T value) {
//if brackets are empty, append to list
if (isAddAtEnd) {
lst.add(value);
@@ -287,7 +287,7 @@
}
}
- public <T> void put(ServletRequest request, T value) {
+ public void put(ServletRequest request, T value) {
if (fma == null) {
if (isListReference) {
List<T> lst = UtilGenerics.checkList(request.getAttribute(attributeName));
@@ -306,7 +306,7 @@
}
}
- public <T> void put(HttpSession session, T value) {
+ public void put(HttpSession session, T value) {
if (fma == null) {
if (isListReference) {
List<T> lst = UtilGenerics.checkList(session.getAttribute(attributeName));
@@ -325,44 +325,44 @@
}
}
- public Object remove(ServletRequest request) {
+ public T remove(ServletRequest request) {
if (fma != null) {
Object theObj = request.getAttribute(attributeName);
if (isListReference) {
- List<?> lst = UtilGenerics.checkList(theObj);
+ List<Object> lst = UtilGenerics.checkList(theObj);
return fma.remove(UtilGenerics.checkMap(lst.get(listIndex), String.class, Object.class));
} else {
return fma.remove(UtilGenerics.checkMap(theObj, String.class, Object.class));
}
} else {
if (isListReference) {
- List<?> lst = UtilGenerics.checkList(request.getAttribute(attributeName));
- return lst.remove(listIndex);
+ List<Object> lst = UtilGenerics.checkList(request.getAttribute(attributeName));
+ return UtilGenerics.<T>cast(lst.remove(listIndex));
} else {
Object theValue = request.getAttribute(attributeName);
request.removeAttribute(attributeName);
- return theValue;
+ return UtilGenerics.<T>cast(theValue);
}
}
}
- public Object remove(HttpSession session) {
+ public T remove(HttpSession session) {
if (fma != null) {
Object theObj = session.getAttribute(attributeName);
if (isListReference) {
- List<?> lst = UtilGenerics.checkList(theObj);
+ List<Object> lst = UtilGenerics.checkList(theObj);
return fma.remove(UtilGenerics.checkMap(lst.get(listIndex), String.class, Object.class));
} else {
return fma.remove(UtilGenerics.checkMap(theObj, String.class, Object.class));
}
} else {
if (isListReference) {
- List<?> lst = UtilGenerics.checkList(session.getAttribute(attributeName));
- return lst.remove(listIndex);
+ List<Object> lst = UtilGenerics.checkList(session.getAttribute(attributeName));
+ return UtilGenerics.<T>cast(lst.remove(listIndex));
} else {
Object theValue = session.getAttribute(attributeName);
session.removeAttribute(attributeName);
- return theValue;
+ return UtilGenerics.<T>cast(theValue);
}
}
}
Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/MapComparator.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/MapComparator.java?rev=682096&r1=682095&r2=682096&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/MapComparator.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/collections/MapComparator.java Sat Aug 2 19:59:30 2008
@@ -68,13 +68,13 @@
Object o2 = null;
if (key instanceof FlexibleMapAccessor) {
- FlexibleMapAccessor fmaKey = (FlexibleMapAccessor) key;
+ FlexibleMapAccessor<Object> fmaKey = UtilGenerics.cast(key);
ascending = fmaKey.getIsAscending();
//Debug.logInfo("Doing compare with a FlexibleMapAccessor [" + fmaKey.getOriginalName() + "] ascending [" + ascending + "]", module);
- o1 = fmaKey.get((Map) map1);
- o2 = fmaKey.get((Map) map2);
+ o1 = fmaKey.get(UtilGenerics.<String, Object>checkMap(map1));
+ o2 = fmaKey.get(UtilGenerics.<String, Object>checkMap(map2));
} else {
if (key instanceof String) {
String keyStr = (String) key;
Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java?rev=682096&r1=682095&r2=682096&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/string/FlexibleStringExpander.java Sat Aug 2 19:59:30 2008
@@ -298,10 +298,10 @@
}
}
public static class VariableElement implements StringElement {
- protected FlexibleMapAccessor fma;
+ protected FlexibleMapAccessor<Object> fma;
public VariableElement(String valueName) {
- this.fma = new FlexibleMapAccessor(valueName);
+ this.fma = new FlexibleMapAccessor<Object>(valueName);
}
public void appendElement(StringBuilder buffer, Map<String, ? extends Object> context, Locale locale) {
@@ -384,7 +384,7 @@
envName = envName.substring(0, currencyPos);
}
- FlexibleMapAccessor fma = new FlexibleMapAccessor(envName);
+ FlexibleMapAccessor<Object> fma = new FlexibleMapAccessor<Object>(envName);
Object envVal = fma.get(context, locale);
if (envVal != null) {
if (localizeCurrency) {