分类统计数据库数据
2008-08-23 23:24
381 查看
/**double 数据类型*/
private static final String STR_DOUBLE="double";
/**INT 数据类型*/
private static final String STR_INT="int";
/**long 数据类型*/
private static final String STR_LONG="long";
/**标志位*/
private static final String TOTAL_PROPERTY_NAME="sumFlag";
/**统计所有数据*/
public static final String SUM_FLAG_TOTAL="SUM_FLAG_TOTAL";
/**分组统计数据*/
public static final String SUM_FLAG_GROUP = "SUM_FLAG_GROUP";
private static final String STR_EMPTY = "";
private static final String STR_ZERO = "0.0";
private static final String SITE_FIRST = "first";
private static final String SITE_LAST = "last";
private static final Logger log = Logger.getLogger(GroupUtil.class);
public GroupUtil() {
super();
}
public static Collection appendTotal(Collection datas,String[] sumPropertys){
if(datas==null){
return null;
}
if(datas.size()==0){
return datas;
}
ArrayList coll = new ArrayList();
try{
coll.addAll(datas);
appendTotalObj(coll,sumPropertys,SUM_FLAG_TOTAL,SITE_LAST);
}catch(Exception e){
log.error(e.getMessage());
coll = null;
}
return coll;
}
public static Collection group(Collection datas,String groupProperty,String[] sumPropertys){
if(datas==null){
return null;
}
if(datas.size()==0){
return datas;
}
Collection resultVals = groupBy4Datas(datas,groupProperty,sumPropertys,SITE_LAST);
return resultVals;
}
public static Collection group(Collection datas,
String groupProperty,
String[] sumPropertys, boolean sumRowAtFirst) {
if (datas == null) {
return null;
}
if (datas.size() == 0) {
return datas;
}
if (sumRowAtFirst) {
return groupBy4Datas(datas, groupProperty, sumPropertys,
SITE_FIRST);
}
else {
return groupBy4Datas(datas, groupProperty, sumPropertys,
SITE_LAST);
}
}
public static void appendTotalObj(List datas,String[] sumPropertys,String totalFlag,String site)
throws Exception{
BigDecimal bigDecimal = null;
String properTyp = null;
String strVal = null;
Object obj = null;
Map map = new HashMap();
for(int i=0;i<sumPropertys.length;i++){
bigDecimal = new BigDecimal(STR_ZERO);
for(int j=0;j<datas.size();j++){
obj = datas.get(j);
if(properTyp==null){
properTyp = getPropertyTypName(obj,sumPropertys[i]);
}
strVal = BeanUtils.getProperty(obj,sumPropertys[i]);
bigDecimal = bigDecimal.add(new BigDecimal(strVal));
}
if(properTyp.equals(STR_DOUBLE)){
map.put(sumPropertys[i],bigDecimal.doubleValue()+STR_EMPTY);
}else if(properTyp.equals(STR_INT)){
map.put(sumPropertys[i],bigDecimal.intValue()+STR_EMPTY);
}else if(properTyp.equals(STR_LONG)){
map.put(sumPropertys[i],bigDecimal.longValue()+STR_EMPTY);
}
properTyp = null;
}
Object newObj = getNewObj(obj,totalFlag);
BeanUtils.copyProperties(newObj,map);
if(SITE_LAST.equals(site)){
datas.add(newObj);
}else{
((List)datas).add(0,newObj);
}
}
protected static String getPropertyTypName(Object obj,String property) throws Exception{
String name = STR_EMPTY;
PropertyDescriptor proDes = PropertyUtils.getPropertyDescriptor(obj,property);
Class propertyClass = proDes.getPropertyType();
name = propertyClass.getName();
return name;
}
protected static Object getNewObj(Object obj,String totalFlag) throws Exception{
Object newObj = obj.getClass().newInstance();
Map map = new HashMap();
map.put(TOTAL_PROPERTY_NAME,totalFlag);
BeanUtils.copyProperties(newObj,obj);
BeanUtils.copyProperties(newObj,map);
return newObj;
}
protected static Collection groupBy4Datas(Collection datas,String groupProperty,
String[] sumPropertys, String site){
ArrayList totalList = new ArrayList();
Collection resultVals = null;
try{
totalList.addAll(datas);
appendTotalObj(totalList,sumPropertys,SUM_FLAG_TOTAL,SITE_LAST);
Object obj = totalList.remove(totalList.size()-1);
Map splitMap = splitDatas(totalList,groupProperty);
ArrayList keyList = new ArrayList();
keyList.addAll(splitMap.keySet());
Iterator it = keyList.iterator();
List coll = null;
while(it.hasNext()){
coll = (List)splitMap.get(it.next());
appendTotalObj(coll,sumPropertys,SUM_FLAG_GROUP,site);
}
Collections.sort(keyList);
resultVals = new ArrayList();
it = keyList.iterator();
while(it.hasNext()){
coll = (List) splitMap.get(it.next());
resultVals.addAll(coll);
}
resultVals.add(obj);
}catch(Exception ex){
log.error(ex.getMessage());
resultVals = null;
}
return resultVals;
}
protected static Map splitDatas(List datas,String groupProerty)throws Exception{
Map splitDatas = new HashMap();
Iterator it = datas.iterator();
Object obj = null;
String key = null;
List splitList = null;
while(it.hasNext()){
obj = it.next();
key = BeanUtils.getProperty(obj,groupProerty);
if(splitDatas.containsKey(key)){
splitList = (List)splitDatas.get(key);
splitList.add(obj);
}else{
splitList = new ArrayList();
splitList.add(obj);
splitDatas.put(key,splitList);
}
}
return splitDatas;
}
String[] sumPropertys = {"accuDepr","mdeprqu"};
return GroupUtil.group(fiaValChaDAO.query4Excel(para),"assetTyp",sumPropertys);
private static final String STR_DOUBLE="double";
/**INT 数据类型*/
private static final String STR_INT="int";
/**long 数据类型*/
private static final String STR_LONG="long";
/**标志位*/
private static final String TOTAL_PROPERTY_NAME="sumFlag";
/**统计所有数据*/
public static final String SUM_FLAG_TOTAL="SUM_FLAG_TOTAL";
/**分组统计数据*/
public static final String SUM_FLAG_GROUP = "SUM_FLAG_GROUP";
private static final String STR_EMPTY = "";
private static final String STR_ZERO = "0.0";
private static final String SITE_FIRST = "first";
private static final String SITE_LAST = "last";
private static final Logger log = Logger.getLogger(GroupUtil.class);
public GroupUtil() {
super();
}
public static Collection appendTotal(Collection datas,String[] sumPropertys){
if(datas==null){
return null;
}
if(datas.size()==0){
return datas;
}
ArrayList coll = new ArrayList();
try{
coll.addAll(datas);
appendTotalObj(coll,sumPropertys,SUM_FLAG_TOTAL,SITE_LAST);
}catch(Exception e){
log.error(e.getMessage());
coll = null;
}
return coll;
}
public static Collection group(Collection datas,String groupProperty,String[] sumPropertys){
if(datas==null){
return null;
}
if(datas.size()==0){
return datas;
}
Collection resultVals = groupBy4Datas(datas,groupProperty,sumPropertys,SITE_LAST);
return resultVals;
}
public static Collection group(Collection datas,
String groupProperty,
String[] sumPropertys, boolean sumRowAtFirst) {
if (datas == null) {
return null;
}
if (datas.size() == 0) {
return datas;
}
if (sumRowAtFirst) {
return groupBy4Datas(datas, groupProperty, sumPropertys,
SITE_FIRST);
}
else {
return groupBy4Datas(datas, groupProperty, sumPropertys,
SITE_LAST);
}
}
public static void appendTotalObj(List datas,String[] sumPropertys,String totalFlag,String site)
throws Exception{
BigDecimal bigDecimal = null;
String properTyp = null;
String strVal = null;
Object obj = null;
Map map = new HashMap();
for(int i=0;i<sumPropertys.length;i++){
bigDecimal = new BigDecimal(STR_ZERO);
for(int j=0;j<datas.size();j++){
obj = datas.get(j);
if(properTyp==null){
properTyp = getPropertyTypName(obj,sumPropertys[i]);
}
strVal = BeanUtils.getProperty(obj,sumPropertys[i]);
bigDecimal = bigDecimal.add(new BigDecimal(strVal));
}
if(properTyp.equals(STR_DOUBLE)){
map.put(sumPropertys[i],bigDecimal.doubleValue()+STR_EMPTY);
}else if(properTyp.equals(STR_INT)){
map.put(sumPropertys[i],bigDecimal.intValue()+STR_EMPTY);
}else if(properTyp.equals(STR_LONG)){
map.put(sumPropertys[i],bigDecimal.longValue()+STR_EMPTY);
}
properTyp = null;
}
Object newObj = getNewObj(obj,totalFlag);
BeanUtils.copyProperties(newObj,map);
if(SITE_LAST.equals(site)){
datas.add(newObj);
}else{
((List)datas).add(0,newObj);
}
}
protected static String getPropertyTypName(Object obj,String property) throws Exception{
String name = STR_EMPTY;
PropertyDescriptor proDes = PropertyUtils.getPropertyDescriptor(obj,property);
Class propertyClass = proDes.getPropertyType();
name = propertyClass.getName();
return name;
}
protected static Object getNewObj(Object obj,String totalFlag) throws Exception{
Object newObj = obj.getClass().newInstance();
Map map = new HashMap();
map.put(TOTAL_PROPERTY_NAME,totalFlag);
BeanUtils.copyProperties(newObj,obj);
BeanUtils.copyProperties(newObj,map);
return newObj;
}
protected static Collection groupBy4Datas(Collection datas,String groupProperty,
String[] sumPropertys, String site){
ArrayList totalList = new ArrayList();
Collection resultVals = null;
try{
totalList.addAll(datas);
appendTotalObj(totalList,sumPropertys,SUM_FLAG_TOTAL,SITE_LAST);
Object obj = totalList.remove(totalList.size()-1);
Map splitMap = splitDatas(totalList,groupProperty);
ArrayList keyList = new ArrayList();
keyList.addAll(splitMap.keySet());
Iterator it = keyList.iterator();
List coll = null;
while(it.hasNext()){
coll = (List)splitMap.get(it.next());
appendTotalObj(coll,sumPropertys,SUM_FLAG_GROUP,site);
}
Collections.sort(keyList);
resultVals = new ArrayList();
it = keyList.iterator();
while(it.hasNext()){
coll = (List) splitMap.get(it.next());
resultVals.addAll(coll);
}
resultVals.add(obj);
}catch(Exception ex){
log.error(ex.getMessage());
resultVals = null;
}
return resultVals;
}
protected static Map splitDatas(List datas,String groupProerty)throws Exception{
Map splitDatas = new HashMap();
Iterator it = datas.iterator();
Object obj = null;
String key = null;
List splitList = null;
while(it.hasNext()){
obj = it.next();
key = BeanUtils.getProperty(obj,groupProerty);
if(splitDatas.containsKey(key)){
splitList = (List)splitDatas.get(key);
splitList.add(obj);
}else{
splitList = new ArrayList();
splitList.add(obj);
splitDatas.put(key,splitList);
}
}
return splitDatas;
}
String[] sumPropertys = {"accuDepr","mdeprqu"};
return GroupUtil.group(fiaValChaDAO.query4Excel(para),"assetTyp",sumPropertys);
相关文章推荐
- Mapreduce——视频播放数据分类统计
- Android创建和使用数据库详… 分类: Android数据存储 2014-05-30 10:58 82人阅读 评论(0) 收藏
- 通用数据采集与统计分析管理系统,随数据库结构的调整而自动调整
- 概率论与数据统计在分类预测中的原理介绍(信息增益、交叉熵等)
- 使用Python实现子区域数据分类统计
- 使用Python实现子区域数据分类统计
- sql server的数据库个数、表个数及表的数据量统计
- ASP.NET2.0里轻松获取数据库连接统计数据
- oracle 数据库 最近两小时 数据文件 io 统计信息
- CSDN用户数据库泄漏的一些统计数据
- 我要啦免费统计(51.la)统计数据库损坏,很可能会丢失用户的统计数据
- 数据库数据泵导出之前的完美数据量统计
- 快速统计某个数据库中每张表的数据量
- Excel透视技巧-三级分类统计名单、分类统计数据
- Excel在统计分析中的应用—第三章—数据库统计函数与数据透视表-Part1-(数据查询与筛选、分类汇总)
- 使用Python实现子区域数据分类统计
- 使用Python实现子区域数据分类统计
- 使用Python实现子区域数据分类统计
- python抓取几大票房统计系统数据的之专资办票房数据库
- 统计数据库已分组的表数据行数