|
| 1 | +package net.servicestack.client; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.reflect.TypeToken; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.net.URL; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +public final class Inspect { |
| 15 | + |
| 16 | + public static final void vars(Map args) { |
| 17 | + String inspectVarsPath = System.getenv("INSPECT_VARS"); |
| 18 | + if (inspectVarsPath == null) |
| 19 | + return; |
| 20 | + |
| 21 | + FileWriter writer = null; |
| 22 | + |
| 23 | + try { |
| 24 | + if (System.getProperty("os.name").toLowerCase().contains("win")) { |
| 25 | + inspectVarsPath = inspectVarsPath.replace("/", "\\"); |
| 26 | + } else { |
| 27 | + inspectVarsPath = inspectVarsPath.replace("\\", "/"); |
| 28 | + } |
| 29 | + |
| 30 | + Path dirPath = Paths.get(inspectVarsPath).getParent(); |
| 31 | + Files.createDirectories(dirPath); |
| 32 | + |
| 33 | + writer = new FileWriter(inspectVarsPath); |
| 34 | + |
| 35 | + new Gson().toJson(args, writer); |
| 36 | + |
| 37 | + } catch (IOException e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } finally { |
| 40 | + if (writer != null) { |
| 41 | + try { |
| 42 | + writer.close(); |
| 43 | + } catch (Exception e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static final String dump(Object obj) { |
| 51 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 52 | + String json = gson.toJson(obj); |
| 53 | + return json.replace("\"", ""); |
| 54 | + } |
| 55 | + |
| 56 | + public static final void printDump(Object obj) { |
| 57 | + System.out.println(dump(obj)); |
| 58 | + } |
| 59 | + |
| 60 | + public static final String dumpTable(Iterable objs, Iterable headers) { |
| 61 | + List rows = Utils.toList(objs); |
| 62 | + List<Map<String, Object>> mapRows = toListMap(rows); |
| 63 | + |
| 64 | + List<String> keys = headers != null ? Utils.toList(headers) : allKeys(mapRows); |
| 65 | + HashMap<String, Integer> colSizes = new HashMap<>(); |
| 66 | + |
| 67 | + for (String k : keys) { |
| 68 | + int max = k.length(); |
| 69 | + for (Map<String, Object> row : mapRows) { |
| 70 | + if (row.containsKey(k)) { |
| 71 | + Object col = row.get(k); |
| 72 | + int valSize = col.toString().length(); |
| 73 | + if (valSize > max) { |
| 74 | + max = valSize; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + colSizes.put(k, max); |
| 79 | + } |
| 80 | + |
| 81 | + // sum + ' padding ' + | |
| 82 | + int rowWidth = Utils.sum(colSizes.values()) + |
| 83 | + (colSizes.size() * 2) + |
| 84 | + (colSizes.size() + 1); |
| 85 | + |
| 86 | + StringBuilder sb = new StringBuilder(); |
| 87 | + Utils.appendLine(sb.append("+").append(Utils.repeat("-", rowWidth - 2)).append("+")); |
| 88 | + sb.append("|"); |
| 89 | + for (String k : keys) { |
| 90 | + sb.append(alignCenter(k, colSizes.get(k))).append("|"); |
| 91 | + } |
| 92 | + Utils.appendLine(sb); |
| 93 | + |
| 94 | + Utils.appendLine(sb.append("|").append(Utils.repeat("-", rowWidth - 2)).append("|")); |
| 95 | + |
| 96 | + for (Map<String, Object> row : mapRows) { |
| 97 | + sb.append("|"); |
| 98 | + for (String k : keys) { |
| 99 | + sb.append(alignAuto(row.get(k), colSizes.get(k))).append("|"); |
| 100 | + } |
| 101 | + Utils.appendLine(sb); |
| 102 | + } |
| 103 | + Utils.appendLine(sb.append("+").append(Utils.repeat("-", rowWidth - 2)).append("+")); |
| 104 | + |
| 105 | + return sb.toString(); |
| 106 | + } |
| 107 | + |
| 108 | + public static final void printDumpTable(Iterable objs) { |
| 109 | + System.out.println(dumpTable(objs, null)); |
| 110 | + } |
| 111 | + |
| 112 | + public static final void printDumpTable(Iterable objs, Iterable headers) { |
| 113 | + System.out.println(dumpTable(objs, headers)); |
| 114 | + } |
| 115 | + |
| 116 | + public static final List<String> allKeys(List<Map<String, Object>> rows) { |
| 117 | + ArrayList<String> to = new ArrayList<>(); |
| 118 | + for (Map<String, Object> row : rows) { |
| 119 | + for (String key : row.keySet()) { |
| 120 | + if (!to.contains(key)) { |
| 121 | + to.add(key); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return to; |
| 126 | + } |
| 127 | + |
| 128 | + public static final boolean isNumber(Object obj) { |
| 129 | + return obj instanceof Long || obj instanceof Integer || obj instanceof Short || |
| 130 | + obj instanceof Byte || obj instanceof Double || obj instanceof Float; |
| 131 | + } |
| 132 | + |
| 133 | + private static final String fmtNumber(Object d) { |
| 134 | + if (d instanceof Double) { |
| 135 | + Double dbl = ((Number) d).doubleValue(); |
| 136 | + return dbl == Math.floor(dbl) ? String.valueOf(((Number) dbl).longValue()) : String.valueOf(dbl); |
| 137 | + } else if (d instanceof Float) { |
| 138 | + Float f = ((Number) d).floatValue(); |
| 139 | + return f == Math.floor(f) ? String.valueOf(((Number) f).longValue()) : String.valueOf(f); |
| 140 | + } |
| 141 | + return d.toString(); |
| 142 | + } |
| 143 | + |
| 144 | + public static final String alignLeft(String str, Integer len) { |
| 145 | + return alignLeft(str, len, " "); |
| 146 | + } |
| 147 | + |
| 148 | + public static final String alignLeft(String str, int len, String pad) { |
| 149 | + if (len < 0) return ""; |
| 150 | + int aLen = len + 1 - str.length(); |
| 151 | + return aLen <= 0 ? str : pad + str + Utils.repeat(pad, aLen); |
| 152 | + } |
| 153 | + |
| 154 | + public static final String alignCenter(String str, int len) { |
| 155 | + return alignCenter(str, len, " "); |
| 156 | + } |
| 157 | + |
| 158 | + public static final String alignCenter(String str, int len, String pad) { |
| 159 | + if (len < 0) return ""; |
| 160 | + int nLen = str.length(); |
| 161 | + int half = (int) Math.floor(len / 2.0 - nLen / 2.0); |
| 162 | + int odds = Math.abs((nLen % 2) - (len % 2)); |
| 163 | + return (Utils.repeat(pad, half + 1)) + str + (Utils.repeat(pad, half + 1 + odds)); |
| 164 | + } |
| 165 | + |
| 166 | + public static final String alignRight(String str, int len) { |
| 167 | + return alignRight(str, len, " "); |
| 168 | + } |
| 169 | + |
| 170 | + public static final String alignRight(String str, int len, String pad) { |
| 171 | + if (len < 0) return ""; |
| 172 | + int aLen = len + 1 - str.length(); |
| 173 | + return aLen <= 0 ? str : Utils.repeat(pad, aLen) + str + pad; |
| 174 | + } |
| 175 | + |
| 176 | + public static final String alignAuto(Object obj, int len) { |
| 177 | + return alignAuto(obj, len, " "); |
| 178 | + } |
| 179 | + |
| 180 | + public static final String alignAuto(Object obj, int len, String pad) { |
| 181 | + String str = obj == null ? "" : String.valueOf(obj); |
| 182 | + if (str.length() <= len) { |
| 183 | + return isNumber(obj) |
| 184 | + ? alignRight(fmtNumber(obj), len, pad) |
| 185 | + : alignLeft(str, len, pad); |
| 186 | + } else { |
| 187 | + return str; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public static final List<Map<String, Object>> toListMap(List objs) { |
| 192 | + Gson gson = new Gson(); |
| 193 | + String json = gson.toJson(objs); |
| 194 | + return gson.fromJson(json, new TypeToken<List<Map<String, Object>>>() { |
| 195 | + }.getType()); |
| 196 | + } |
| 197 | + |
| 198 | + public static final Map<String, Object> toMap(Object obj) { |
| 199 | + Gson gson = new Gson(); |
| 200 | + String json = gson.toJson(obj); |
| 201 | + return gson.fromJson(json, new TypeToken<Map<String, Object>>() { |
| 202 | + }.getType()); |
| 203 | + } |
| 204 | + |
| 205 | + public static final String readUrlAsText(URL url) { |
| 206 | + StringBuilder sb = new StringBuilder(); |
| 207 | + BufferedReader reader = null; |
| 208 | + try { |
| 209 | + reader = new BufferedReader(new InputStreamReader(url.openStream())); |
| 210 | + String line = null; |
| 211 | + while ((line = reader.readLine()) != null) { |
| 212 | + sb.append(line); |
| 213 | + } |
| 214 | + } catch (Exception e) { |
| 215 | + e.printStackTrace(); |
| 216 | + } finally { |
| 217 | + if (reader != null) { |
| 218 | + try { |
| 219 | + reader.close(); |
| 220 | + } catch (Exception e) { |
| 221 | + e.printStackTrace(); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + return sb.toString(); |
| 226 | + } |
| 227 | +} |
0 commit comments