App Programming/JAVA2008. 5. 26. 12:12

/*
 * Created on 2003. 5. 23.
 *
 */
package com.lgeds.jdf;

import java.lang.reflect.Field;

/**
 * @author 정봉섭
 * theflowing@nate.com
 * value Object의 배열을 구조화 시키는  method의 집합
 */
public class ArrayMart {

 public static final int ASC = 1;
 public static final int DESC = 2;

 /**
  * @param objs
  * @param fieldName
  * @param sort int ASC, DESC
  * @return
  * @throws ArrayMartException
  * 인수로 주어진 배열을 fieldName순으로 배열한다.<br>
  * 이 때 인수로 주어진 attribute는 public으로 선언되어 있어야 한다.
  */
 public Object[] orderby(Object[] objs, String fieldName, int sort)
  throws ArrayMartException {

  Class nowClass = null;
  Class comClass = null;

  Field nowField = null;
  Field comField = null;

  Object nowobj = null;

  boolean how;
  String fieldtype = null;

  try {

   if (objs.length > 0) {
    fieldtype = getFieldType(objs[0], fieldName);
   }

   int k;
   for (int i = 0; i < objs.length; i++) {
    nowobj = objs[i];
    nowClass = objs[i].getClass();
    nowField = nowClass.getDeclaredField(fieldName);

    k = i;
    for (int j = i + 1; j < objs.length; j++) {
     comClass = objs[j].getClass();
     comField = comClass.getDeclaredField(fieldName);
     how =
      compare(
       fieldtype,
       objs[j],
       objs[i],
       comField,
       nowField);

     if ((sort == ASC && !how) || (sort == DESC && how)) {
      nowobj = objs[j];
      k = j;
     }
    }

    objs[k] = objs[i];
    objs[i] = nowobj;

   }
  } catch (Exception e) {
   throw new ArrayMartException(e.getMessage());
  }

  return objs;
 }

 /**
  * @param objs
  * @param fieldName  순서되로 배열되는 attribute
  * @param markFieldName 순위가 기록되는 attribute
  * @param sort
  * @return
  * @throws ArrayMartException
  *
  * 인수로 주어진 배열을 fieldName순으로 배열하고,<br>
  * 그 순위를 기록한다.
  * 이 때 인수로 주어진 attribute는 public으로 선언되어 있어야 한다.
  */
 public Object[] markOrder(
  Object[] objs,
  String fieldName,
  String markFieldName,
  int sort)
  throws ArrayMartException {
  Class nowClass = null;
  Class comClass = null;
  java.lang.reflect.Field nowField = null;
  java.lang.reflect.Field comField = null;
  java.lang.reflect.Field markField = null;

  Object nowobj = null;

  boolean how;
  String fieldtype = null;

  try {
   if (objs.length > 0) {
    fieldtype = getFieldType(objs[0], fieldName);
   }
   int k;
   for (int i = 0; i < objs.length; i++) {
    nowobj = objs[i];
    nowClass = objs[i].getClass();
    nowField = nowClass.getDeclaredField(fieldName);

    k = i;
    for (int j = i + 1; j < objs.length; j++) {
     comClass = objs[j].getClass();
     comField = comClass.getDeclaredField(fieldName);

     how =
      compare(
       fieldtype,
       objs[j],
       objs[i],
       comField,
       nowField);

     if ((sort == ASC && !how) || (sort == DESC && how)) {
      nowobj = objs[j];
      k = j;
     }
    }

    objs[k] = objs[i];
    objs[i] = nowobj;

    markField = nowClass.getDeclaredField(markFieldName);

    if (i == 0) {
     markField.setInt(objs[i], 1);
    } else {
     markField.setInt(
      objs[i],
      getSameOrder(
       fieldtype,
       objs[i - 1],
       objs[i],
       fieldName,
       markFieldName,
       i));
    }

   }

   return objs;
  } catch (Exception e) {
   throw new ArrayMartException(e.getMessage());
  }
 }

 /**
  * @param objs
  * @param fieldName
  * @return
  * @throws ArrayMartException
  * 인수로 주어진 배열을 fieldName순으로 배열한다.<br>
  * 이 때 인수로 주어진 attribute는 public으로 선언되어 있어야 한다.
  * 배열 순서는 ASC 이다.
  */
 public Object[] orderby(Object[] objs, String fieldName)
  throws ArrayMartException {
  return orderby(objs, fieldName, ASC);
 }

