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:org.shareok.data.redis.RedisUtil.java

public static void sortJobList(List<RedisJob> jobList) {
    jobList.sort(new Comparator<RedisJob>() {
        @Override/* w ww .jav a2 s  .  c o m*/
        public int compare(RedisJob o1, RedisJob o2) {
            int result = 0;
            try {
                if (null != o1 && null != o2) {
                    result = Math.toIntExact(o1.getJobId() - o2.getJobId());
                }
            } catch (UnsupportedOperationException uoex) {
                logger.error("Cannot compare the tow job objects", uoex);
            }
            return result;
        }
    });
}

From source file:org.openmrs.module.accounting.web.controller.account.AccountFormController.java

@ModelAttribute("listParents")
public List<Account> registerListParents() {
    List<Account> listParents = new ArrayList<Account>(
            Context.getService(AccountingService.class).getListParrentAccount());
    Collections.sort(listParents, new Comparator<Account>() {

        public int compare(Account o1, Account o2) {
            return o1.getName().compareToIgnoreCase(o2.getName());
        }/*from  w ww.  j av  a2  s.c  o  m*/
    });
    return listParents;
}

From source file:eu.larkc.RDFPig.optimizers.strategies.SamplePruneOptimizationStrategy.java

@Override
public void prune(ArrayList<Set<TupleCostPair>> searchSpace, Executor executor, CostCalculator calculator) {

    // Only sample up to MAXSAMPLINGCYCLES steps
    int maxSamplingCycles = Configuration.getInstance().getPropertyInt(Configuration.MAXSAMPLINGCYCLES);
    if (searchSpace.size() > maxSamplingCycles) {
        return;/*from  w  w  w.  ja va  2s  .  c  o m*/
    }

    logger.info("Calling SamplePrune for cycle: " + searchSpace.size());

    // Sort last calculated expressions by cost
    Set<TupleCostPair> currentLevel = searchSpace.get(searchSpace.size() - 1);

    if (currentLevel.isEmpty()) {
        logger.warn("Empty current level, searchSpace sizes: ");
        for (Set<TupleCostPair> s : searchSpace) {
            logger.warn(Integer.toString(s.size()));
        }
        return;
    }

    List<TupleCostPair> sortedByCost = new ArrayList<TupleCostPair>(currentLevel);
    Collections.sort(sortedByCost, new Comparator<TupleCostPair>() {
        @Override
        public int compare(TupleCostPair o1, TupleCostPair o2) {
            if (o1.cost == o2.cost)
                return 0;
            else
                return o1.cost > o2.cost ? 1 : -1;
        }
    });

    // TODO adapt pruning to stddev?

    // Prune the bottom part
    double staticPruneRatio = Configuration.getInstance()
            .getPropertyDouble(Configuration.DYNAMICPROGRAMMINGSTATICPRUNEBEFORE);
    int staticPruneIndex = (int) Math.ceil(sortedByCost.size() / staticPruneRatio);
    if (staticPruneIndex == sortedByCost.size())
        staticPruneIndex--;
    sortedByCost = sortedByCost.subList(0, staticPruneIndex);

    // Sample the remaining ones
    Set<TupleExpr> candidates = new HashSet<TupleExpr>();
    for (TupleCostPair t : sortedByCost) {
        candidates.add(t.tuple);
    }
    try {
        executor.execute(candidates, Configuration.getInstance().getPropertyDouble(Configuration.SAMPLE_RATE));
    } catch (Exception e) {
        throw new RuntimeException("Could not sample", e);
    }

    // Update costs and remove the unsampled ones from the search space
    Iterator<TupleCostPair> it = currentLevel.iterator();
    while (it.hasNext()) {
        TupleCostPair c = it.next();
        if (candidates.contains(c.tuple))
            c.cost = calculator.getCost(c.tuple);
        else
            it.remove();
    }

    logger.info("Sample-Prune done for level " + (searchSpace.size() - 1) + " current level size = "
            + currentLevel.size());

}

From source file:com.chinamobile.bcbsp.bspcontroller.HRNQueue.java

@Override
public void resortQueue() {
    // TODO Auto-generated method stub
    Comparator<JobInProgress> comp = new Comparator<JobInProgress>() {
        @Override//from   w ww .  ja v a  2 s .co m
        public int compare(JobInProgress o1, JobInProgress o2) {
            int res;
            if (o1.getHRN() > o2.getHRN()) {
                res = -1;
            } else if (o1.getHRN() == o2.getHRN()) {
                res = 0;
            } else {
                res = 1;
            }
            return res;
        }
    };
    synchronized (queue) {
        try {
            resort_tmp.clear();
            int wait_count = queue.size();
            int i = 0;
            for (i = 0; i < wait_count; i++) {
                resort_tmp.add(queue.take());
            }
            Collections.sort(resort_tmp, comp);
            for (i = 0; i < wait_count; i++) {
                queue.put(resort_tmp.get(i));
            }
        } catch (Exception e) {
            //LOG.error("resort error: " + e.toString());
            throw new RuntimeException("resort error: ", e);
        }
    }
}

From source file:net.chrissearle.flickrvote.web.vote.ShowVoteChartAction.java

public String stats() throws Exception {
    initializeChallengeInfo();//  w  w w .  j ava2 s.  c om

    ChallengeItem challenge = photographyService.getChallengeImages(challengeInfo.getChallengeTag());

    images = new ArrayList<Image>(challenge.getImages().size());

    for (ImageItem image : challenge.getImages()) {
        final DisplayImage displayImage = new DisplayImage(image);

        images.add(displayImage);

        if (logger.isLoggable(Level.FINE)) {
            logger.info("Saw " + displayImage.toString());
        }
    }

    Collections.sort(images, new Comparator<Image>() {
        public int compare(Image o1, Image o2) {
            return o2.getVoteCount().compareTo(o1.getVoteCount());
        }
    });

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

    return SUCCESS;
}

