/** * Licensed under the Artistic License; you may not use this file * except in compliance with the License. * You may obtain a copy of the License at * * http://displaytag.sourceforge.net/license.html * * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ package org.displaytag.model; import org.displaytag.exception.SortComparatorInstantiationException; import org.displaytag.util.ReflectHelper; import java.util.Comparator; import java.util.ArrayList; /** * Factory for RowSort Comparator object. * @author Fabrizio Giustina * @version $Revision: 1 $ ($Author: Chidambaram_pl $) */ public final class SortComparatorFactory { /** * Constructor for SortComparatorFactory. */ private SortComparatorFactory() { // unused } /** * If the user has specified a sortComparator, then this method takes care of creating the SortComparator (and checking to * make sure it is a subclass of the java.util.Comparator object). If there are any problems loading the SortComparator then * this will throw a SortComparatorInstantiationException which will get propagated up to the page. * @param sortComparatorName String full sortComparator class name * @return instance of Comparator * @throws SortComparatorInstantiationException if unable to load specified Comparator */ public static Comparator loadSortComparator(String sortComparatorName, Class[] parameterTypes, Object[] parameterValues) throws SortComparatorInstantiationException { if (sortComparatorName == null || sortComparatorName.length() == 0) { //return null; // If no sort comparator is provided, then use the default RowSort comparator sortComparatorName = "org.displaytag.model.RowSorter"; } Comparator comparator = null; try { Class sortComparatorClass = ReflectHelper.classForName(sortComparatorName); if (parameterTypes != null && parameterTypes.length > 0) { java.lang.reflect.Constructor constructor = sortComparatorClass.getConstructor(parameterTypes); comparator = (Comparator) constructor.newInstance(parameterValues); } else { comparator = (Comparator) sortComparatorClass.newInstance(); } return comparator; } catch (ClassNotFoundException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } catch (InstantiationException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } catch (IllegalAccessException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } catch (ClassCastException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } catch (IllegalArgumentException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } catch (NoSuchMethodException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } catch (java.lang.reflect.InvocationTargetException e) { throw new SortComparatorInstantiationException(SortComparatorFactory.class, sortComparatorName, e); } } }