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:edu.msu.cme.rdp.alignment.pairwise.PairwiseKNN.java

public static List<Neighbor> getKNN(Sequence query, List<Sequence> dbSeqs, AlignmentMode mode, int k,
        int wordSize, int prefilter) throws IOException {
    List<Neighbor> ret = new ArrayList();
    Neighbor n;//from  w  ww. j av a2 s  .  co  m
    Comparator c = new Comparator<Neighbor>() {
        public int compare(Neighbor t, Neighbor t1) {
            return t.alignment.getScore() - t1.alignment.getScore();
        }
    };

    SequenceType seqType = SeqUtils.guessSequenceType(query);
    ScoringMatrix matrix;
    KmerMatchCore kerMatchCore;
    if (seqType == SequenceType.Nucleotide) {
        matrix = ScoringMatrix.getDefaultNuclMatrix();
        kerMatchCore = new NuclSeqMatch(dbSeqs, wordSize);
    } else {
        matrix = ScoringMatrix.getDefaultProteinMatrix();
        kerMatchCore = new ProteinSeqMatch(dbSeqs, wordSize);
    }

    List<Sequence> refList;

    if (prefilter == 0) { // do not pre-filter the reference seqs
        refList = dbSeqs;
    } else {
        refList = new ArrayList<Sequence>();
        ArrayList<ProteinSeqMatch.BestMatch> topKMatches = kerMatchCore.findTopKMatch(query, prefilter);
        for (KmerMatchCore.BestMatch bestTarget : topKMatches) {
            refList.add(bestTarget.getBestMatch());
        }
    }

    for (Sequence dbSeq : refList) {
        n = new Neighbor();
        n.dbSeq = dbSeq;
        PairwiseAlignment fwd = PairwiseAligner.align(n.dbSeq.getSeqString(), query.getSeqString(), matrix,
                mode);
        if (seqType == SequenceType.Nucleotide) {
            PairwiseAlignment rc = PairwiseAligner.align(n.dbSeq.getSeqString(),
                    IUBUtilities.reverseComplement(query.getSeqString()), matrix, mode);

            if (rc.getScore() > fwd.getScore()) {
                n.alignment = rc;
                n.reverse = true;
            } else {
                n.alignment = fwd;
                n.reverse = false;
            }
        } else {
            n.alignment = fwd;
            n.reverse = false;
        }

        insert(n, ret, c, k);
    }

    return ret;
}

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

@Override
public String execute() throws Exception {
    List<PhotographerItem> photographerItemList = photographyService.getPhotographers();

    List<String> amFollowing = followService.amFollowing();

    List<String> followers = followService.followingMe();

    for (PhotographerItem item : photographerItemList) {
        final String twitterId = item.getTwitter();

        if (twitterId != null && !"".equals(twitterId)) {
            TwitterStatus status = new TwitterStatus(item.getName(), twitterId, amFollowing.contains(twitterId),
                    followers.contains(twitterId));

            statuses.add(status);/*from  w w w . j  a v a  2s.c om*/
        }
    }

    Collections.sort(statuses, new Comparator<TwitterStatus>() {
        public int compare(TwitterStatus o1, TwitterStatus o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });

    return SUCCESS;
}

From source file:com.taobao.ad.easyschedule.dao.datatrackinglog.impl.JPADataTrackingLogDAOImpl.java

@SuppressWarnings("unchecked")
public List<DatatrackingLogDO> getDataTrackingByGroupAndName(final DatatrackingLogDO datatrackingLog)
        throws DataAccessException {

    return (List<DatatrackingLogDO>) this.getJpaTemplate().execute(new JpaCallback() {
        @Override/* www . java  2s  .c o  m*/
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createQuery(
                    "select t from es_datatracking_log t where t.jobName=?1 and t.jobGroup=?2  and t.createTime>=?3 and t.createTime<=?4 order by t.createTime desc")
                    .setParameter(1, datatrackingLog.getJobName())
                    .setParameter(2, datatrackingLog.getJobGroup())
                    .setParameter(3, datatrackingLog.getQueryStartTime())
                    .setParameter(4, datatrackingLog.getQueryEndTime());
            List<DatatrackingLogDO> dataList = query.getResultList();
            if (dataList != null && !dataList.isEmpty()) {
                Collections.sort(dataList, new Comparator<DatatrackingLogDO>() {
                    @Override
                    public int compare(DatatrackingLogDO o1, DatatrackingLogDO o2) {
                        return o1.getCreateTime().compareTo(o2.getCreateTime());
                    }
                });
            }
            return dataList;
        }
    });

}

