Example usage for org.apache.commons.collections ComparatorUtils naturalComparator

List of usage examples for org.apache.commons.collections ComparatorUtils naturalComparator

Introduction

In this page you can find the example usage for org.apache.commons.collections ComparatorUtils naturalComparator.

Prototype

public static Comparator naturalComparator() 

Source Link

Document

Gets a comparator that uses the natural order of the objects.

Usage

From source file:mondrian.olap.fun.PartialSortTest.java

@SuppressWarnings({ "unchecked" })
private void doPartialSort(Object[] items, boolean descending, int limit) {
    Comparator<Object> comp = ComparatorUtils.naturalComparator();
    if (descending) {
        comp = ComparatorUtils.reversedComparator(comp);
    }/*from  w  ww .  j a v a2s .c  om*/
    FunUtil.partialSort(items, comp, limit);
}

From source file:mondrian.olap.fun.PartialSortTest.java

private static <T extends Comparable> boolean isPartiallySorted(T[] vec, int limit, boolean descending) {
    //noinspection unchecked
    return isPartiallySorted(vec, limit, (Comparator<T>) ComparatorUtils.naturalComparator(), descending);
}

From source file:com.facultyshowcase.app.cmscomp.professor.list.ProfessorProfileListGenerator.java

public String getIdentity(CmsRequest<ProfessorProfileListCMSBean> request) {

    Date lastModifiedDate = (Date) ComparatorUtils.min(request.getPageElement().getLastModified(),
            _ProfessorProfileDAO.getLastModifiedDate(),
            ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator()));

    return Long.toString(lastModifiedDate.getTime());

}

From source file:com.facultyshowcase.app.cmscomp.professor.viewer.ProfessorProfileGenerator.java

@Override
public String getIdentity(CmsRequest<ProfessorProfileCMSBean> request) {

    String slug = getSlug(request);

    if (StringFactory.trimToNull(slug) == null) {
        return super.getIdentity(request);
    }//from  w ww .java2  s. c  om

    Date lastModifiedDate = (Date) ComparatorUtils.min(request.getPageElement().getLastModified(),
            _ProfessorProfileDAO.getLastModifiedDate(slug),
            ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator()));

    return slug + " " + lastModifiedDate.getTime();

}

From source file:mondrian.olap.fun.PartialSortTest.java

private void speedTest(Logger logger, int length, int limit) {
    logger.debug("sorting the max " + limit + " of " + length + " random Integers");

    // random input, 3 copies
    // repeated keys
    Integer[] vec1 = newRandomIntegers(length, 0, length / 5);
    Integer[] vec2 = vec1.clone();
    Integer[] vec3 = vec1.clone();
    Integer[] vec4 = vec1.clone();

    // full sort vec1
    long now = System.currentTimeMillis();
    Arrays.sort(vec1);/*w w  w  .j a v a  2s  .  c o m*/
    long dt = System.currentTimeMillis() - now;
    logger.debug(" full mergesort took " + dt + " msecs");

    // partial sort vec2
    now = System.currentTimeMillis();
    doPartialSort(vec2, true, limit);
    dt = System.currentTimeMillis() - now;
    logger.debug(" partial quicksort took " + dt + " msecs");

    // marc's stable partial quicksort vec3
    @SuppressWarnings({ "unchecked" })
    Comparator<Integer> comp = new ReverseComparator(ComparatorUtils.naturalComparator());
    List<Integer> vec3List = Arrays.asList(vec3);
    now = System.currentTimeMillis();
    FunUtil.stablePartialSort(vec3List, comp, limit, 2);
    dt = System.currentTimeMillis() - now;
    logger.debug(" marc's stable partial quicksort took " + dt + " msecs");

    // julian's algorithm stable partial sort vec4
    @SuppressWarnings({ "unchecked" })
    List<Integer> vec4List = Arrays.asList(vec4);
    now = System.currentTimeMillis();
    FunUtil.stablePartialSort(vec4List, comp, limit, 4);
    dt = System.currentTimeMillis() - now;
    logger.debug(" julian's stable partial sort took " + dt + " msecs");
}

From source file:mondrian.olap.fun.FunUtil.java

/**
 * Partial Sort: sorts in place an array of Objects using a given Comparator,
 * but only enough so that the N biggest (or smallest) items are at the start
 * of the array. Not a stable sort, unless the Comparator is so contrived.
 *
 * @param items will be partially-sorted in place
 * @param comp a Comparator; null means use natural comparison
 *///ww  w .  j a  v a2  s  .c  om
static <T> void partialSort(T[] items, Comparator<T> comp, int limit) {
    if (comp == null) {
        //noinspection unchecked
        comp = (Comparator<T>) ComparatorUtils.naturalComparator();
    }
    new Quicksorter<T>(items, comp).partialSort(limit);
}

From source file:org.apache.openmeetings.remote.WhiteBoardService.java

