PageRenderTime 80ms CodeModel.GetById 64ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/testing/library/src/com/google/appengine/library/Util.java

http://datanucleus-appengine.googlecode.com/
Java | 55 lines | 40 code | 11 blank | 4 comment | 5 complexity | 306df53316f0fb3240608b0546c4afb5 MD5 | raw file
 1package com.google.appengine.library;
 2
 3import java.util.Enumeration;
 4import java.util.HashMap;
 5import java.util.Map;
 6
 7import javax.servlet.http.HttpServletRequest;
 8
 9final class Util {
10
11  /**
12   * Convenience class that returns "" for {@code null} value. This saves the
13   * check against {@code null} at caller.
14   */
15  static final class NullToEmptyMapWrapper {
16    final private Map<String, String> map;
17  
18    public NullToEmptyMapWrapper(Map<String, String> map) {
19      this.map = map;
20    }
21  
22    public String get(String k) {
23      String v = map.get(k);
24      return v == null ? "" : v;
25    }
26
27    public String put(String k, String v) {
28      return map.put(k, v);
29    }
30}
31
32  static final String nullSafeToString(Object obj) {
33    return obj != null ? obj.toString() : "";
34  }
35
36  @SuppressWarnings("unchecked")
37  static NullToEmptyMapWrapper wrapFormFields(HttpServletRequest req) {
38    Map<String, String> formFieldsMap = new HashMap<String, String>();
39    for (Enumeration<String> e = req.getParameterNames(); e.hasMoreElements();) {
40      String fieldName = e.nextElement();
41      String[] fieldValues = req.getParameterValues(fieldName);
42      String oneValue = null; // holds the parameter as a concatenated string.
43      if (fieldValues.length > 0) {
44        oneValue = fieldValues[0];
45      }
46      for (int i = 1; i < fieldValues.length; i++) {
47        oneValue += " " + fieldValues[i];
48      }
49      
50      formFieldsMap.put(fieldName, oneValue);
51    }
52    return new NullToEmptyMapWrapper(formFieldsMap);
53  }
54
55}