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:Main.java

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

    List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(parameters.getSupportedPreviewSizes());

    Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
        @Override/*from   w ww. ja  v a 2  s  .c  o m*/
        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;
        }
    });

    Point bestSize = null;
    float screenAspectRatio = (float) screenResolution.x / (float) screenResolution.y;

    float diff = Float.POSITIVE_INFINITY;
    for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
        int realWidth = supportedPreviewSize.width;
        int realHeight = supportedPreviewSize.height;
        int pixels = realWidth * realHeight;
        if (pixels < MIN_PREVIEW_PIXELS || pixels > MAX_PREVIEW_PIXELS) {
            continue;
        }
        boolean isCandidatePortrait = realWidth < realHeight;
        int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
        int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;
        if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
            return new Point(realWidth, realHeight);
        }
        float aspectRatio = (float) maybeFlippedWidth / (float) maybeFlippedHeight;
        float newDiff = Math.abs(aspectRatio - screenAspectRatio);
        if (newDiff < diff) {
            bestSize = new Point(realWidth, realHeight);
            diff = newDiff;
        }
    }

    if (bestSize == null) {
        Camera.Size defaultSize = parameters.getPreviewSize();
        bestSize = new Point(defaultSize.width, defaultSize.height);
    }
    return bestSize;
}

From source file:uk.co.onehp.trickle.dao.HibernateBetDao.java

@Override
public Bet getNextBet() {
    List<Bet> bets = incompleteBets();
    Collections.sort(bets, new Comparator<Bet>() {

        @Override//www . ja v  a 2s . co  m
        public int compare(Bet o1, Bet o2) {
            int mostSeconds1 = DateUtil.getMostSeconds(o1.getUnprocessedTimings());
            int mostSeconds2 = DateUtil.getMostSeconds(o2.getUnprocessedTimings());
            return o1.getHorse().getRace().getStartTime().minusSeconds(mostSeconds1)
                    .compareTo(o2.getHorse().getRace().getStartTime().minusSeconds(mostSeconds2));
        }
    });
    return bets.size() > 0 ? bets.get(0) : null;
}

From source file:org.macula.cart.demo.service.impl.DemoApplicationServiceImpl.java

@Override
public List<DemoApplication> getAllApplications() {
    List<DemoApplication> result = new ArrayList<DemoApplication>(demoApplicationRepository.findAll());
    Collections.sort(result, new Comparator<DemoApplication>() {

        @Override//from ww w.  jav  a  2  s.  com
        public int compare(DemoApplication o1, DemoApplication o2) {
            int result = o1.getAppGroup().compareTo(o2.getAppGroup());
            return result != 0 ? result : o1.getAppId().compareToIgnoreCase(o2.getAppId());
        }
    });
    return result;
}

From source file:io.dyn.el.SpelExpression.java

public static Comparator<Expression> comparator(final EvaluationContext evaluationContext) {
    return new Comparator<Expression>() {
        @Override/* www.  jav  a  2s  .  com*/
        public int compare(Expression ex1, Expression ex2) {
            return new SpelExpression(ex1, evaluationContext).compareTo(ex2);
        }
    };
}

From source file:fr.cvlaminck.merging.impl.DefaultValueMergers.java

public DefaultValueMergers() {
    this.registeredMergerTypes = new TreeSet<Class<?>>(new Comparator<Class<?>>() {
        @Override//from w w w .j  ava2  s  .  c o  m
        public int compare(Class<?> c1, Class<?> c2) {
            if (c2.isAssignableFrom(c1))
                return -1;
            return 1;
        }
    });
    this.mergers = new HashMap<>();
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.clustering.entropy.MatrixExperiments.java

public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortByValue(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        @Override/*from  www .  ja v a2 s  .co m*/
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }
    });

    LinkedHashMap<K, V> result = new LinkedHashMap<>();
    for (Map.Entry<K, V> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

From source file:de.upb.wdqa.wdvd.processors.statistics.FrequencyUtils.java

public static List<Map.Entry<Comparable<?>, Long>> sortByFrequency(Frequency frequency) {
    Iterator<Map.Entry<Comparable<?>, Long>> iterator = frequency.entrySetIterator();

    List<Map.Entry<Comparable<?>, Long>> list = new ArrayList<Map.Entry<Comparable<?>, Long>>();

    while (iterator.hasNext()) {
        Map.Entry<Comparable<?>, Long> entry = iterator.next();

        list.add(entry);/*from  w ww  .  ja  v  a 2  s .c  om*/
    }

    Comparator<Map.Entry<Comparable<?>, Long>> comparator = new Comparator<Map.Entry<Comparable<?>, Long>>() {

        @Override
        public int compare(Map.Entry<Comparable<?>, Long> arg0, Map.Entry<Comparable<?>, Long> arg1) {
            if (arg0 == null || arg1 == null) {
                throw new NullPointerException();
            }

            return -Long.compare(arg0.getValue(), arg1.getValue());
        }
    };

    Collections.sort(list, comparator);

    return list;
}

From source file:com.quartercode.eventbridge.def.channel.DefaultChannel.java

/**
 * Creates a new default channel.//from   w w  w .  j av  a2s. c o m
 * 
 * @param interceptorType The type of interceptor that can be used by the channel.
 */
public DefaultChannel(Class<T> interceptorType) {

    this.interceptorType = interceptorType;

    interceptors = new TreeMap<>(new Comparator<Integer>() {

        @Override
        public int compare(Integer o1, Integer o2) {

            // Reverse the comparison in order to make the map ordering being large to small
            return Integer.compare(o2, o1);
        }

    });
}

From source file:io.yields.plugins.kpi.KPIReport.java

private KPIReport(String reference, Collection<ScoreResult> kpiScores) {
    this.reference = reference;

    List<ScoreResult> sortedKpiScores = new ArrayList<ScoreResult>(kpiScores);
    sort(sortedKpiScores, new Comparator<ScoreResult>() {
        @Override/*w ww. ja va  2s .co  m*/
        public int compare(ScoreResult scoreResult, ScoreResult other) {
            return new Double(other.getScore()).compareTo(scoreResult.getScore());
        }
    });

    this.kpiScores = sortedKpiScores;
}

From source file:org.springframework.data.examples.repository.ContactRepositoryImpl.java

@Override
public Page<Contact> findAll(Pageable pageable) {
    long first = pageable.getOffset() + 1;
    long last = pageable.getOffset() + pageable.getPageSize();
    SelectResults<Contact> results = template.find("select * from /Contact where id >= $1 and id <= $2", first,
            last);// w ww .  j  a v a  2s  . c o m

    List<Contact> contacts = results.asList();

    Collections.sort(contacts, new Comparator<Contact>() {

        @Override
        public int compare(Contact c0, Contact c1) {
            return (int) (c0.getId() - c1.getId());
        }

    });

    return results == null ? null : new PageImpl<Contact>(contacts, pageable, results.size());
}