List of usage examples for java.util Comparator compare
int compare(T o1, T o2);
From source file:org.openfaces.component.table.impl.TableDataModel.java
private void sortRows(List<RowInfo> extractedRows) { if (table == null) return;//w ww . j a v a2 s . c o m final Comparator<Object> rowDataComparator = table.createRowDataComparator(groupingRules, sortingRules); Comparator<RowInfo> rowInfoComparator = new Comparator<RowInfo>() { public int compare(RowInfo rowInfo1, RowInfo rowInfo2) { return rowDataComparator.compare(rowInfo1.getRowData(), rowInfo2.getRowData()); } }; if (rowDataComparator != null) Collections.sort(extractedRows, rowInfoComparator); }
From source file:org.kuali.rice.krad.util.BeanPropertyComparator.java
/** * Compare two JavaBeans by the properties given to the constructor. * //from w ww. j a v a2 s . co m * @param o1 Object The first bean to get data from to compare against * @param o2 Object The second bean to get data from to compare * @return int negative or positive based on order */ public int compare(Object o1, Object o2) { int compared = 0; try { for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { String currentProperty = i.next().toString(); // choose appropriate comparator Comparator currentComparator = null; try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); Class propertyClass = propertyDescriptor.getPropertyType(); if (propertyClass.equals(String.class)) { currentComparator = this.stringComparator; } else if (TypeUtils.isBooleanClass(propertyClass)) { currentComparator = this.booleanComparator; } else { currentComparator = this.genericComparator; } } catch (NullPointerException e) { throw new BeanComparisonException( "unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); } // compare the values Object value1 = PropertyUtils.getProperty(o1, currentProperty); Object value2 = PropertyUtils.getProperty(o2, currentProperty); /* Fix for KULRICE-5170 : BeanPropertyComparator throws exception when a null value is found in sortable non-string data type column */ if (value1 == null && value2 == null) return 0; else if (value1 == null) return -1; else if (value2 == null) return 1; /* End KULRICE-5170 Fix*/ compared = currentComparator.compare(value1, value2); } } catch (IllegalAccessException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (NoSuchMethodException e) { throw new BeanComparisonException("unable to compare property values", e); } catch (InvocationTargetException e) { throw new BeanComparisonException("unable to compare property values", e); } return compared; }
From source file:org.openfaces.component.table.AbstractTable.java
protected Comparator<Object> createRowDataComparator(List<SortingRule> sortingRules) { if (sortingRules == null) return null; int sortingRuleCount = sortingRules.size(); if (sortingRuleCount == 0) return null; FacesContext facesContext = FacesContext.getCurrentInstance(); final Comparator<Object>[] comparators = new Comparator[sortingRuleCount]; int readyComparatorCount = 0; for (int i = 0; i < sortingRuleCount; i++) { SortingRule rule = sortingRules.get(i); Comparator<Object> ruleComparator = createRuleComparator(facesContext, rule); if (ruleComparator == null) continue; comparators[readyComparatorCount++] = ruleComparator; }/*from w ww . j a v a2s . c o m*/ final int readyComparatorCount1 = readyComparatorCount; return new Comparator<Object>() { public int compare(Object o1, Object o2) { for (int i = 0; i < readyComparatorCount1; i++) { Comparator<Object> comparator = comparators[i]; int result = comparator.compare(o1, o2); if (result != 0) return result; } return 0; } }; }
From source file:org.openfaces.component.table.impl.TableDataModel.java
private boolean recordsInTheSameGroup(Comparator<Object> comparator, RowInfo rowInfo1, RowInfo rowInfo2) { Object record1 = rowInfo1 != null ? rowInfo1.getRowData() : null; Object record2 = rowInfo2 != null ? rowInfo2.getRowData() : null; if (record1 == null || record2 == null) return false; if (record1 instanceof GroupHeaderOrFooter || record2 instanceof GroupHeaderOrFooter) return false; return comparator.compare(record1, record2) == 0; }
From source file:android.support.designox.widget.CoordinatorLayout.java
private static void selectionSort(final List<View> list, final Comparator<View> comparator) { if (list == null || list.size() < 2) { return;/*from ww w . j a v a 2 s. c om*/ } final View[] array = new View[list.size()]; list.toArray(array); final int count = array.length; for (int i = 0; i < count; i++) { int min = i; for (int j = i + 1; j < count; j++) { if (comparator.compare(array[j], array[min]) < 0) { min = j; } } if (i != min) { // We have a different min so swap the items final View minItem = array[min]; array[min] = array[i]; array[i] = minItem; } } // Finally add the array back into the collection list.clear(); for (int i = 0; i < count; i++) { list.add(array[i]); } }
From source file:enumj.Enumerator.java
/** * Returns the minimum of the enumerated elements according to the provided * comparator./*from ww w . j a v a2s . c o m*/ * * @param comparator state-less {@link Comparator} instance * to compare enumerated elements. * @return {@link Optional} instance containing the minimal enumerated * element. * @exception IllegalArgumentException <code>comparator</code> * is null. * @see #max(java.util.Comparator) */ public default Optional<E> min(Comparator<? super E> comparator) { E m = null; while (hasNext()) { E e = next(); m = (m == null || comparator.compare(e, m) < 0) ? e : m; } return Optional.ofNullable(m); }
From source file:org.rhq.enterprise.server.content.RepoManagerBean.java
public PackageVersion getLatestPackageVersion(Subject subject, int packageId, int repoId) { if (!authzManager.canViewRepo(subject, repoId)) { throw new PermissionException("User [" + subject + "] cannot access the repo with id " + repoId); }/* w ww . ja v a2 s.c o m*/ Query q = entityManager.createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_AND_REPO_ID); q.setParameter("packageId", packageId); q.setParameter("repoId", repoId); @SuppressWarnings("unchecked") List<PackageVersion> results = (List<PackageVersion>) q.getResultList(); if (results.size() == 0) { return null; } else if (results.size() == 1) { return results.get(0); } PackageVersion latest = results.get(0); String packageTypeName = latest.getGeneralPackage().getPackageType().getName(); Comparator<PackageVersion> versionComparator = null; try { PackageTypeBehavior behavior = ContentManagerHelper.getPackageTypeBehavior(packageTypeName); versionComparator = behavior.getPackageVersionComparator(packageTypeName); } catch (Exception e) { log.error("Could not get the package type behavior for package type '" + packageTypeName + "'. This should not happen.", e); } if (versionComparator == null) { versionComparator = PackageVersion.DEFAULT_COMPARATOR; } Iterator<PackageVersion> it = results.iterator(); it.next(); //skip the first element, we don't have to compare it with itself while (it.hasNext()) { PackageVersion current = it.next(); if (versionComparator.compare(latest, current) < 0) { latest = current; } } return latest; }
From source file:enumj.Enumerator.java
/** * Returns the maximum of the enumerated elements according to the provided * comparator./*from w ww . ja va 2s . c o m*/ * * @param comparator state-less {@link Comparator} instance * to compare enumerated elements. * @return {@link Optional} instance containing the maximal enumerated * element. * @exception IllegalArgumentException <code>comparator</code> * is null. * @see #min(java.util.Comparator) */ public default Optional<E> max(Comparator<? super E> comparator) { Checks.ensureNotNull(comparator, Messages.NULL_ENUMERATOR_COMPARATOR); E m = null; while (hasNext()) { E e = next(); m = (m == null || comparator.compare(e, m) > 0) ? e : m; } return Optional.ofNullable(m); }
From source file:org.apache.flink.runtime.operators.sort.CombiningUnilateralSortMergerITCase.java
@Test public void testSortAndValidate() throws Exception { final Hashtable<TestData.Key, Integer> countTable = new Hashtable<TestData.Key, Integer>(KEY_MAX); for (int i = 1; i <= KEY_MAX; i++) { countTable.put(new TestData.Key(i), new Integer(0)); }/* w ww .j av a 2s . c o m*/ // comparator final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator(); // reader MockRecordReader reader = new MockRecordReader(); // merge iterator LOG.debug("initializing sortmerger"); TestCountCombiner2 comb = new TestCountCombiner2(); Sorter<Record> merger = new CombiningUnilateralSortMerger<Record>(comb, this.memoryManager, this.ioManager, reader, this.parentTask, this.serializerFactory, this.comparator, 0.25, 2, 0.7f); // emit data LOG.debug("emitting data"); TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.FIX_LENGTH); Record rec = new Record(); final TestData.Value value = new TestData.Value("1"); for (int i = 0; i < NUM_PAIRS; i++) { Assert.assertTrue((rec = generator.next(rec)) != null); final TestData.Key key = rec.getField(0, TestData.Key.class); rec.setField(1, value); reader.emit(rec); countTable.put(new TestData.Key(key.getKey()), countTable.get(key) + 1); } reader.close(); rec = null; // check order MutableObjectIterator<Record> iterator = merger.getIterator(); LOG.debug("checking results"); Record rec1 = new Record(); Record rec2 = new Record(); Assert.assertTrue((rec1 = iterator.next(rec1)) != null); countTable.put(new TestData.Key(rec1.getField(0, TestData.Key.class).getKey()), countTable.get(rec1.getField(0, TestData.Key.class)) - (Integer.parseInt(rec1.getField(1, TestData.Value.class).toString()))); while ((rec2 = iterator.next(rec2)) != null) { final Key k1 = rec1.getField(0, TestData.Key.class); final Key k2 = rec2.getField(0, TestData.Key.class); Assert.assertTrue(keyComparator.compare(k1, k2) <= 0); countTable.put(new TestData.Key(k2.getKey()), countTable.get(k2) - (Integer.parseInt(rec2.getField(1, TestData.Value.class).toString()))); Record tmp = rec1; rec1 = rec2; k1.setKey(k2.getKey()); rec2 = tmp; } for (Integer cnt : countTable.values()) { Assert.assertTrue(cnt == 0); } merger.close(); // if the combiner was opened, it must have been closed Assert.assertTrue(comb.opened == comb.closed); }
From source file:eu.stratosphere.pact.runtime.sort.CombiningUnilateralSortMergerITCase.java
@Test public void testSortAndValidate() throws Exception { final Hashtable<TestData.Key, Integer> countTable = new Hashtable<TestData.Key, Integer>(KEY_MAX); for (int i = 1; i <= KEY_MAX; i++) { countTable.put(new TestData.Key(i), new Integer(0)); }//from w w w .j ava 2s. c o m // comparator final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator(); // reader MockRecordReader reader = new MockRecordReader(); // merge iterator LOG.debug("initializing sortmerger"); TestCountCombiner2 comb = new TestCountCombiner2(); Sorter<Record> merger = new CombiningUnilateralSortMerger<Record>(comb, this.memoryManager, this.ioManager, reader, this.parentTask, this.serializerFactory, this.comparator, 64 * 1024 * 1024, 2, 0.7f); // emit data LOG.debug("emitting data"); TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.FIX_LENGTH); Record rec = new Record(); final TestData.Value value = new TestData.Value("1"); for (int i = 0; i < NUM_PAIRS; i++) { Assert.assertTrue((rec = generator.next(rec)) != null); final TestData.Key key = rec.getField(0, TestData.Key.class); rec.setField(1, value); reader.emit(rec); countTable.put(new TestData.Key(key.getKey()), countTable.get(key) + 1); } reader.close(); rec = null; // check order MutableObjectIterator<Record> iterator = merger.getIterator(); LOG.debug("checking results"); Record rec1 = new Record(); Record rec2 = new Record(); Assert.assertTrue((rec1 = iterator.next(rec1)) != null); countTable.put(new TestData.Key(rec1.getField(0, TestData.Key.class).getKey()), countTable.get(rec1.getField(0, TestData.Key.class)) - (Integer.parseInt(rec1.getField(1, TestData.Value.class).toString()))); while ((rec2 = iterator.next(rec2)) != null) { final Key k1 = rec1.getField(0, TestData.Key.class); final Key k2 = rec2.getField(0, TestData.Key.class); Assert.assertTrue(keyComparator.compare(k1, k2) <= 0); countTable.put(new TestData.Key(k2.getKey()), countTable.get(k2) - (Integer.parseInt(rec2.getField(1, TestData.Value.class).toString()))); Record tmp = rec1; rec1 = rec2; k1.setKey(k2.getKey()); rec2 = tmp; } for (Integer cnt : countTable.values()) { Assert.assertTrue(cnt == 0); } merger.close(); // if the combiner was opened, it must have been closed Assert.assertTrue(comb.opened == comb.closed); }