public Cliparts getClipArtIcons() {
    try {//from w  w w .  j a  v  a 2  s  .co m
        File clipart_dir = OmFileHelper.getPublicClipartsDir();

        FilenameFilter getFilesOnly = new FilenameFilter() {
            public boolean accept(File b, String name) {
                File f = new File(b, name);
                return !f.isDirectory();
            }
        };

        FilenameFilter getDirectoriesOnly = new FilenameFilter() {
            public boolean accept(File b, String name) {
                File f = new File(b, name);
                return f.isDirectory() && !f.getName().equals("thumb");
            }
        };

        Cliparts cl = new Cliparts();
        cl.setFolderName("general");

        String[] files_general = clipart_dir.list(getFilesOnly);
        @SuppressWarnings("unchecked")
        Comparator<String> comparator = ComparatorUtils.naturalComparator();
        Arrays.sort(files_general, comparator);

        cl.setGeneralList(files_general);
        cl.setSubCategories(new LinkedList<Cliparts>());

        for (File dir : clipart_dir.listFiles(getDirectoriesOnly)) {
            Cliparts cl_sub = new Cliparts();
            cl_sub.setFolderName("math");
            String[] files = dir.list(getFilesOnly);
            Arrays.sort(files, comparator);
            cl_sub.setGeneralList(files);
            cl.getSubCategories().add(cl_sub);
        }

        return cl;

    } catch (Exception err) {
        log.error("[getClipArtIcons]", err);
    }
    return null;
}

From source file:org.dspace.app.itemimport.ItemImportServiceImpl.java

@Override
public void addItems(Context c, List<Collection> mycollections, String sourceDir, String mapFile,
        boolean template) throws Exception {
    // create the mapfile
    File outFile = null;/*from   w  w w .j  a  v  a 2s.  co  m*/
    PrintWriter mapOut = null;
    try {
        Map<String, String> skipItems = new HashMap<>(); // set of items to skip if in 'resume'
        // mode

        System.out.println("Adding items from directory: " + sourceDir);
        log.debug("Adding items from directory: " + sourceDir);
        System.out.println("Generating mapfile: " + mapFile);
        log.debug("Generating mapfile: " + mapFile);

        boolean directoryFileCollections = false;
        if (mycollections == null) {
            directoryFileCollections = true;
        }

        if (!isTest) {
            // get the directory names of items to skip (will be in keys of
            // hash)
            if (isResume) {
                skipItems = readMapFile(mapFile);
            }

            // sneaky isResume == true means open file in append mode
            outFile = new File(mapFile);
            mapOut = new PrintWriter(new FileWriter(outFile, isResume));

            if (mapOut == null) {
                throw new Exception("can't open mapfile: " + mapFile);
            }
        }

        // open and process the source directory
        File d = new java.io.File(sourceDir);

        if (d == null || !d.isDirectory()) {
            throw new Exception("Error, cannot open source directory " + sourceDir);
        }

        String[] dircontents = d.list(directoryFilter);

        Arrays.sort(dircontents, ComparatorUtils.naturalComparator());

        for (int i = 0; i < dircontents.length; i++) {
            if (skipItems.containsKey(dircontents[i])) {
                System.out.println("Skipping import of " + dircontents[i]);
            } else {
                List<Collection> clist;
                if (directoryFileCollections) {
                    String path = sourceDir + File.separatorChar + dircontents[i];
                    try {
                        List<Collection> cols = processCollectionFile(c, path, "collections");
                        if (cols == null) {
                            System.out.println(
                                    "No collections specified for item " + dircontents[i] + ". Skipping.");
                            continue;
                        }
                        clist = cols;
                    } catch (IllegalArgumentException e) {
                        System.out.println(e.getMessage() + " Skipping.");
                        continue;
                    }
                } else {
                    clist = mycollections;
                }
                Item item = addItem(c, clist, sourceDir, dircontents[i], mapOut, template);
                c.uncacheEntity(item);
                System.out.println(i + " " + dircontents[i]);
            }
        }

    } finally {
        if (mapOut != null) {
            mapOut.flush();
            mapOut.close();
        }
    }
}

From source file:org.eclipse.jubula.client.alm.mylyn.ui.dialogs.InspectALMAttributesDialog.java

/**
 * @return a null safe natural comparator
 */
private Comparator getCommonsComparator() {
    return ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator());
}

From source file:org.eclipse.jubula.client.ui.views.TestresultSummaryView.java

/**
 * Creates and returns a comparator for natural comparison that can also 
 * handle <code>null</code> values. 
 * /*  w  ww .  j a  v a2 s .c o m*/
 * @return the created comparator.
 */
@SuppressWarnings("rawtypes")
private static Comparator getCommonsComparator() {
    return ComparatorUtils.nullHighComparator(ComparatorUtils.naturalComparator());
}