From source file:gov.nih.nci.caintegrator.web.ajax.StudyDeploymentAjaxUpdater.java

/**
 * {@inheritDoc}/*from   w ww .  ja  v  a  2s.c  o m*/
 */
@Override
protected void initializeDynamicTable(DisplayableUserWorkspace workspace) {
    int counter = 0;
    List<StudyConfiguration> studyConfigurationList = new ArrayList<StudyConfiguration>();
    studyConfigurationList.addAll(workspace.getManagedStudies());
    Comparator<StudyConfiguration> nameComparator = new Comparator<StudyConfiguration>() {
        public int compare(StudyConfiguration configuration1, StudyConfiguration configuration2) {
            return configuration1.getStudy().getShortTitleText()
                    .compareToIgnoreCase(configuration2.getStudy().getShortTitleText());
        }
    };
    Collections.sort(studyConfigurationList, nameComparator);
    String username = workspace.getUserWorkspace().getUsername();
    for (StudyConfiguration studyConfiguration : studyConfigurationList) {
        getDwrUtil(username).addRows(STATUS_TABLE, createRow(studyConfiguration), retrieveRowOptions(counter));
        updateJobStatus(username, studyConfiguration);
        counter++;
    }
}

From source file:org.motechproject.server.omod.web.model.JSONLocationSerializer.java

public JSONLocationSerializer() {
    communityNameComparator = new Comparator<Community>() {
        public int compare(Community c1, Community c2) {
            return c1.getName().compareTo(c2.getName());
        }/*from   w w  w.java 2s. co m*/
    };
}

From source file:com.clustercontrol.accesscontrol.viewer.RoleSettingTreeContentProvider.java

/**
 * ??????/*  w  w w.  j a v a2  s .co m*/
 * 
 * @param parentElement 
 * @return ??
 * 
 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
 */
@Override
public Object[] getChildren(Object parentElement) {
    List<RoleTreeItem> ret = ((RoleTreeItem) parentElement).getChildren();
    Collections.sort(ret, new Comparator<RoleTreeItem>() {
        @Override
        public int compare(RoleTreeItem o1, RoleTreeItem o2) {
            if (o1.getData() instanceof RoleInfo && o2.getData() instanceof RoleInfo) {
                RoleInfo r1 = (RoleInfo) o1.getData();
                RoleInfo r2 = (RoleInfo) o2.getData();
                String s1 = r1.getRoleType();
                String s2 = r2.getRoleType();
                m_log.trace("s1=" + s1 + ", s2=" + s2);
                if (s1 == null || s2 == null) {
                    // ??
                    return r1.getRoleName().compareTo(r2.getRoleName());
                } else if (s1.equals(s2)) {
                    return r1.getRoleId().compareTo(r2.getRoleId());
                } else {
                    return r1.getRoleType().compareTo(r2.getRoleType()) * -1;
                }
            } else if (o1.getData() instanceof UserInfo && o2.getData() instanceof UserInfo) {
                UserInfo u1 = (UserInfo) o1.getData();
                UserInfo u2 = (UserInfo) o2.getData();
                m_log.trace("u1=" + u1 + ", u2=" + u2);
                return u1.getUserId().compareTo(u2.getUserId());
            }
            m_log.warn("unknown error");
            return -1; // ??????????
        }
    });
    return ret.toArray();
}

From source file:controllers.user.UserDetailApp.java

/**
 * ???????? ?//from w w  w.  ja v  a2 s  .com
 */
private static void sortJobExpList(List<JobExp> jobExps) {
    Collections.sort(jobExps, new Comparator<JobExp>() {
        public int compare(JobExp o1, JobExp o2) {
            if (o1 != null && o2 != null && o1.getEndYear() != null && o2.getEndYear() != null
                    && o1.getEndMonth() != null && o2.getEndMonth() != null) {
                if (StringUtils.isBlank(o2.getEndYear()) || StringUtils.equals(o2.getEndYear(), "")) {
                    return 1;
                }
                if (StringUtils.isBlank(o1.getEndYear()) || StringUtils.equals(o1.getEndYear(), "")) {
                    return -1;
                }

                int result = Integer.valueOf(o2.getEndYear()) - Integer.valueOf(o1.getEndYear());
                if (result == 0) {
                    if (StringUtils.isBlank(o2.getEndMonth())) {
                        return 1;
                    }
                    if (StringUtils.isBlank(o1.getEndMonth())) {
                        return -1;
                    }
                    int result2 = Integer.valueOf(o2.getEndMonth()) - Integer.valueOf(o1.getEndMonth());
                    if (result2 == 0) {
                        int result3 = Integer.valueOf(o2.getBeginYear()) - Integer.valueOf(o1.getBeginYear());
                        if (result3 == 0) {
                            return Integer.valueOf(o2.getBeginMonth()) - Integer.valueOf(o1.getBeginMonth());
                        } else {
                            return result3;
                        }
                    } else {
                        return result2;
                    }
                } else {
                    return result;
                }
            }
            return 0;
        }

    });
}

From source file:com.indeed.iupload.core.domain.IndexRepository.java

public List<Index> getIndexList() throws IOException {
    List<Index> indexList = new ArrayList<Index>();
    for (String name : getIndexNameList()) {
        Index index = getIndexObjectFromName(name);
        indexList.add(index);//from   ww w.  ja  v a  2  s .  c  om
    }
    Collections.sort(indexList, new Comparator<Index>() {
        @Override
        public int compare(Index o1, Index o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    return indexList;
}