Example usage for java.util Comparator Comparator

List of usage examples for java.util Comparator Comparator

Introduction

In this page you can find the example usage for java.util Comparator Comparator.

Prototype

Comparator

Source Link

Usage

From source file:com.github.jinahya.sql.database.metadata.bind.PseudoColumn.java

public static Comparator<PseudoColumn> natural() {

    return new Comparator<PseudoColumn>() {

        @Override//from w  ww  .jav a2  s  .c om
        public int compare(final PseudoColumn o1, final PseudoColumn o2) {

            // by TABLE_CAT,TABLE_SCHEM, TABLE_NAME and COLUMN_NAME.
            return new CompareToBuilder().append(o1.getTableCat(), o2.getTableCat())
                    .append(o1.getTableSchem(), o2.getTableSchem()).append(o1.getTableName(), o2.getTableName())
                    .append(o1.getColumnName(), o2.getColumnName()).build();
        }

    };
}

From source file:com.github.jinahya.sql.database.metadata.bind.Procedure.java

public static Comparator<Procedure> natural() {

    return new Comparator<Procedure>() {

        @Override/*from w  w w. j a v  a 2s .  c  o  m*/
        public int compare(final Procedure o1, final Procedure o2) {

            // by PROCEDURE_CAT, PROCEDURE_SCHEM, PROCEDURE_NAME
            // and SPECIFIC_NAME.
            return new CompareToBuilder().append(o1.getProcedureCat(), o2.getProcedureCat())
                    .append(o1.getProcedureSchem(), o2.getProcedureSchem())
                    .append(o1.getProcedureName(), o2.getProcedureName())
                    .append(o1.getSpecificName(), o2.getSpecificName()).build();
        }

    };
}

From source file:net.rrm.ehour.project.util.ProjectUtil.java

private static List<Project> filterBillability(Collection<Project> projects, boolean billable) {
    List<Project> sortedProjects = new ArrayList<>();

    for (Project project : projects) {
        if (project.isBillable() == billable) {
            sortedProjects.add(project);
        }//from  www . j  av  a  2 s . com
    }

    Collections.sort(sortedProjects, new Comparator<Project>() {
        @Override
        public int compare(Project o1, Project o2) {
            return new CompareToBuilder().append(o1.getCustomer(), o2.getCustomer())
                    .append(o1.getName(), o2.getName()).append(o1.getProjectCode(), o2.getProjectCode())
                    .append(o1.getProjectId(), o2.getProjectId()).toComparison();
        }
    });
    return sortedProjects;
}

From source file:net.firejack.platform.core.utils.SortFileUtils.java

/**
 * @param files/*from w  w  w .  ja v a  2  s. co  m*/
 * @param desc
 * @return
 */
public static File[] sortingByExt(File[] files, boolean desc) {
    Arrays.sort(files, new Comparator() {
        public int compare(final Object o1, final Object o2) {
            String s1 = ((File) o1).getName().toLowerCase();
            String s2 = ((File) o2).getName().toLowerCase();
            final int s1Dot = s1.lastIndexOf('.');
            final int s2Dot = s2.lastIndexOf('.');
            //
            if ((s1Dot == -1) == (s2Dot == -1)) { // both or neither
                s1 = s1.substring(s1Dot + 1);
                s2 = s2.substring(s2Dot + 1);
                Integer i1 = Integer.parseInt(s1);
                Integer i2 = Integer.parseInt(s2);
                return i1.compareTo(i2);
            } else if (s1Dot == -1) { // only s2 has an extension, so s1 goes first
                return -1;
            } else { // only s1 has an extension, so s1 goes second
                return 1;
            }
        }
    });
    if (desc) {
        org.apache.commons.lang.ArrayUtils.reverse(files);
    }
    return files;
}

From source file:de.hasait.clap.impl.AbstractCLAPNode.java

protected static final void addHelpNode(final Map<CLAPHelpCategoryImpl, Set<CLAPHelpNode>> pOptionNodes,
        final CLAPHelpCategoryImpl pCurrentCategory, final CLAPHelpNode pNode) {
    final CLAPHelpCategoryImpl currentCategory = pNode.getHelpCategory() != null ? pNode.getHelpCategory()
            : pCurrentCategory;/* ww w  .  ja v  a 2  s.c  o  m*/
    if (!pOptionNodes.containsKey(currentCategory)) {
        pOptionNodes.put(currentCategory, new TreeSet<CLAPHelpNode>(new Comparator<CLAPHelpNode>() {

            @Override
            public int compare(final CLAPHelpNode pO1, final CLAPHelpNode pO2) {
                return pO1.getHelpID().compareTo(pO2.getHelpID());
            }

        }));
    }
    pOptionNodes.get(currentCategory).add(pNode);
}

