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.dtolabs.utils.Mapper.java

/**
 * see {@link #comparator(Mapper)}//from ww w. j  a v a  2 s  .  c  o m
 * @param reverse if true, reverse the order of comparison
 * @return
 */
public static Comparator comparator(final Mapper mapper, final boolean reverse) {
    return new Comparator() {
        public int compare(Object o, Object o1) {
            return ((Comparable) mapper.map(reverse ? o1 : o)).compareTo(mapper.map(reverse ? o : o1));
        }
    };
}

From source file:it.reply.orchestrator.controller.ControllerTestUtils.java

public static List<Resource> createResources(Deployment deployment, int total, boolean sorted) {
    List<Resource> resources = Lists.newArrayList();
    for (int i = 0; i < total; ++i) {
        resources.add(createResource(deployment));
    }//from w ww  .ja v  a2s.  c  o m

    if (sorted) {
        resources.stream().sorted(new Comparator<Resource>() {

            @Override
            public int compare(Resource o1, Resource o2) {
                return o1.getCreated().compareTo(o2.getCreated());
            }
        }).collect(Collectors.toList());
    }

    return resources;
}

From source file:com.ms.app.web.comset.controller.ComsetController.java

@ControllerAction
public WebResult show(Map<String, Object> model, final String sortcount, final String clear) throws Exception {
    if ("true".equals(clear)) {
        ResourceTools.clear();/*  w w w  . j  a va 2 s . co m*/
    }
    List<UnitInfo> list = ResourceTools.getRecordUnitInfo();

    Collections.sort(list, new Comparator<UnitInfo>() {

        public int compare(UnitInfo o1, UnitInfo o2) {
            long v1 = o1.getCount();
            long v2 = o2.getCount();
            if ("false".equals(sortcount)) {
                v1 = o1.getPeriod() / o1.getCount();
                v2 = o2.getPeriod() / o2.getCount();
            }
            if (v1 < v2) {
                return 1;
            } else if (v1 > v2) {
                return -1;
            } else {
                return 0;
            }
        }
    });
    if (list.size() > 100) {
        List<UnitInfo> newlist = new ArrayList<UnitInfo>(100);
        for (int i = 0; i < 100; i++) {
            newlist.add(list.get(i));
        }
        model.put("comsetList", newlist);
    } else {
        model.put("comsetList", list);
    }
    model.put("sort_type", "false".equals(sortcount) ? 0 : 1);
    return toView("/comset/show.htm");
}

From source file:org.jasig.portlet.contacts.control.PortletViewController.java

@ModelAttribute("domains")
public Set<ContactDomain> getDomains(PortletPreferences prefs) {
    log.debug("finding Domains to return");
    final List<String> domainActive = Arrays.asList(prefs.getValues("domainsActive", new String[0]));

    String[] defaultOn = prefs.getValues("defaultOn", new String[0]);
    String[] userOn = prefs.getValues("domainOn", new String[0]);
    String[] userOff = prefs.getValues("domainOff", new String[0]);

    Set<String> domains = new HashSet<String>();
    domains.addAll(Arrays.asList(defaultOn));
    domains.addAll(Arrays.asList(userOn));
    domains.removeAll(Arrays.asList(userOff));
    domains.retainAll(domainActive);/*from  w  ww. j a v  a 2s  .  c  om*/

    Set<ContactDomain> activeDomains = new TreeSet<ContactDomain>(new Comparator<ContactDomain>() {

        @Override
        public int compare(ContactDomain o1, ContactDomain o2) {
            int index1 = domainActive.indexOf(o1.getName());
            int index2 = domainActive.indexOf(o2.getName());
            return index1 - index2;
        }
    });
    for (ContactDomain domain : contactDomains) {
        if (domains.contains(domain.getName())) {
            activeDomains.add(domain);
        }
    }
    log.debug("returning " + activeDomains.size() + "domains");
    return activeDomains;
}

From source file:de.codesourcery.eve.skills.ui.components.AbstractComponent.java

protected static <T> List<T> sortUsingLabelProvider(Collection<T> data, final ILabelProvider<T> provider) {

    final Comparator<T> comparator = new Comparator<T>() {

        @Override/*from   w  w w. jav a 2  s. co  m*/
        public int compare(T o1, T o2) {
            return provider.getLabel(o1).compareTo(provider.getLabel(o2));
        }
    };

    if (data instanceof List) {
        Collections.sort((List<T>) data, comparator);
        return (List<T>) data;
    }

    final List<T> target = new ArrayList<T>(data);
    Collections.sort(target, comparator);
    return target;
}

From source file:net.chrissearle.flickrvote.web.stats.ShowPhotographerChartAction.java

