TableModel.class

/*     */ package com.itiptop.kernel.portal.model;
/*     */
/*     */ import com.itiptop.common.utils.ArrayUtils;
/*     */ import com.itiptop.common.utils.DateUtils;
/*     */ import com.itiptop.common.utils.NumberUtils;
/*     */ import com.itiptop.common.utils.StringUtils;
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Calendar;
/*     */ import java.util.Collections;
/*     */ import java.util.Comparator;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */
/*     */ public class TableModel
/*     */   implements Serializable
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   private int colOrder;
/*     */   private String objectType;
/*     */   private Map<String, ColMetaInfo> colMetaInfos;
/*     */   private List<Map<String, Object>> data;
/*     */
/*     */   public TableModel()
/*     */   {
/*  27 */     this.colOrder = 0;
/*     */
/*  31 */     this.colMetaInfos = new HashMap(10);
/*     */
/*  33 */     this.data = new ArrayList(30); }
/*     */
/*     */   public Map<String, ColMetaInfo> getColMetaInfos() {
/*  36 */     return this.colMetaInfos;
/*     */   }
/*     */
/*     */   public boolean isScriptOrder(String col) {
/*  40 */     ColMetaInfo m = (ColMetaInfo)this.colMetaInfos.get(col);
/*  41 */     return ((m == null) ? false : m.isScriptCol());
/*     */   }
/*     */
/*     */   public void sort(String col, boolean asc)
/*     */   {
/*  67 */     Comparator c = new RowComparetor(col, asc);
/*  68 */     Collections.sort(this.data, c);
/*     */   }
/*     */
/*     */   public boolean isEmpty() {
/*  72 */     return this.data.isEmpty();
/*     */   }
/*     */
/*     */   public List<Map<String, Object>> getData() {
/*  76 */     return this.data;
/*     */   }
/*     */
/*     */   public int getExportCols() {
/*  80 */     int i = 0;
/*  81 */     for (String s : this.colMetaInfos.keySet()) {
/*  82 */       ColMetaInfo col = (ColMetaInfo)this.colMetaInfos.get(s);
/*  83 */       if (col.exportable) {
/*  84 */         ++i;
/*     */       }
/*     */     }
/*  87 */     return i;
/*     */   }
/*     */
/*     */   public int[] getHeaderWidths() {
/*  91 */     List metas = new ArrayList(getColMetaInfos().values());
/*  92 */     Collections.sort(metas);
/*     */
/*  94 */     List a = new ArrayList();
/*  95 */     for (ColMetaInfo col : metas) {
/*  96 */       if (col.exportable) {
/*  97 */         a.add(Integer.valueOf(col.getRelativeColWidth()));
/*     */       }
/*     */     }
/* 100 */     return ArrayUtils.toPrimitive((Integer[])(Integer[])a.toArray(new Integer[0]));
/*     */   }
/*     */
/*     */   public void addColData(String col, Object[] data) {
/* 104 */     addColMetaInfo(this.colOrder++, col, null, null, null);
/* 105 */     if (this.data.isEmpty())
/* 106 */       for (Object o : data) {
/* 107 */         Map record = new HashMap();
/* 108 */         if (o instanceof Double) {
/* 109 */           Double d = (Double)o;
/* 110 */           record.put(col, Double.valueOf(NumberUtils.round(d.doubleValue(), 6)));
/*     */         } else {
/* 112 */           record.put(col, o);
/*     */         }
/* 114 */         addDataRecord(record);
/*     */       }
/*     */     else
/* 117 */       for (int i = 0; i < data.length; ++i)
/* 118 */         if (i >= this.data.size()) {
/* 119 */           for (int j = i; j < data.length; ++j) {
/* 120 */             Map record = new HashMap();
/* 121 */             if (data[j] instanceof Double) {
/* 122 */               Double d = (Double)data[j];
/* 123 */               record.put(col, Double.valueOf(NumberUtils.round(d.doubleValue(), 6)));
/*     */             } else {
/* 125 */               record.put(col, data[j]);
/*     */             }
/* 127 */             addDataRecord(record);
/*     */           }
/*     */         } else {
/* 130 */           Map record = (Map)this.data.get(i);
/* 131 */           if (data[i] instanceof Double) {
/* 132 */             Double d = (Double)data[i];
/* 133 */             record.put(col, Double.valueOf(NumberUtils.round(d.doubleValue(), 6)));
/*     */           } else {
/* 135 */             record.put(col, data[i]);
/*     */           }
/*     */         }
/*     */   }
/*     */
/*     */   public TableModel toPieData(Date b, String[] labels, Object[] data)
/*     */   {
/* 143 */     TableModel ret = new TableModel();
/* 144 */     ret.addColMetaInfo(1, "timestamp", "时间戳", "string", "20%");
/* 145 */     int i = 2;
/* 146 */     Map total = new HashMap();
/* 147 */     for (String col : labels) {
/* 148 */       ret.addColMetaInfo(i++, col, col, "string", "20%");
/* 149 */       total.put(col, data[(i - 3)]);
/*     */     }
/* 151 */     total.put("timestamp", DateUtils.format(b));
/* 152 */     return ret;
/*     */   }
/*     */
/*     */   public TableModel toListData(Date begin, Date end, int interval, boolean hasTotal) {
/* 156 */     return toListData(begin, end, interval, hasTotal, 600);
/*     */   }
/*     */
/*     */   public TableModel toListData(Date begin, Date end, int interval, boolean hasTotal, int width) {
/* 160 */     TableModel ret = new TableModel();
/* 161 */     ret.addColMetaInfo(1, "timestamp", "时间戳", "string", "120");
/* 162 */     int i = 2;
/* 163 */     int cols = this.colMetaInfos.size();
/*     */
/* 165 */     int colWidth = width;
/*     */
/* 167 */     if (cols > 1) {
/* 168 */       colWidth = (width - 60) / (cols - 1);
/*     */     }
/*     */
/* 171 */     for (String col : this.colMetaInfos.keySet()) {
/* 172 */       if ("label".equalsIgnoreCase(col))
/*     */         continue;
/* 174 */       ret.addColMetaInfo(i++, col, col, "string", colWidth + "");
/*     */     }
/*     */
/* 177 */     ret.data = new ArrayList(this.data);
/* 178 */     long b = DateUtils.getSeconds(begin);
/*     */
/* 180 */     if (interval == -1) {
/* 181 */       Calendar cal = Calendar.getInstance();
/* 182 */       for (int j = 0; j < ret.data.size(); ++j) {
/* 183 */         Map r = (Map)ret.data.get(j);
/* 184 */         r.put("timestamp", DateUtils.formatMonth(new Date(b)));
/* 185 */         cal.setTime(new Date(b));
/* 186 */         cal.add(2, 1);
/* 187 */         b = DateUtils.getSeconds(cal.getTime());
/*     */       }
/*     */     } else {
/* 190 */       for (int j = 0; j < ret.data.size(); ++j) {
/* 191 */         Map r = (Map)ret.data.get(j);
/* 192 */         r.put("timestamp", DateUtils.formatTime(new Date(b)));
/* 193 */         b += interval * 1000;
/*     */       }
/*     */     }
/*     */
/* 197 */     if (hasTotal)
/*     */     {
/*     */       String col;
/* 198 */       Map r = new HashMap();
/* 199 */       r.put("timestamp", "最大值");
/* 200 */       for (Iterator i$ = this.colMetaInfos.keySet().iterator(); i$.hasNext(); ) { col = (String)i$.next();
/* 201 */         if ("label".equalsIgnoreCase(col))
/*     */           continue;
/* 203 */         r.put(col, Double.valueOf(NumberUtils.round(getMaxColVal(col), 3)));
/*     */       }
/* 205 */       ret.addDataRecord(r);
/*     */
/* 207 */       r = new HashMap();
/* 208 */       r.put("timestamp", "最小值");
/* 209 */       for (i$ = this.colMetaInfos.keySet().iterator(); i$.hasNext(); ) { col = (String)i$.next();
/* 210 */         if ("label".equalsIgnoreCase(col))
/*     */           continue;
/* 212 */         r.put(col, Double.valueOf(NumberUtils.round(getMinColVal(col), 3)));
/*     */       }
/*     */
/* 215 */       ret.addDataRecord(r);
/*     */
/* 217 */       r = new HashMap();
/* 218 */       r.put("timestamp", "平均值");
/* 219 */       for (i$ = this.colMetaInfos.keySet().iterator(); i$.hasNext(); ) { col = (String)i$.next();
/* 220 */         if ("label".equalsIgnoreCase(col))
/*     */           continue;
/* 222 */         r.put(col, Double.valueOf(NumberUtils.round(getAvColVal(col), 3)));
/*     */       }
/* 224 */       ret.addDataRecord(r);
/*     */
/* 226 */       r = new HashMap();
/* 227 */       r.put("timestamp", "合计");
/* 228 */       for (i$ = this.colMetaInfos.keySet().iterator(); i$.hasNext(); ) { col = (String)i$.next();
/* 229 */         if ("label".equalsIgnoreCase(col))
/*     */           continue;
/* 231 */         r.put(col, Double.valueOf(NumberUtils.round(getColTotal(col), 3)));
/*     */       }
/* 233 */       ret.addDataRecord(r);
/*     */     }
/*     */
/* 236 */     return ret;
/*     */   }
/*     */
/*     */   public TableModel toListData(Date begin, Date end, int interval) {
/* 240 */     return toListData(begin, end, interval, true, 600);
/*     */   }
/*     */
/*     */   public TableModel toListData(Date begin, Date end, int interval, int width) {
/* 244 */     return toListData(begin, end, interval, true, width);
/*     */   }
/*     */
/*     */   public double getColTotal(String col) {
/* 248 */     double ret = 0.0D;
/* 249 */     for (int j = 0; j < this.data.size(); ++j) {
/* 250 */       Map r = (Map)this.data.get(j);
/* 251 */       Object d = r.get(col);
/* 252 */       if (d != null)
/* 253 */         ret += Double.parseDouble(String.valueOf(d));
/*     */     }
/* 255 */     return ret;
/*     */   }
/*     */
/*     */   public double getMaxColVal(String col) {
/* 259 */     double ret = 0.0D;
/* 260 */     for (int j = 0; j < this.data.size(); ++j) {
/* 261 */       Map r = (Map)this.data.get(j);
/* 262 */       Object d = r.get(col);
/* 263 */       if ((d != null) && (Double.parseDouble(String.valueOf(d)) > ret)) {
/* 264 */         ret = Double.parseDouble(String.valueOf(d));
/*     */       }
/*     */     }
/* 267 */     return ret;
/*     */   }
/*     */
/*     */   public double getMinColVal(String col) {
/* 271 */     if (this.data.isEmpty())
/* 272 */       return 0.0D;
/* 273 */     Map r = (Map)this.data.get(0);
/* 274 */     double ret = Double.parseDouble(String.valueOf(r.get(col)));
/*     */
/* 276 */     for (int j = 1; j < this.data.size(); ++j) {
/* 277 */       r = (Map)this.data.get(j);
/* 278 */       Object d = r.get(col);
/* 279 */       if ((d != null) && (Double.parseDouble(String.valueOf(d)) < ret)) {
/* 280 */         ret = Double.parseDouble(String.valueOf(d));
/*     */       }
/*     */     }
/*     */
/* 284 */     return ret;
/*     */   }
/*     */
/*     */   public double getAvColVal(String col) {
/* 288 */     return (getColTotal(col) / this.data.size());
/*     */   }
/*     */
/*     */   public void addColMetaInfo(String name, ColMetaInfo meta) {
/* 292 */     this.colMetaInfos.put(name, meta);
/*     */   }
/*     */
/*     */   public void addColMetaInfo(String name, String displayName, String colType, String colWidth) {
/* 296 */     ColMetaInfo meta = new ColMetaInfo(name, displayName, colType, colWidth);
/* 297 */     addColMetaInfo(name, meta);
/*     */   }
/*     */
/*     */   public void addColMetaInfo(int order, String name, String displayName, String colType, String colWidth) {
/* 301 */     ColMetaInfo meta = new ColMetaInfo(name, displayName, colType, colWidth);
/* 302 */     meta.setOrder(order);
/* 303 */     addColMetaInfo(name, meta);
/*     */   }
/*     */
/*     */   public void addColMetaInfo(int order, String name, String displayName, String colType, String colWidth, boolean export)
/*     */   {
/* 308 */     ColMetaInfo meta = new ColMetaInfo(name, displayName, colType, colWidth);
/* 309 */     meta.setOrder(order);
/* 310 */     meta.setExportable(export);
/* 311 */     addColMetaInfo(name, meta);
/*     */   }
/*     */
/*     */   public void addColMetaInfo(int order, String name, String displayName, String colType, String colWidth, boolean export, boolean scriptCol)
/*     */   {
/* 316 */     ColMetaInfo meta = new ColMetaInfo(name, displayName, colType, colWidth, scriptCol);
/* 317 */     meta.setOrder(order);
/* 318 */     meta.setExportable(export);
/* 319 */     addColMetaInfo(name, meta);
/*     */   }
/*     */
/*     */   public void addDataRecord(Map<String, Object> record) {
/* 323 */     this.data.add(record);
/*     */   }
/*     */
/*     */   public void setData(List<Map<String, Object>> data) {
/* 327 */     this.data = data;
/*     */   }
/*     */
/*     */   public String getObjectType() {
/* 331 */     return this.objectType;
/*     */   }
/*     */
/*     */   public void setObjectType(String objectType) {
/* 335 */     this.objectType = objectType;
/*     */   }
/*     */
/*     */   public static TableModel instance() {
/* 339 */     return new TableModel();
/*     */   }
/*     */
/*     */   public List<String> getCols() {
/* 343 */     List cols = new ArrayList(this.colMetaInfos.values());
/* 344 */     Collections.sort(cols);
/* 345 */     List ret = new ArrayList();
/* 346 */     for (ColMetaInfo col : cols) {
/* 347 */       ret.add(col.getName());
/*     */     }
/* 349 */     return ret;
/*     */   }
/*     */
/*     */   public String[] getLabels()
/*     */   {
/*     */     int i;
/* 353 */     String[] colData = new String[this.data.size()];
/*     */     try {
/* 355 */       i = 0;
/* 356 */       for (Map data : this.data) {
/* 357 */         String key = data.get("label") + "";
/* 358 */         colData[(i++)] = key;
/*     */       }
/*     */     } catch (Exception e) {
/*     */     }
/* 362 */     return colData;
/*     */   }
/*     */
/*     */   public Double[] getColData(String col)
/*     */   {
/*     */     int i;
/* 366 */     Double[] colData = new Double[this.data.size()];
/* 367 */     int n = 0;
/*     */     try {
/* 369 */       i = 0;
/* 370 */       for (Map data : this.data) {
/* 371 */         if (!(data.containsKey(col))) {
/* 372 */           ++n;
/*     */         }
/* 374 */         Object b = data.get(col);
/*     */
/* 377 */         colData[(i++)] = Double.valueOf((b == null) ? 0.0D : ((Double)b).doubleValue());
/*     */       }
/*     */     } catch (Exception e) {
/*     */     }
/* 381 */     Double[] ret = new Double[this.data.size() - n];
/* 382 */     System.arraycopy(colData, 0, ret, 0, ret.length);
/* 383 */     return ret;
/*     */   }
/*     */
/*     */   public static class ColMetaInfo
/*     */     implements Comparable<ColMetaInfo>, Serializable
/*     */   {
/*     */     private static final long serialVersionUID = 1L;
/*     */     private String name;
/*     */     private String displayName;
/*     */     private String colType;
/*     */     private String colWidth;
/*     */     private int order;
/* 394 */     private boolean exportable = true;
/* 395 */     private boolean scriptCol = false;
/* 396 */     private boolean sortable = true;
/*     */
/*     */     public ColMetaInfo(String name, String displayName, String colType, String colWidth, boolean scriptCol) {
/* 399 */       this.name = name;
/* 400 */       this.displayName = displayName;
/* 401 */       this.colType = colType;
/* 402 */       this.colWidth = colWidth;
/* 403 */       this.scriptCol = scriptCol;
/*     */     }
/*     */
/*     */     public ColMetaInfo(String name, String displayName, String colType, String colWidth) {
/* 407 */       this.name = name;
/* 408 */       this.displayName = displayName;
/* 409 */       this.colType = colType;
/* 410 */       this.colWidth = colWidth;
/*     */     }
/*     */
/*     */     public int getRelativeColWidth() {
/* 414 */       if ((StringUtils.isNotEmpty(this.colWidth)) && (this.colWidth.indexOf("%") != -1)) {
/* 415 */         String p = this.colWidth.substring(0, this.colWidth.indexOf("%"));
/* 416 */         return Integer.parseInt(p);
/*     */       }
/* 418 */       return 20;
/*     */     }
/*     */
/*     */     public boolean isSortable()
/*     */     {
/* 423 */       return this.sortable;
/*     */     }
/*     */
/*     */     public void setSortable(boolean sortable) {
/* 427 */       this.sortable = sortable;
/*     */     }
/*     */
/*     */     public void setExportable(boolean exportable) {
/* 431 */       this.exportable = exportable;
/*     */     }
/*     */
/*     */     public boolean isScriptCol() {
/* 435 */       return this.scriptCol;
/*     */     }
/*     */
/*     */     public boolean isExportable() {
/* 439 */       return this.exportable;
/*     */     }
/*     */
/*     */     public String getName() {
/* 443 */       return this.name;
/*     */     }
/*     */
/*     */     public String getDisplayName() {
/* 447 */       return this.displayName;
/*     */     }
/*     */
/*     */     public String getColType() {
/* 451 */       return this.colType;
/*     */     }
/*     */
/*     */     public String getColWidth() {
/* 455 */       return this.colWidth;
/*     */     }
/*     */
/*     */     public void setName(String name) {
/* 459 */       this.name = name;
/*     */     }
/*     */
/*     */     public void setDisplayName(String displayName) {
/* 463 */       this.displayName = displayName;
/*     */     }
/*     */
/*     */     public void setColType(String colType) {
/* 467 */       this.colType = colType;
/*     */     }
/*     */
/*     */     public void setColWidth(String colWidth) {
/* 471 */       this.colWidth = colWidth;
/*     */     }
/*     */
/*     */     public void setOrder(int order)
/*     */     {
/* 478 */       this.order = order;
/*     */     }
/*     */
/*     */     public int getOrder() {
/* 482 */       return this.order;
/*     */     }
/*     */
/*     */     public int compareTo(ColMetaInfo o)
/*     */     {
/* 490 */       int other = o.order;
/* 491 */       return (this.order - other);
/*     */     }
/*     */   }
/*     */
/*     */   static class RowComparetor
/*     */     implements Comparator<Map<String, Object>>
/*     */   {
/*     */     String col;
/*     */     boolean asc;
/*     */
/*     */     public RowComparetor(String col, boolean asc)
/*     */     {
/*  48 */       this.col = col;
/*  49 */       this.asc = asc;
/*     */     }
/*     */
/*     */     public int compare(Map<String, Object> o1, Map<String, Object> o2)
/*     */     {
/*  55 */       Comparable a = (Comparable)o1.get(this.col);
/*  56 */       Comparable b = (Comparable)o2.get(this.col);
/*     */
/*  58 */       if (this.asc) {
/*  59 */         return a.compareTo(b);
/*     */       }
/*  61 */       return (-a.compareTo(b));
/*     */     }
/*     */   }
/*     */ }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值