Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Method;

import java.util.Vector;

public class Main {
    public static final int SORT_UP = 1;
    public static final int SORT_DOWN = 2;

    /**
     * Method sortVector.
     * @param vector Vector
     * @param getter String
     * @throws Exception
     */
    public static void sortVector(Vector vector, String getter) throws Exception {
        sortVector(vector, getter, SORT_UP);
    }

    /**
     * Method sortVector.
     * @param vector Vector
     * @param getter String
     * @param sortDirection int
     * @throws Exception
     */
    public static void sortVector(Vector vector, String getter, int sortDirection) throws Exception {
        sortVector(vector, getter, sortDirection, true);
    }

    /**
     * Method sortVector.
     * @param vector Vector
     * @param getter String
     * @param sortDirection int
     * @param caseSensitiveSort boolean
     * @throws Exception
     */
    public static void sortVector(Vector vector, String getter, int sortDirection, boolean caseSensitiveSort)
            throws Exception {
        if (vector.size() > 1) {
            sortVectorRecursive(vector, getter, 0, vector.size() - 1, sortDirection, caseSensitiveSort);
        }
    }

    /**
     * Method sortVectorRecursive.
     * @param vector Vector
     * @param getter String
     * @param low double
     * @param high double
     * @param sortDirection int
     * @param caseSensitiveSort boolean
     * @throws Exception
     */
    private static void sortVectorRecursive(Vector vector, String getter, double low, double high,
            int sortDirection, boolean caseSensitiveSort) throws Exception {
        Class[] cls = new Class[0];
        Object[] objs = new Object[0];
        Object middleVectorElem = vector.elementAt((int) ((low + high) / 2));
        Method method = null;
        Object middleObject = null;

        if (middleVectorElem != null) {
            method = middleVectorElem.getClass().getMethod(getter, cls);
            middleObject = method.invoke(middleVectorElem, objs);
        }

        Object tmpElement;
        Object compareVectorElem;
        Object compareObject;
        int i = (int) high;
        int j = (int) low;

        Class[] objectClass = new Class[1];
        objectClass[0] = Class.forName("java.lang.Object");
        Class[] stringObjectClass = new Class[1];
        stringObjectClass[0] = Class.forName("java.lang.String");

        Object[] object = new Object[1];
        object[0] = middleObject;
        Object compResult;
        boolean repeatDo;

        do {
            do {
                compareVectorElem = vector.elementAt(j);
                if (compareVectorElem != null) {
                    method = compareVectorElem.getClass().getMethod(getter, cls);
                    compareObject = method.invoke(compareVectorElem, objs);
                    if (compareObject != null) {
                        if (!(caseSensitiveSort) && compareObject instanceof String
                                && object[0] instanceof String) {
                            method = compareObject.getClass().getMethod("compareToIgnoreCase", stringObjectClass);
                        } else {
                            method = compareObject.getClass().getMethod("compareTo", objectClass);
                        }
                        if (object[0] != null) {
                            compResult = method.invoke(compareObject, object);
                        } else {
                            compResult = new Integer(1);
                        }
                    } else {
                        if (object[0] != null) {
                            compResult = new Integer(-1);
                        } else {
                            compResult = new Integer(0);
                        }
                    }
                } else {
                    if (object[0] != null) {
                        compResult = new Integer(-1);
                    } else {
                        compResult = new Integer(0);
                    }
                }

                if (((sortDirection == SORT_UP) && (((Integer) compResult).intValue() < 0))
                        || ((sortDirection == SORT_DOWN) && (((Integer) compResult).intValue() > 0))) {
                    j++;
                    repeatDo = true;
                } else {
                    repeatDo = false;
                }
            } while (repeatDo);

            do {
                compareVectorElem = vector.elementAt(i);
                if (compareVectorElem != null) {
                    method = compareVectorElem.getClass().getMethod(getter, cls);
                    compareObject = method.invoke(compareVectorElem, objs);
                    if (compareObject != null) {
                        if (!(caseSensitiveSort) && compareObject instanceof String
                                && object[0] instanceof String) {
                            method = compareObject.getClass().getMethod("compareToIgnoreCase", stringObjectClass);
                        } else {
                            method = compareObject.getClass().getMethod("compareTo", objectClass);
                        }

                        if (object[0] != null) {
                            compResult = method.invoke(compareObject, object);
                        } else {
                            compResult = new Integer(1);
                        }
                    } else {
                        if (object[0] != null) {
                            compResult = new Integer(-1);
                        } else {
                            compResult = new Integer(0);
                        }
                    }
                } else {
                    if (object[0] != null) {
                        compResult = new Integer(-1);
                    } else {
                        compResult = new Integer(0);
                    }
                }
                if (((sortDirection == SORT_UP) && (((Integer) compResult).intValue() > 0))
                        || ((sortDirection == SORT_DOWN) && (((Integer) compResult).intValue() < 0))) {
                    i--;
                    repeatDo = true;
                } else {
                    repeatDo = false;
                }
            } while (repeatDo);

            if (i >= j) {
                if (i != j) {
                    tmpElement = vector.elementAt(i);

                    vector.set(i, vector.elementAt(j));
                    vector.set(j, tmpElement);
                }

                i--;
                j++;
            }
        } while (j <= i);

        if (low < i) {
            sortVectorRecursive(vector, getter, low, i, sortDirection, caseSensitiveSort);
        }

        if (j < high) {
            sortVectorRecursive(vector, getter, j, high, sortDirection, caseSensitiveSort);
        }
    }
}