 /**
   * @param objs
   * @param fieldName
   * @return
   * @throws ArrayMartException
   * 인수로 주어진 필드의 합계를 계산한다.
   */
 public double getFieldSum(Object[] objs, String fieldName)
  throws ArrayMartException {
  Class c = null;
  java.lang.reflect.Field field = null;
  double sum = 0;
  String fieldtype = null;

  try {
   if (objs.length > 0) {
    fieldtype = getFieldType(objs[0], fieldName);
   }
   for (int i = 0; i < objs.length; i++) {
    c = objs[i].getClass();
    field = c.getDeclaredField(fieldName);

    if (fieldtype.equals("java.lang.String")) {
     sum = sum + Double.parseDouble((String) field.get(objs[i]));
    } else if (fieldtype.equals("int")) {
     sum = sum + field.getInt(objs[i]);
    } else if (fieldtype.equals("double")) {
     sum = sum + field.getDouble(objs[i]);
    } else if (fieldtype.equals("long")) {
     sum = sum + field.getLong(objs[i]);
    } else if (fieldtype.equals("float")) {
     sum = sum + field.getFloat(objs[i]);
    } else {
     throw new ArrayMartException("field type not match to calculate");
    }
   }

   return sum;
  } catch (Exception e) {
   throw new ArrayMartException(e.getMessage());
  }
 }

 private boolean compare(
  String fieldtype,
  Object oj,
  Object oi,
  Field fj,
  Field fi)
  throws ArrayMartException, IllegalArgumentException, IllegalAccessException {
  boolean how;
  if (fieldtype.equals("java.lang.String")) {
   how = ((String) fj.get(oj)).compareTo((String) fi.get(oi)) > 0;
  } else if (fieldtype.equals("int")) {
   how = fj.getInt(oj) > fi.getInt(oi);
  } else if (fieldtype.equals("double")) {
   how = fj.getDouble(oj) > fi.getDouble(oi);
  } else if (fieldtype.equals("long")) {
   how = fj.getLong(oj) > fi.getLong(oi);
  } else if (fieldtype.equals("float")) {
   how = fj.getFloat(oj) > fi.getFloat(oi);
  } else {
   throw new ArrayMartException("field type not match order");
  }
  return how;
 }

 private boolean equals(
  String fieldtype,
  Object oj,
  Object oi,
  Field fj,
  Field fi)
  throws ArrayMartException, IllegalArgumentException, IllegalAccessException {
  boolean how;
  if (fieldtype.equals("java.lang.String")) {
   how = ((String) fj.get(oj)).compareTo((String) fi.get(oi)) == 0;
  } else if (fieldtype.equals("int")) {
   how = fj.getInt(oj) == fi.getInt(oi);
  } else if (fieldtype.equals("double")) {
   how = fj.getDouble(oj) == fi.getDouble(oi);
  } else if (fieldtype.equals("long")) {
   how = fj.getLong(oj) == fi.getLong(oi);
  } else if (fieldtype.equals("float")) {
   how = fj.getFloat(oj) == fi.getFloat(oi);
  } else {
   throw new ArrayMartException("field type not match order");
  }
  return how;
 }

 private String getFieldType(Object obj, String name) throws Exception {

  Class cls = null;
  Field field = null;
  cls = obj.getClass();
  field = cls.getDeclaredField(name);
  return field.getType().getName();

 }
 private int getSameOrder(
  String fieldtype,
  Object previous,
  Object now,
  String fieldName,
  String markName,
  int order)
  throws Exception {

  Class preClass = previous.getClass();
  Class nowClass = now.getClass();

  Field preField = preClass.getDeclaredField(fieldName);
  Field nowField = nowClass.getDeclaredField(fieldName);
  Field markField = preClass.getDeclaredField(markName);

  boolean is = equals(fieldtype, previous, now, preField, nowField);

  if (is) {
   order = markField.getInt(previous);
  } else {
   order = order + 1;
  }
  return order;
 }

}



Posted by BAGE