use of java.util.TreeSet in project pinot by linkedin.
the class PinotDataCustomBitSetTest method testFindNthBitSetRandom.
@Test
public void testFindNthBitSetRandom() {
final int ITERATIONS = 100000;
final int LENGTH = 256;
final int MAX_BITS_ON = 32;
Random random = new Random();
for (int i = 0; i < ITERATIONS; i++) {
PinotDataCustomBitSet bitset = PinotDataCustomBitSet.withBitLength(LENGTH);
TreeSet<Integer> bitsOn = new TreeSet<Integer>();
int bitsToTurnOn = random.nextInt(MAX_BITS_ON);
while (bitsOn.size() < bitsToTurnOn) {
bitsOn.add(random.nextInt(LENGTH));
}
TreeSet<Integer> bitsOnCopy = new TreeSet<Integer>(bitsOn);
for (Integer indexOfBitToTurnOn : bitsOn) {
bitset.setBit(indexOfBitToTurnOn);
}
int startSearchIndex = random.nextInt(LENGTH);
// Discard all bits before or at the search index
Iterator<Integer> bitsOnIterator = bitsOn.iterator();
while (bitsOnIterator.hasNext()) {
Integer next = bitsOnIterator.next();
if (next <= startSearchIndex) {
bitsOnIterator.remove();
}
}
// Discard all bits set on before the search ahead limit
int nthBitToFind = random.nextInt(MAX_BITS_ON / 2) + 1;
for (int j = 0; j < nthBitToFind - 1 && !bitsOn.isEmpty(); j++) {
bitsOn.pollFirst();
}
// Check the result against the expected index
int expectedIndex;
if (bitsOn.isEmpty()) {
expectedIndex = -1;
} else {
expectedIndex = bitsOn.pollFirst();
}
long nthBitSetAfter = bitset.findNthBitSetAfter(startSearchIndex, nthBitToFind);
if (nthBitSetAfter != expectedIndex) {
// System.out.println("Bits set " + bitsOnCopy + ", searching for " + nthBitToFind + "th bit from " + startSearchIndex);
nthBitSetAfter = bitset.findNthBitSetAfter(startSearchIndex, nthBitToFind);
}
Assert.assertEquals(nthBitSetAfter, expectedIndex, "Bits set " + bitsOnCopy + ", searching for " + nthBitToFind + "th bit from " + startSearchIndex);
bitset.close();
}
}
use of java.util.TreeSet in project pinot by linkedin.
the class OrOperatorTest method testComplex.
@Test
public void testComplex() {
int[] list1 = new int[] { 2, 3, 6, 10, 15, 16, 28 };
int[] list2 = new int[] { 3, 6, 8, 20, 28 };
int[] list3 = new int[] { 1, 2, 3, 6, 30 };
TreeSet<Integer> set = new TreeSet<Integer>();
set.addAll(Lists.newArrayList(ArrayUtils.toObject(list1)));
set.addAll(Lists.newArrayList(ArrayUtils.toObject(list2)));
set.addAll(Lists.newArrayList(ArrayUtils.toObject(list3)));
Iterator<Integer> expectedIterator = set.iterator();
List<BaseFilterOperator> operators = new ArrayList<>();
operators.add(makeFilterOperator(list1));
operators.add(makeFilterOperator(list2));
final OrOperator orOperator1 = new OrOperator(operators);
List<BaseFilterOperator> operators1 = new ArrayList<>();
operators1.add(orOperator1);
operators1.add(makeFilterOperator(list3));
final OrOperator orOperator = new OrOperator(operators1);
orOperator.open();
BaseFilterBlock block;
while ((block = orOperator.getNextBlock()) != null) {
final BlockDocIdSet blockDocIdSet = block.getBlockDocIdSet();
final BlockDocIdIterator iterator = blockDocIdSet.iterator();
int docId;
while ((docId = iterator.next()) != Constants.EOF) {
// System.out.println(docId);
Assert.assertEquals(expectedIterator.next().intValue(), docId);
}
}
orOperator.close();
}
use of java.util.TreeSet in project head by mifos.
the class ImportTransactionsServiceFacadeWebTier method undoFullImport.
@Override
public void undoFullImport(String importTransactionsFileName) {
MifosUser mifosUser = (MifosUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserContext userContext = new UserContextFactory().create(mifosUser);
TreeSet<String> accountsWithAdjustedPayments = new TreeSet<String>();
try {
ImportedFilesEntity filesEntity = this.importedFilesService.getImportedFileByName(importTransactionsFileName);
List<AccountTrxnEntity> trxUndo = new ArrayList<AccountTrxnEntity>(filesEntity.getImportedTrxn());
List<ImportedAccPaymentDto> accPaymentList = new ArrayList<ImportedAccPaymentDto>();
for (AccountTrxnEntity trxn : trxUndo) {
try {
validateForAdjustedPayments(trxn, accountsWithAdjustedPayments);
accPaymentList.add(new ImportedAccPaymentDto(trxn.getAccount().getGlobalAccountNum(), trxn.getAccountPayment().getPaymentId()));
} catch (BusinessRuleException e) {
//nothing to do
}
}
for (ImportedAccPaymentDto accDto : accPaymentList) {
try {
this.accountServiceFacade.applyHistoricalAdjustment(accDto.getGlobalNum(), accDto.getPaymentId(), IMPORT_UNDONE, userContext.getId(), null);
} catch (MifosRuntimeException e) {
// TODO: validation will be added with MIFOS-5779
}
}
this.importedFilesService.saveImportedFileName(filesEntity.getFileName(), filesEntity.getSubmittedBy(), null, Boolean.TRUE, filesEntity.getUndoable());
} catch (Exception e) {
throw new MifosRuntimeException(e);
}
}
use of java.util.TreeSet in project head by mifos.
the class OfficeListTag method getBranchOffices.
void getBranchOffices(XmlBuilder html, List<OfficeHierarchyDto> officeList, Locale preferredUserLocale, String loggedInOfficeSearchId, String branchName) {
html.singleTag("br");
html.startTag("span", "class", "fontnormalBold");
html.text(branchName);
html.endTag("span");
html.singleTag("br");
if (officeList == null) {
html.startTag("span", "class", "fontnormal");
html.text(MessageLookup.getLocalizedMessage("Office.labelNo"));
html.text(" ");
html.text(branchName.toLowerCase());
html.text(" ");
html.text(MessageLookup.getLocalizedMessage("Office.labelPresent"));
html.endTag("span");
} else {
for (int i = 0; i < officeList.size(); i++) {
OfficeHierarchyDto officeParent = officeList.get(i);
Set<OfficeHierarchyDto> branchList = new TreeSet<OfficeHierarchyDto>();
for (OfficeHierarchyDto dataScopeBranch : officeParent.getChildren()) {
if (dataScopeBranch.getSearchId().startsWith(loggedInOfficeSearchId) && dataScopeBranch.isActive()) {
branchList.add(dataScopeBranch);
}
}
if (branchList.size() > 0) {
if (i > 0) {
html.singleTag("br");
}
html.startTag("span", "class", "fontnormal");
html.text(officeParent.getOfficeName());
html.endTag("span");
html.startTag("table", "width", "90%", "border", "0", "cellspacing", "0", "cellpadding", "0");
for (OfficeHierarchyDto office : branchList) {
html.startTag("tr", "class", "fontnormal");
bullet(html);
html.startTag("td", "width", "99%");
html.append(getLink(office.getOfficeId(), office.getOfficeName()));
html.endTag("td");
html.endTag("tr");
}
html.endTag("table");
}
}
}
}
use of java.util.TreeSet in project morphia by mongodb.
the class MappingValidator method validate.
/**
* Validates a List of MappedClasses
*
* @param classes the MappedClasses to validate
* @param mapper the Mapper to use for validation
*/
public void validate(final Mapper mapper, final List<MappedClass> classes) {
final Set<ConstraintViolation> ve = new TreeSet<ConstraintViolation>(new Comparator<ConstraintViolation>() {
@Override
public int compare(final ConstraintViolation o1, final ConstraintViolation o2) {
return o1.getLevel().ordinal() > o2.getLevel().ordinal() ? -1 : 1;
}
});
final List<ClassConstraint> rules = getConstraints();
for (final MappedClass c : classes) {
for (final ClassConstraint v : rules) {
v.check(mapper, c, ve);
}
}
if (!ve.isEmpty()) {
final ConstraintViolation worst = ve.iterator().next();
final Level maxLevel = worst.getLevel();
if (maxLevel.ordinal() >= Level.FATAL.ordinal()) {
throw new ConstraintViolationException(ve);
}
// sort by class to make it more readable
final List<LogLine> l = new ArrayList<LogLine>();
for (final ConstraintViolation v : ve) {
l.add(new LogLine(v));
}
sort(l);
for (final LogLine line : l) {
line.log(LOG);
}
}
}
Aggregations