From source file:com.github.jinahya.sql.database.metadata.bind.CrossReference.java

public static Comparator<CrossReference> natural() {

    return new Comparator<CrossReference>() {

        @Override//from  w w  w  . ja va 2 s .  co  m
        public int compare(final CrossReference o1, final CrossReference o2) {

            // by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ.
            return new CompareToBuilder().append(o1.getFktableCat(), o2.getFktableCat())
                    .append(o1.getFktableSchem(), o2.getFktableSchem())
                    .append(o1.getFktableName(), o2.getFktableName()).append(o1.getKeySeq(), o2.getKeySeq())
                    .build();
        }

    };
}

From source file:CompTest.java

public static Comparator integerComparator() {
    return new Comparator() {

        public int compare(Object o1, Object o2) {
            int val1 = ((Integer) o1).intValue();
            int val2 = ((Integer) o2).intValue();
            return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));
        }/*  w  w w. j  a va  2 s  .co  m*/
    };
}

From source file:com.github.jinahya.sql.database.metadata.bind.Schema.java

public static Comparator<Schema> narual() {

    return new Comparator<Schema>() {

        @Override/*from w  ww.j  av a2  s  .  co  m*/
        public int compare(final Schema o1, final Schema o2) {

            // by TABLE_CATALOG and TABLE_SCHEM.
            return new CompareToBuilder().append(o1.getTableCatalog(), o2.getTableCatalog())
                    .append(o1.getTableSchem(), o2.getTableSchem()).build();
        }

    };
}

From source file:Main.java

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {

    List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
    if (rawSupportedSizes == null) {
        Log.w(TAG, "Device returned no supported preview sizes; using default");
        Camera.Size defaultSize = parameters.getPreviewSize();
        return new Point(defaultSize.width, defaultSize.height);
    }//from   ww w  . j av a2s .co m

    // Sort by size, descending
    List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(rawSupportedSizes);
    Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
        @Override
        public int compare(Camera.Size a, Camera.Size b) {
            int aPixels = a.height * a.width;
            int bPixels = b.height * b.width;
            if (bPixels < aPixels) {
                return -1;
            }
            if (bPixels > aPixels) {
                return 1;
            }
            return 0;
        }
    });

    if (Log.isLoggable(TAG, Log.INFO)) {
        StringBuilder previewSizesString = new StringBuilder();
        for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
            previewSizesString.append(supportedPreviewSize.width).append('x')
                    .append(supportedPreviewSize.height).append(' ');
        }
        Log.i(TAG, "Supported preview sizes: " + previewSizesString);
    }

    double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;

    // Remove sizes that are unsuitable
    Iterator<Camera.Size> it = supportedPreviewSizes.iterator();
    while (it.hasNext()) {
        Camera.Size supportedPreviewSize = it.next();
        int realWidth = supportedPreviewSize.width;
        int realHeight = supportedPreviewSize.height;
        if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {
            it.remove();
            continue;
        }

        boolean isCandidatePortrait = realWidth < realHeight;
        int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
        int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;

        double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight;
        double distortion = Math.abs(aspectRatio - screenAspectRatio);
        if (distortion > MAX_ASPECT_DISTORTION) {
            it.remove();
            continue;
        }

        if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
            Point exactPoint = new Point(realWidth, realHeight);
            Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
            return exactPoint;
        }
    }

    // If no exact match, use largest preview size. This was not a great
    // idea on older devices because
    // of the additional computation needed. We're likely to get here on
    // newer Android 4+ devices, where
    // the CPU is much more powerful.
    if (!supportedPreviewSizes.isEmpty()) {
        Camera.Size largestPreview = supportedPreviewSizes.get(0);
        Point largestSize = new Point(largestPreview.width, largestPreview.height);
        Log.i(TAG, "Using largest suitable preview size: " + largestSize);
        return largestSize;
    }

    // If there is nothing at all suitable, return current preview size
    Camera.Size defaultPreview = parameters.getPreviewSize();
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);

    return defaultSize;
}

From source file:Main.java

/**
 * Returns a comparator based on whether the order is ascending or not.
 * Has a dummy parameter to make sure generics can infer the type correctly.
 *///from  w w  w .j av a 2  s .c o  m
static <T extends Comparable<T>> Comparator<T> getComparator(boolean ascending, T dummy) {
    Comparator<T> comp;
    if (ascending) {
        comp = new Comparator<T>() {
            public int compare(T o1, T o2) {
                return o1.compareTo(o2);
            }
        };
    } else {
        comp = new Comparator<T>() {
            public int compare(T o1, T o2) {
                return o2.compareTo(o1);
            }
        };
    }
    return comp;
}