List of usage examples for java.util.function BiPredicate test
boolean test(T t, U u);
From source file:com.redhat.example.rules.unittest.CsvTestHelper.java
/** * Check the actual list with the expected list which are filtered by the parentRow info * @param actuals a list of actual result facts * @param filename the file name which contains expected values * @param clazz the class of actuals/*w w w .j a v a 2s .co m*/ * @param keyClass the key class if actuals are in a Map. it's null if actuals are not in a Map. * @param checkByIndex the flag to check actuals and expected values by same index * @param parentRow parent row to filter this child list * @return mapping array with actualIndex and expectIndex * @see CsvTestHelper#assertExpectCSV(List, String, boolean) */ public static <T> Integer[] assertExpectCSVwithParentRow(List<T> actuals, String filename, Class<?> clazz, Class<?> keyClass, boolean checkByIndex, String parentRow) { Integer[] ret = new Integer[actuals != null ? actuals.size() : 0]; int countActuals = 0; if (actuals != null) { for (Object act : actuals) { if (act != null) { countActuals++; } } } // in case of no actuals, or all actuals are null if (countActuals == 0) { // check if there are expected records int countExpectedRecords = 0; for (Map<String, Object> map : readCsvIntoMaps(filename, false)) { if (parentRow != null) { // count expected records where the parent# value equals parentRow if (parentRow.equals(map.get(RuleFactWatcher.Constants.parentRowKey))) { countExpectedRecords++; if (map.containsKey(RuleFactWatcher.Constants.valueAttributeStr) && !RuleFactWatcher.Constants.nullCheckStr .equals(map.get(RuleFactWatcher.Constants.valueAttributeStr))) { fail("actual is null but expect is not null"); } } } else { countExpectedRecords++; if (map.containsKey(RuleFactWatcher.Constants.valueAttributeStr) && !RuleFactWatcher.Constants.nullCheckStr .equals(map.get(RuleFactWatcher.Constants.valueAttributeStr))) { fail("actual is null but expect is not null"); } } } if (actuals == null) { if (countExpectedRecords > 0) { fail("No actuals records but there are expected records in " + filename + (parentRow != null ? (" " + RuleFactWatcher.Constants.parentRowKey + ":" + parentRow) : "")); } } else if (actuals.size() != countExpectedRecords) { fail("Record number mismatch of actuals and expected records in " + filename + (parentRow != null ? (" " + RuleFactWatcher.Constants.parentRowKey + ":" + parentRow) : "")); } return ret; } List<CsvColumnDef> columnDefs = null; try { columnDefs = readColumnDef(filename, clazz.getSimpleName()); } catch (Exception e1) { e1.printStackTrace(); fail("fail at readColumnDef(" + filename + ", " + clazz.getSimpleName() + ")"); } BiPredicate<ExpectedRecord, Object> predicate = createTestPredicate(columnDefs, clazz, keyClass); Map<String, Boolean> testSkipMap = createTestSkipMap(columnDefs); String factClassName = clazz.getSimpleName(); // the index of actual record int actualIndex = 0; // the flag the record is checked or not boolean foundExpect = false; // the index of expect record int expectIndex = 0; List<ExpectedRecord> expectedRecords = readExpectedCsv(filename, clazz, keyClass); // checkByIndex mode, remove other parents' records if (checkByIndex && parentRow != null) { for (int i = 0; i < expectedRecords.size();) { if (!parentRow.equals(expectedRecords.get(i).map.get(RuleFactWatcher.Constants.parentRowKey))) { expectedRecords.remove(i); } else { i++; } } } for (Object actual : actuals) { actualIndex++; foundExpect = false; expectIndex = 0; if (actual != null) { for (ExpectedRecord expect : expectedRecords) { expectIndex++; if (expect.fact == null) { if (checkByIndex && actualIndex == expectIndex) { fail("**assertExpectCSV** Expected record for the actual record (" + actual + ") " + factClassName + "[" + (actualIndex - 1) + "] " + "is null."); } // skip if it's null record in expected records } else if (parentRow != null && !parentRow.equals(expect.map.get(RuleFactWatcher.Constants.parentRowKey))) { // skip if the parent row is not same } else if ((checkByIndex && actualIndex == expectIndex) || (!checkByIndex && predicate.test(expect, actual))) { // index match OR the actual fact matches the expected record // if there is at least one expected record for the actual fact, set the flag true. foundExpect = true; expect.used = true; // update the mapping array of actualIndex and expectIndex; ret[actualIndex - 1] = expectIndex - 1; checkAttributes(actual, actualIndex, expect, clazz, keyClass, testSkipMap); } } if (foundExpect == false) { // an actual record is not in the expected records fail("**assertExpectCSV** Expected record for the actual record " + factClassName + "[" + (actualIndex - 1) + "] " + "does NOT exist."); } } else { if (checkByIndex) { if (expectedRecords.size() < actualIndex) { fail("**assertExpectCSV** Expected record for the actual record " + factClassName + "[" + (actualIndex - 1) + "] " + "does NOT exist."); } else if (expectedRecords.get(actualIndex - 1).fact != null) { fail("**assertExpectCSV** Expected record for the actual record (null) " + factClassName + "[" + (actualIndex - 1) + "] " + "is not null."); } else { foundExpect = true; } } else { logger.debug("**assertExpectCSV** Skipping actual record {}[{}] as it's null", factClassName, actualIndex - 1); } } } int expectedRecordNo = 0; boolean unused = false; for (ExpectedRecord record : expectedRecords) { if ((parentRow == null || parentRow.equals(record.map.get(RuleFactWatcher.Constants.parentRowKey))) && record.fact != null && !record.used) { logger.warn("**assertExpectCSV** Expected record {}[{}] is not used.", factClassName, expectedRecordNo); unused = true; } expectedRecordNo++; } if (unused == true) { fail("fail as there are unused expected record(s) of the " + factClassName + " class"); } // checkByIndex mode, no fails -> matching each index if (checkByIndex) { for (int i = 0; i < ret.length; i++) { ret[i] = i; } } return ret; }
From source file:gedi.util.ArrayUtils.java
public static <T> int unique(T[] a, int fromIndex, int toIndex, BiPredicate<T, T> equals) { if (toIndex <= fromIndex) return 0; int index = fromIndex; for (int i = index + 1; i < toIndex; i++) { if (!equals.test(a[i], a[index])) index++;/*from w ww .j a v a 2 s . c o m*/ a[index] = a[i]; } return index + 1; }