package ca.janeg.cb;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/**
 *  A constructor group object contains class constructor information separated
 *  into groups based on their access privileges. Each grouping is sorted on the
 *  constructors simple name.
 *
 *@author     Jane Griscti jane@janeg.ca
 *@created    January 13, 2002
 */
class ConstructorGroup {

    private final Class owner;
    private Constructor[] ctors;
    private Constructor[] publicConstructors;
    private Constructor[] protectedConstructors;
    private Constructor[] packageConstructors;
    private Constructor[] privateConstructors;

    boolean hasCtors;


    /**
     *  Creates a ConstructorGroup object.
     *
     *@param  owner  the class object the methods are derived from
     */
    ConstructorGroup( final Class owner ) {
        this.owner = owner;
        ctors = owner.getDeclaredConstructors();
        Arrays.sort( ctors, NameComparator.getInstance() );

        hasCtors = Array.getLength( ctors ) > 0;
        if( hasCtors ) separateByAccess();
    }


    private void separateByAccess() {
        Object[] obj  = AccessSeparator.separate( ctors );

        ArrayList al  = (ArrayList)obj[0];
        publicConstructors = (Constructor[])al.toArray( new Constructor[0] );

        al = (ArrayList)obj[1];
        protectedConstructors = (Constructor[])al.toArray( new Constructor[0] );

        al = (ArrayList)obj[2];
        privateConstructors = (Constructor[])al.toArray( new Constructor[0] );

        al = (ArrayList)obj[3];
        packageConstructors = (Constructor[])al.toArray( new Constructor[0] );
    }



    Constructor[] getPublicConstructors() {
        return publicConstructors;
    }


    Constructor[] getProtectedConstructors() {
        return protectedConstructors;
    }


    Constructor[] getPrivateConstructors() {
        return privateConstructors;
    }


    Constructor[] getPackageConstructors() {
        return packageConstructors;
    }


    Constructor[] getAllConstructors() {
        return ctors;
    }

}