Rows.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.cnd3b.common.data;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.cnd3b.utility.Math;
  5. import org.apache.poi.ss.formula.functions.T;
  6. import org.dom4j.Document;
  7. import org.dom4j.DocumentHelper;
  8. import org.dom4j.Element;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. public class Rows extends ArrayList<Row> {
  13. private static final long serialVersionUID = 1L;
  14. public int totalRows = 0;
  15. public int totalPage = 0;
  16. public String errmsg = "";
  17. public String getErrMsg() {
  18. return errmsg;
  19. }
  20. public int getTotalRows() {
  21. return totalRows;
  22. }
  23. public int getTotalPage() {
  24. return totalPage;
  25. }
  26. public Row getRow(int i) {
  27. if (i + 1 > this.size()) {
  28. return null;
  29. }
  30. return get(i);
  31. }
  32. public Row getLastRow() {
  33. return get(this.size() - 1);
  34. }
  35. /**
  36. * 获取字段合计值
  37. *
  38. * @param column
  39. * @return
  40. */
  41. public double sum(String column) {
  42. double value = 0d;
  43. for (Row row : this) {
  44. if (row.containsKey(column)) {
  45. value = Math.add(value, row.getDouble(column));
  46. }
  47. }
  48. return value;
  49. }
  50. private HashMap<String, Double> minValueMap = new HashMap<>();
  51. private HashMap<String, Double> maxValueMap = new HashMap<>();
  52. /**
  53. * 获取字段最小值
  54. *
  55. * @param column
  56. * @return
  57. */
  58. public double min(String column) {
  59. if (minValueMap.containsKey(column)) {
  60. return minValueMap.get(column);
  61. }
  62. caculate(column);
  63. return minValueMap.get(column);
  64. }
  65. /**
  66. * 获取字段最大值
  67. *
  68. * @param column
  69. * @return
  70. */
  71. public double max(String column) {
  72. if (maxValueMap.containsKey(column)) {
  73. return maxValueMap.get(column);
  74. }
  75. caculate(column);
  76. return maxValueMap.get(column);
  77. }
  78. private void caculate(String column) {
  79. double minvalue = 0d;
  80. double maxvalue = 0d;
  81. for (Row row : this) {
  82. if (row.containsKey(column)) {
  83. double value = row.getDouble(column);
  84. minvalue = java.lang.Math.min(minvalue, value);
  85. maxvalue = java.lang.Math.max(maxvalue, value);
  86. }
  87. }
  88. minValueMap.put(column, minvalue);
  89. maxValueMap.put(column, maxvalue);
  90. }
  91. public Rows removeColumn(String[] columns) {
  92. for (Row row : this) {
  93. for (String column : columns) {
  94. row.remove(column);
  95. }
  96. }
  97. return this;
  98. }
  99. public JSONArray toJsonArray() {
  100. // JSONArray array = new JSONArray();
  101. // for (Row row : this) {
  102. // JSONObject object = row.toJsonObject();
  103. // array.add(object);
  104. // }
  105. // return array;
  106. return (JSONArray) JSON.toJSON(this);
  107. }
  108. public JSONArray toJsonArray(String column) {
  109. JSONArray array = new JSONArray();
  110. for (Row row : this) {
  111. Object value = row.get(column);
  112. array.add(value);
  113. }
  114. return array;
  115. }
  116. public String[] toArray(String column) {
  117. return toArrayList(column).toArray(new String[0]);
  118. }
  119. public ArrayList<String> toArrayList(String column) {
  120. ArrayList<String> list = new ArrayList<>();
  121. for (Row row : this) {
  122. list.add(row.getString(column));
  123. }
  124. return list;
  125. }
  126. public RowsMap toRowsMap(String fieldname) {
  127. RowsMap map = new RowsMap();
  128. for (Row row : this) {
  129. String key = row.getString(fieldname);
  130. Rows subrows = null;
  131. if (map.containsKey(key)) {
  132. subrows = map.get(key);
  133. } else {
  134. subrows = new Rows();
  135. }
  136. subrows.add(row);
  137. map.put(key, subrows);
  138. }
  139. return map;
  140. }
  141. public RowsMap toRowsMap(String fieldnames[]) {
  142. RowsMap map = new RowsMap();
  143. for (Row row : this) {
  144. StringBuffer key = new StringBuffer();
  145. for (String fieldname : fieldnames) {
  146. key.append(row.getString(fieldname));
  147. }
  148. Rows subrows = null;
  149. if (map.containsKey(key.toString())) {
  150. subrows = map.get(key.toString());
  151. } else {
  152. subrows = new Rows();
  153. }
  154. subrows.add(row);
  155. map.put(key.toString(), subrows);
  156. }
  157. return map;
  158. }
  159. public String toXml() {
  160. Document document = DocumentHelper.createDocument();
  161. if (this.size() > 0) {
  162. Iterator<String> it = this.get(0).keySet().iterator();
  163. ArrayList<String> list = new ArrayList<>();
  164. while (it.hasNext()) {
  165. list.add(it.next());
  166. }
  167. Element root = document.addElement("rows");
  168. for (int i = 0; i < this.size(); i++) {
  169. Row row = getRow(i);
  170. Element element = root.addElement("row" + i);
  171. for (String key : list) {
  172. Object value = row.get(key);
  173. Element e = element.addElement(key);
  174. if (value == null) {
  175. e.addText("null");
  176. } else {
  177. e.addText(row.get(key).toString());
  178. }
  179. }
  180. }
  181. }
  182. return document.asXML();
  183. }
  184. }