Utils.java revision a2216e7e327b464ac03932b4add2983e9052bc79
1package afeclient.client; 2 3import com.google.gwt.json.client.JSONArray; 4import com.google.gwt.json.client.JSONObject; 5import com.google.gwt.json.client.JSONString; 6import com.google.gwt.json.client.JSONValue; 7 8import java.util.Collection; 9import java.util.Iterator; 10import java.util.Set; 11 12/** 13 * Utility methods. 14 */ 15public class Utils { 16 public static final String PLATFORM_SUFFIX = " (platform)"; 17 18 public static final ClassFactory factory = new SiteClassFactory(); 19 20 /** 21 * Converts a collection of Java <code>String</code>s into a <code>JSONArray 22 * </code> of <code>JSONString</code>s. 23 */ 24 public static JSONArray stringsToJSON(Collection strings) { 25 JSONArray result = new JSONArray(); 26 for(Iterator i = strings.iterator(); i.hasNext(); ) { 27 String s = (String) i.next(); 28 result.set(result.size(), new JSONString(s)); 29 } 30 return result; 31 } 32 33 /** 34 * Converts a <code>JSONArray</code> of <code>JSONStrings</code> to an 35 * array of Java <code>Strings</code>. 36 */ 37 public static String[] JSONtoStrings(JSONArray strings) { 38 String[] result = new String[strings.size()]; 39 for (int i = 0; i < strings.size(); i++) { 40 result[i] = strings.get(i).isString().stringValue(); 41 } 42 return result; 43 } 44 45 /** 46 * Converts a <code>JSONArray</code> of <code>JSONObjects</code> to an 47 * array of Java <code>Strings</code> by grabbing the specified field from 48 * each object. 49 */ 50 public static String[] JSONObjectsToStrings(JSONArray objects, String field) { 51 String[] result = new String[objects.size()]; 52 for (int i = 0; i < objects.size(); i++) { 53 JSONValue fieldValue = objects.get(i).isObject().get(field); 54 result[i] = fieldValue.isString().stringValue(); 55 } 56 return result; 57 } 58 59 /** 60 * Get a value out of an array of size 1. 61 * @return array[0] 62 * @throws IllegalArgumentException if the array is not of size 1 63 */ 64 public static JSONValue getSingleValueFromArray(JSONArray array) { 65 if(array.size() != 1) { 66 throw new IllegalArgumentException("Array is not of size 1"); 67 } 68 return array.get(0); 69 } 70 71 public static String formatStatusCounts(JSONObject counts, String joinWith) { 72 String result = ""; 73 Set statusSet = counts.keySet(); 74 for (Iterator i = statusSet.iterator(); i.hasNext();) { 75 String status = (String) i.next(); 76 int count = (int) counts.get(status).isNumber().getValue(); 77 result += Integer.toString(count) + " " + status; 78 if (i.hasNext()) 79 result += joinWith; 80 } 81 return result; 82 } 83 84 public static JSONObject copyJSONObject(JSONObject source) { 85 JSONObject dest = new JSONObject(); 86 for(Iterator i = source.keySet().iterator(); i.hasNext(); ) { 87 String key = (String) i.next(); 88 dest.put(key, source.get(key)); 89 } 90 return dest; 91 } 92 93 public static String[] getLabelStrings() { 94 StaticDataRepository staticData = StaticDataRepository.getRepository(); 95 JSONArray labels = staticData.getData("labels").isArray(); 96 String[] result = new String[labels.size()]; 97 for (int i = 0; i < labels.size(); i++) { 98 JSONObject label = labels.get(i).isObject(); 99 String name = label.get("name").isString().stringValue(); 100 boolean platform = label.get("platform").isNumber().getValue() != 0; 101 if (platform) { 102 name += PLATFORM_SUFFIX; 103 } 104 result[i] = name; 105 } 106 return result; 107 } 108 109 public static String decodeLabelName(String labelName) { 110 if (labelName.endsWith(PLATFORM_SUFFIX)) { 111 int nameLength = labelName.length() - PLATFORM_SUFFIX.length(); 112 return labelName.substring(0, nameLength); 113 } 114 return labelName; 115 } 116 117 public static JSONString getLockedText(JSONObject host) { 118 boolean locked = host.get("locked").isNumber().getValue() > 0; 119 return new JSONString(locked ? "Yes" : "No"); 120 } 121} 122