From source file:org.openmrs.web.controller.report.CohortListController.java

protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    List<Cohort> cohorts = new ArrayList<Cohort>();
    if (Context.isAuthenticated()) {
        cohorts = Context.getCohortService().getAllCohorts();
        Collections.sort(cohorts, new Comparator<Cohort>() {

            public int compare(Cohort a, Cohort b) {
                int temp = a.isVoided().compareTo(b.isVoided());
                if (temp == 0)
                    temp = a.getCohortId().compareTo(b.getCohortId());
                return temp;
            }/*from w  w w  . j av  a2 s .c  om*/
        });
    }
    return cohorts;
}

From source file:com.bigdata.dastor.service.RangeSliceResponseResolver.java

public List<Row> resolve(Collection<Message> responses) throws DigestMismatchException, IOException {
    CollatingIterator collator = new CollatingIterator(new Comparator<Pair<Row, InetAddress>>() {
        public int compare(Pair<Row, InetAddress> o1, Pair<Row, InetAddress> o2) {
            return partitioner.getToken(o1.left.key).compareTo(partitioner.getToken(o2.left.key));
        }//from w  w  w  . j  a v  a 2s  .com
    });

    int n = 0;
    for (Message response : responses) {
        RangeSliceReply reply = RangeSliceReply.read(response.getMessageBody());
        n = Math.max(n, reply.rows.size());
        collator.addIterator(new RowIterator(reply.rows.iterator(), response.getFrom()));
    }

    // for each row, compute the combination of all different versions seen, and repair incomplete versions
    ReducingIterator<Pair<Row, InetAddress>, Row> iter = new ReducingIterator<Pair<Row, InetAddress>, Row>(
            collator) {
        List<ColumnFamily> versions = new ArrayList<ColumnFamily>(sources.size());
        List<InetAddress> versionSources = new ArrayList<InetAddress>(sources.size());
        String key;

        @Override
        protected boolean isEqual(Pair<Row, InetAddress> o1, Pair<Row, InetAddress> o2) {
            return o1.left.key.equals(o2.left.key);
        }

        public void reduce(Pair<Row, InetAddress> current) {
            key = current.left.key;
            versions.add(current.left.cf);
            versionSources.add(current.right);
        }

        protected Row getReduced() {
            ColumnFamily resolved = ReadResponseResolver.resolveSuperset(versions);
            ReadResponseResolver.maybeScheduleRepairs(resolved, table, key, versions, versionSources);
            versions.clear();
            versionSources.clear();
            return new Row(key, resolved);
        }
    };

    List<Row> resolvedRows = new ArrayList<Row>(n);
    while (iter.hasNext())
        resolvedRows.add(iter.next());

    return resolvedRows;
}

From source file:org.opencredo.couchdb.inbound.CouchDbChangesPollingMessageSource.java

/**
 * Creates an instance with a custom CouchDbChangesOperations.
 *///ww  w  . j ava  2s .c o m
public CouchDbChangesPollingMessageSource(CouchDbChangesOperations couchDbChangesOperations) {
    this.couchDbChangesOperations = couchDbChangesOperations;
    this.toBeReceived = new PriorityBlockingQueue<ChangedDocument>(DEFAULT_INTERNAL_QUEUE_CAPACITY,
            new Comparator<ChangedDocument>() {
                public int compare(ChangedDocument doc1, ChangedDocument doc2) {
                    long diff = doc1.getSequence() - doc2.getSequence();
                    if (diff == 0) {
                        return 0;
                    } else {
                        return diff > 0 ? 1 : -1;
                    }
                }
            });
}