public String stats() throws Exception {
    initializePhotographerInfo();//w w w.j  a  v a 2 s .c om

    this.challenges = new ArrayList<DisplayChallengeSummary>();

    for (ChallengeSummary challengeSummary : challengeService.getChallengesByType(ChallengeType.CLOSED)) {
        challenges.add(new DisplayChallengeSummary(challengeSummary));
    }

    Collections.sort(this.challenges, new Comparator<Challenge>() {
        public int compare(Challenge o1, Challenge o2) {
            return o2.getChallengeTag().compareTo(o1.getChallengeTag());
        }
    });

    if (this.small) {
        this.height = (long) (18 * challenges.size()) + 20;
    } else {
        this.height = (long) (30 * challenges.size()) + 140;
    }

    this.images = new ArrayList<DisplayImage>();

    final Map<String, DisplayImage> map = new HashMap<String, DisplayImage>();

    for (ImageItem image : photographyService.getImagesForPhotographer(photographerInfo.getPhotographerId())) {
        map.put(image.getChallenge().getTag(), new DisplayImage(image));
    }

    for (Challenge challenge : this.challenges) {
        if (map.containsKey(challenge.getChallengeTag())) {
            images.add(map.get(challenge.getChallengeTag()));
        } else {
            images.add(null);
        }
    }

    return SUCCESS;
}

From source file:net.chrissearle.flickrvote.web.admin.SearchTagCheckAction.java

@Override
public String execute() throws Exception {
    challenge = new DisplayChallengeSummary(challengeService.getChallengeSummary(tag));

    Set<ImageItemStatus> searchIssues = statusCheckService.checkSearch(tag);

    takenDateIssues = new HashSet<DisplayImage>();
    multipleImageIssues = new ArrayList<DisplayImage>();

    for (ImageItemStatus status : searchIssues) {
        if (status.getStatus() == Status.TAKEN_DATE_TOO_EARLY) {
            for (ImageItem image : status.getImages()) {
                takenDateIssues.add(new DisplayImage(image));
            }//from   w  w  w.ja  v a2s.c  o  m
        }
        if (status.getStatus() == Status.MULTIPLE_IMAGE_BY_SAME_PHOTOGRAPHER) {
            for (ImageItem image : status.getImages()) {
                multipleImageIssues.add(new DisplayImage(image));
            }
        }
    }

    Collections.sort(multipleImageIssues, new Comparator<DisplayImage>() {
        public int compare(DisplayImage o1, DisplayImage o2) {
            return o1.getPhotographerId().compareTo(o2.getPhotographerId());
        }
    });

    return SUCCESS;
}

From source file:io.smartspaces.network.client.internal.ros.RosNetworkInformationClient.java

/**
 * Construct a new client.//from  w  ww  .j  a  v  a2s  . com
 */
public RosNetworkInformationClient() {
    lowerCaseStringComparator = new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
        }
    };

    networkTopicInformationComparator = new Comparator<NetworkTopicInformation>() {

        @Override
        public int compare(NetworkTopicInformation o1, NetworkTopicInformation o2) {
            return o1.getTopicName().compareToIgnoreCase(o2.getTopicName());
        }
    };

    networkNodeInformationComparator = new Comparator<NetworkNodeInformation>() {

        @Override
        public int compare(NetworkNodeInformation o1, NetworkNodeInformation o2) {
            return o1.getNodeName().compareToIgnoreCase(o2.getNodeName());
        }
    };
}

From source file:com.agileapes.couteau.context.spring.event.SpringEventTranslator.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    schemes.addAll(applicationContext.getBeansOfType(TranslationScheme.class, false, true).values());
    Collections.sort(schemes, new OrderedBeanComparator());
    Collections.sort(schemes, new Comparator<TranslationScheme>() {
        @Override/*from w  w  w .j  a  va 2s . co  m*/
        public int compare(TranslationScheme o1, TranslationScheme o2) {
            final Integer first = o1 instanceof Ordered ? ((Ordered) o1).getOrder() : 0;
            final Integer second = o2 instanceof Ordered ? ((Ordered) o2).getOrder() : 0;
            return first.compareTo(second);
        }
    });
}

From source file:captureplugin.drivers.defaultdriver.configpanels.ChannelTableModel.java

/**
 * Updates the List of Channels/*  www  . j a v a  2s . c  o m*/
 */
private void updateChannels() {
    PluginManager pl = Plugin.getPluginManager();
    if (pl == null) {
        return;
    }
    TreeMap<Channel, String> channels = mData.getChannels();

    final Channel[] channelArray = pl.getSubscribedChannels();
    for (Channel channel : channelArray) {
        if (!channels.containsKey(channel)) {
            channels.put(channel, "");
        }
    }
    mData.setChannels(channels);
    mChannelRows = new ArrayList<Channel>(mData.getChannels().keySet());

    Collections.sort(mChannelRows, new Comparator<Channel>() {
        public int compare(Channel a, Channel b) {
            return ArrayUtils.indexOf(channelArray, a) - ArrayUtils.indexOf(channelArray, b);
        }
    });
}