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);
}
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();
}
}
});
}
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;
}
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);
}
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;
}
}
}
Aggregations