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.FunctionColumn.java

public static Comparator<FunctionColumn> natural() {

    return new Comparator<FunctionColumn>() {

        @Override//w ww.j a v  a 2  s.  c om
        public int compare(final FunctionColumn o1, final FunctionColumn o2) {

            // by FUNCTION_CAT, FUNCTION_SCHEM, FUNCTION_NAME
            // and SPECIFIC_NAME.
            return new CompareToBuilder().append(o1.getFunctionCat(), o2.getFunctionCat())
                    .append(o1.getFunctionSchem(), o2.getFunctionSchem())
                    .append(o1.getFunctionName(), o2.getFunctionName())
                    .append(o1.getSpecificName(), o2.getSpecificName()).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();
        if (defaultSize == null) {
            throw new IllegalStateException("Parameters contained no preview size!");
        }//from   w  w w .j a  v  a  2s.com
        return new Point(defaultSize.width, defaultSize.height);
    }

    // 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();
    if (defaultPreview == null) {
        throw new IllegalStateException("Parameters contained no preview size!");
    }
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);
    return defaultSize;
}

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

public static Comparator<Attribute> natural() {

    return new Comparator<Attribute>() {

        @Override// w ww  .  java 2  s.  c o  m
        public int compare(final Attribute o1, final Attribute o2) {

            // TYPE_CAT, TYPE_SCHEM, TYPE_NAME and ORDINAL_POSITION.
            return new CompareToBuilder().append(o1.getTypeCat(), o2.getTypeCat())
                    .append(o1.getTypeSchem(), o2.getTypeSchem())
                    .append(o1.getOrdinalPosition(), o2.getOrdinalPosition()).build();
        }

    };
}

From source file:springfox.documentation.spi.service.contexts.Orderings.java

public static Comparator<Operation> positionComparator() {
    return new Comparator<Operation>() {
        @Override/*from ww  w  .  ja  va  2s  . co m*/
        public int compare(Operation first, Operation second) {
            return Ints.compare(first.getPosition(), second.getPosition());
        }
    };
}

From source file:Main.java

/**
 * Get the list of xml files in the bookmark export folder.
 * @return The list of xml files in the bookmark export folder.
 *//*from w ww .j  a  v a  2  s.c  o  m*/
public static List<String> getExportedBookmarksFileList() {
    List<String> result = new ArrayList<String>();

    File folder = Environment.getExternalStorageDirectory();

    if (folder != null) {

        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                if ((pathname.isFile()) && (pathname.getPath().toLowerCase(Locale.US).endsWith(".xml")
                        || pathname.getPath().toLowerCase(Locale.US).endsWith(".json"))) {
                    return true;
                }
                return false;
            }
        };

        File[] files = folder.listFiles(filter);

        for (File file : files) {
            result.add(file.getName());
        }
    }

    Collections.sort(result, new Comparator<String>() {

        @Override
        public int compare(String arg0, String arg1) {
            return arg1.compareTo(arg0);
        }
    });

    return result;
}

From source file:Main.java

/**
 * Get the list of xml files in the bookmark export folder.
 * @return The list of xml files in the bookmark export folder.
 *///from w ww. j a va  2 s.  co m
public static List<String> getExportedBookmarksFileList() {
    List<String> result = new ArrayList<String>();

    File folder = getBookmarksExportFolder();

    if (folder != null) {

        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                if ((pathname.isFile()) && (pathname.getPath().endsWith(".xml"))) {
                    return true;
                }
                return false;
            }
        };

        File[] files = folder.listFiles(filter);

        for (File file : files) {
            result.add(file.getName());
        }
    }

    Collections.sort(result, new Comparator<String>() {

        @Override
        public int compare(String arg0, String arg1) {
            return arg1.compareTo(arg0);
        }
    });

    return result;
}

From source file:Main.java

public static <E, V extends Comparable<? super V>, K> Map<K, V> sortByValueDesc(Map<K, V> map, int limit) {
    List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o2.getValue()).compareTo(o1.getValue());
        }/* w w  w.  ja  va2  s. c  o  m*/
    });

    Map<K, V> result = new LinkedHashMap<K, V>();
    int counter = 0;
    for (Map.Entry<K, V> entry : list) {
        if (limit > 0 && counter >= limit) {
            break;
        }
        result.put(entry.getKey(), entry.getValue());
        counter++;
    }
    return result;
}

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();
        if (defaultSize == null) {
            throw new IllegalStateException("Parameters contained no preview size!");
        }//w  w  w. j  av a2s.  com
        return new Point(defaultSize.width, defaultSize.height);
    }

    // Sort by size, descending
    List<Camera.Size> supportedPreviewSizes = new ArrayList<>(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();
    if (defaultPreview == null) {
        throw new IllegalStateException("Parameters contained no preview size!");
    }
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    //        Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);
    return defaultSize;
}

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

public static Comparator<Function> natural() {

    return new Comparator<Function>() {

        @Override//  ww w  .ja  va2s .  c  o  m
        public int compare(Function o1, Function o2) {

            // by FUNCTION_CAT, FUNCTION_SCHEM, FUNCTION_NAME
            // and SPECIFIC_NAME.
            return new CompareToBuilder().append(o1.getFunctionCat(), o2.getFunctionCat())
                    .append(o1.getFunctionSchem(), o2.getFunctionSchem())
                    .append(o1.getFunctionName(), o2.getFunctionName())
                    .append(o1.getSpecificName(), o2.getSpecificName()).build();
        }

    };
}

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

public static Comparator<ProcedureColumn> natural() {

    return new Comparator<ProcedureColumn>() {

        @Override//w  w w . j  a  va  2s .  c o  m
        public int compare(final ProcedureColumn o1, final ProcedureColumn 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();
        }

    };
}