From source file:com.devnexus.ting.core.service.impl.DefaultTwitterService.java

/** {@inheritDoc} */
@Override//  w  w  w . j  a  v a2  s  .c o m
public Collection<TwitterMessage> getTwitterMessages() {
    SortedSet<TwitterMessage> twitterMessages = new TreeSet<TwitterMessage>(new Comparator<TwitterMessage>() {
        @Override
        public int compare(TwitterMessage twitterMessage1, TwitterMessage twitterMessage2) {
            return twitterMessage2.getCreatedAt().compareTo(twitterMessage1.getCreatedAt());
        }
    });

    twitterMessages.addAll(this.twitterMessages.asMap().values());
    return twitterMessages;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsPage.java

/**
 * Create a list of all known non-public PermissionSets.
 *//*  w  w  w  . jav  a 2  s.c  o m*/
protected List<PermissionSet> buildListOfSelectableRoles() {
    List<PermissionSet> list = new ArrayList<PermissionSet>();
    for (PermissionSet ps : userAccountsDao.getAllPermissionSets()) {
        if (!ps.isForPublic()) {
            list.add(ps);
        }
    }

    Collections.sort(list, new Comparator<PermissionSet>() {
        @Override
        public int compare(PermissionSet ps1, PermissionSet ps2) {
            return ps1.getUri().compareTo(ps2.getUri());
        }
    });
    return list;
}

From source file:eu.eubrazilcc.lvl.storage.security.PermissionHistory.java

public static @Nullable PermissionModification latestModification(final Set<PermissionModification> history,
        final String permission) {
    String permission2 = null;//from  w  w  w.ja v  a  2s .c om
    checkArgument(history != null, "Uninitialized or invalid history");
    checkArgument(isNotBlank(permission2 = trimToNull(permission)), "Uninitialized or invalid permission");
    final List<PermissionModification> sortedList = from(new Comparator<PermissionModification>() {
        @Override
        public int compare(final PermissionModification pm1, final PermissionModification pm2) {
            if (pm1 == pm2)
                return 0;
            return ComparisonChain.start().compare(pm1.getModificationDate(), pm2.getModificationDate())
                    .compare(pm1.getPermission(), pm2.getPermission())
                    .compare(pm1.getModificationType(), pm2.getModificationType()).result();
        }
    }).reverse().sortedCopy(history);
    PermissionModification latestModification = null;
    for (int i = 0; i < sortedList.size() && latestModification == null; i++) {
        final PermissionModification item = sortedList.get(i);
        if (permission2.equals(item.getPermission())) {
            latestModification = item;
        }
    }
    return latestModification;
}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps a local or remote MBeanServer's entire object tree for support
 * purposes. Nested arrays and CompositeData objects in MBean attribute
 * values are handled.//w  w w.ja  va  2  s  .c o  m
 * 
 * @param connection
 *            the server connection (or server itself)
 * @param out
 *            PrintWriter to write the output to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void dumpConnection(MBeanServerConnection connection, PrintWriter out) throws IOException {
    JmxDumpUtil.showStartBanner(out);

    // Get all the object names
    Set<ObjectName> objectNames = connection.queryNames(null, null);

    // Sort the names (don't assume ObjectName implements Comparable in JDK
    // 1.5)
    Set<ObjectName> newObjectNames = new TreeSet<ObjectName>(new Comparator<ObjectName>() {
        public int compare(ObjectName o1, ObjectName o2) {
            return o1.toString().compareTo(o2.toString());
        }
    });
    newObjectNames.addAll(objectNames);
    objectNames = newObjectNames;

    // Dump each MBean
    for (ObjectName objectName : objectNames) {
        try {
            printMBeanInfo(connection, objectName, out, null);
        } catch (JMException e) {
            // Sometimes beans can disappear while we are examining them
        }
    }
}