Search in sources :

Example 31 with Comparator

use of java.util.Comparator in project jersey by jersey.

the class JsonMoxyTest method testJAXBListRepresentationJSONSet.

@Test
public void testJAXBListRepresentationJSONSet() throws Exception {
    final WebTarget target = target("JAXBListResourceJSON");
    Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    final Collection<JaxbBean> b;
    a = new HashSet<>(a);
    b = target.path("set").request().post(Entity.entity(new GenericEntity<Set<JaxbBean>>((Set<JaxbBean>) a) {
    }, "application/json"), new GenericType<Set<JaxbBean>>() {
    });
    final Comparator<JaxbBean> c = new Comparator<JaxbBean>() {

        @Override
        public int compare(final JaxbBean t, final JaxbBean t1) {
            return t.value.compareTo(t1.value);
        }
    };
    final TreeSet<JaxbBean> t1 = new TreeSet<>(c);
    final TreeSet<JaxbBean> t2 = new TreeSet<>(c);
    t1.addAll(a);
    t2.addAll(b);
    assertEquals(t1, t2);
}
Also used : GenericType(javax.ws.rs.core.GenericType) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) Comparator(java.util.Comparator) TreeSet(java.util.TreeSet) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 32 with Comparator

use of java.util.Comparator in project AndroidAsync by koush.

the class AsyncHttpServer method directory.

public void directory(String regex, final File directory, final boolean list) {
    assert directory.isDirectory();
    addAction("GET", regex, new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            String path = request.getMatcher().replaceAll("");
            File file = new File(directory, path);
            if (file.isDirectory() && list) {
                ArrayList<File> dirs = new ArrayList<File>();
                ArrayList<File> files = new ArrayList<File>();
                for (File f : file.listFiles()) {
                    if (f.isDirectory())
                        dirs.add(f);
                    else
                        files.add(f);
                }
                Comparator<File> c = new Comparator<File>() {

                    @Override
                    public int compare(File lhs, File rhs) {
                        return lhs.getName().compareTo(rhs.getName());
                    }
                };
                Collections.sort(dirs, c);
                Collections.sort(files, c);
                files.addAll(0, dirs);
                return;
            }
            if (!file.isFile()) {
                response.code(404);
                response.end();
                return;
            }
            try {
                FileInputStream is = new FileInputStream(file);
                response.code(200);
                Util.pump(is, response, new CompletedCallback() {

                    @Override
                    public void onCompleted(Exception ex) {
                        response.end();
                    }
                });
            } catch (FileNotFoundException ex) {
                response.code(404);
                response.end();
            }
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Comparator(java.util.Comparator) File(java.io.File)

Example 33 with Comparator

use of java.util.Comparator in project pinot by linkedin.

the class StarTreeJsonNode method build.

private int build(StarTreeIndexNodeInterf indexNode, StarTreeJsonNode json) {
    Iterator<? extends StarTreeIndexNodeInterf> childrenIterator = indexNode.getChildrenIterator();
    if (!childrenIterator.hasNext()) {
        return 0;
    }
    int childDimensionId = indexNode.getChildDimensionName();
    String childDimensionName = dimensionNameToIndexMap.inverse().get(childDimensionId);
    Dictionary dictionary = dictionaries.get(childDimensionName);
    int totalChildNodes = indexNode.getNumChildren();
    Comparator<Pair<String, Integer>> comparator = new Comparator<Pair<String, Integer>>() {

        @Override
        public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
            return -1 * Integer.compare(o1.getRight(), o2.getRight());
        }
    };
    MinMaxPriorityQueue<Pair<String, Integer>> queue = MinMaxPriorityQueue.orderedBy(comparator).maximumSize(MAX_CHILDREN).create();
    StarTreeJsonNode allNode = null;
    while (childrenIterator.hasNext()) {
        StarTreeIndexNodeInterf childIndexNode = childrenIterator.next();
        int childDimensionValueId = childIndexNode.getDimensionValue();
        String childDimensionValue = "ALL";
        if (childDimensionValueId != StarTreeIndexNodeInterf.ALL) {
            childDimensionValue = dictionary.get(childDimensionValueId).toString();
        }
        StarTreeJsonNode childJson = new StarTreeJsonNode(childDimensionValue);
        totalChildNodes += build(childIndexNode, childJson);
        if (childDimensionValueId != StarTreeIndexNodeInterf.ALL) {
            json.addChild(childJson);
            queue.add(ImmutablePair.of(childDimensionValue, totalChildNodes));
        } else {
            allNode = childJson;
        }
    }
    //put ALL node at the end
    if (allNode != null) {
        json.addChild(allNode);
    }
    if (totalChildNodes > MAX_CHILDREN) {
        Iterator<Pair<String, Integer>> qIterator = queue.iterator();
        Set<String> topKDimensions = new HashSet<>();
        topKDimensions.add("ALL");
        while (qIterator.hasNext()) {
            topKDimensions.add(qIterator.next().getKey());
        }
        Iterator<StarTreeJsonNode> iterator = json.getChildren().iterator();
        while (iterator.hasNext()) {
            StarTreeJsonNode next = iterator.next();
            if (!topKDimensions.contains(next.getName())) {
                iterator.remove();
            }
        }
    }
    return totalChildNodes;
}
Also used : Dictionary(com.linkedin.pinot.core.segment.index.readers.Dictionary) StarTreeIndexNodeInterf(com.linkedin.pinot.core.startree.StarTreeIndexNodeInterf) Comparator(java.util.Comparator) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) HashSet(java.util.HashSet)

Example 34 with Comparator

use of java.util.Comparator in project k-9 by k9mail.

the class MessageListFragment method getComparator.

/**
     * @return The comparator to use to display messages in an ordered
     *         fashion. Never {@code null}.
     */
private Comparator<Cursor> getComparator() {
    final List<Comparator<Cursor>> chain = new ArrayList<>(3);
    // Add the specified comparator
    final Comparator<Cursor> comparator = SORT_COMPARATORS.get(sortType);
    if (sortAscending) {
        chain.add(comparator);
    } else {
        chain.add(new ReverseComparator<>(comparator));
    }
    // Add the date comparator if not already specified
    if (sortType != SortType.SORT_DATE && sortType != SortType.SORT_ARRIVAL) {
        final Comparator<Cursor> dateComparator = SORT_COMPARATORS.get(SortType.SORT_DATE);
        if (sortDateAscending) {
            chain.add(dateComparator);
        } else {
            chain.add(new ReverseComparator<>(dateComparator));
        }
    }
    // Add the id comparator
    chain.add(new ReverseIdComparator());
    // Build the comparator chain
    return new ComparatorChain<>(chain);
}
Also used : ComparatorChain(com.fsck.k9.fragment.MessageListFragmentComparators.ComparatorChain) ArrayList(java.util.ArrayList) ReverseIdComparator(com.fsck.k9.fragment.MessageListFragmentComparators.ReverseIdComparator) Cursor(android.database.Cursor) UnreadComparator(com.fsck.k9.fragment.MessageListFragmentComparators.UnreadComparator) AttachmentComparator(com.fsck.k9.fragment.MessageListFragmentComparators.AttachmentComparator) SenderComparator(com.fsck.k9.fragment.MessageListFragmentComparators.SenderComparator) DateComparator(com.fsck.k9.fragment.MessageListFragmentComparators.DateComparator) ReverseIdComparator(com.fsck.k9.fragment.MessageListFragmentComparators.ReverseIdComparator) ReverseComparator(com.fsck.k9.fragment.MessageListFragmentComparators.ReverseComparator) SubjectComparator(com.fsck.k9.fragment.MessageListFragmentComparators.SubjectComparator) ArrivalComparator(com.fsck.k9.fragment.MessageListFragmentComparators.ArrivalComparator) FlaggedComparator(com.fsck.k9.fragment.MessageListFragmentComparators.FlaggedComparator) Comparator(java.util.Comparator)

Example 35 with Comparator

use of java.util.Comparator in project killbill by killbill.

the class CBADao method useExistingCBAFromTransaction.

// Distribute account CBA across all COMMITTED unpaid invoices
private void useExistingCBAFromTransaction(final BigDecimal accountCBA, final List<Tag> invoicesTags, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalCallContext context) throws InvoiceApiException, EntityPersistenceException {
    if (accountCBA.compareTo(BigDecimal.ZERO) <= 0) {
        return;
    }
    // PERF: Computing the invoice balance is difficult to do in the DB, so we effectively need to retrieve all invoices on the account and filter the unpaid ones in memory.
    // This should be infrequent though because of the account CBA check above.
    final List<InvoiceModelDao> allInvoices = invoiceDaoHelper.getAllInvoicesByAccountFromTransaction(invoicesTags, entitySqlDaoWrapperFactory, context);
    final List<InvoiceModelDao> unpaidInvoices = invoiceDaoHelper.getUnpaidInvoicesByAccountFromTransaction(allInvoices, null);
    // We order the same os BillingStateCalculator-- should really share the comparator
    final List<InvoiceModelDao> orderedUnpaidInvoices = Ordering.from(new Comparator<InvoiceModelDao>() {

        @Override
        public int compare(final InvoiceModelDao i1, final InvoiceModelDao i2) {
            return i1.getInvoiceDate().compareTo(i2.getInvoiceDate());
        }
    }).immutableSortedCopy(unpaidInvoices);
    BigDecimal remainingAccountCBA = accountCBA;
    for (final InvoiceModelDao unpaidInvoice : orderedUnpaidInvoices) {
        remainingAccountCBA = computeCBAComplexityAndCreateCBAItem(remainingAccountCBA, unpaidInvoice, entitySqlDaoWrapperFactory, context);
        if (remainingAccountCBA.compareTo(BigDecimal.ZERO) <= 0) {
            break;
        }
    }
}
Also used : BigDecimal(java.math.BigDecimal) Comparator(java.util.Comparator)

Aggregations

Comparator (java.util.Comparator)322 ArrayList (java.util.ArrayList)123 List (java.util.List)58 Test (org.junit.Test)58 HashMap (java.util.HashMap)50 IOException (java.io.IOException)36 Map (java.util.Map)35 File (java.io.File)24 HashSet (java.util.HashSet)23 TreeSet (java.util.TreeSet)20 Set (java.util.Set)18 Iterator (java.util.Iterator)15 Method (java.lang.reflect.Method)14 Collections (java.util.Collections)14 Date (java.util.Date)14 TreeMap (java.util.TreeMap)14 ArrayMap (android.util.ArrayMap)12 Collection (java.util.Collection)11 LinkedList (java.util.LinkedList)11 SimpleDateFormat (java.text.SimpleDateFormat)10