com.helger.commons.compare.IComparator Java Examples

The following examples show how to use com.helger.commons.compare.IComparator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSortedIIterableIterator ()
{
  assertNotNull (getSorted ((IIterableIterator <String>) null));

  final ICommonsList <String> aList = newList ("d", "c", "b", "a");
  ICommonsList <String> aSorted = getSorted (new IterableIterator <> (aList));
  assertEquals (4, aSorted.size ());
  assertEquals ("a", aSorted.get (0));
  assertEquals ("b", aSorted.get (1));
  assertEquals ("c", aSorted.get (2));
  assertEquals ("d", aSorted.get (3));

  aSorted = getSorted (new IterableIterator <> (aList), IComparator.getComparatorCollating (Locale.US));
  assertEquals (4, aSorted.size ());
  assertEquals ("a", aSorted.get (0));
  assertEquals ("b", aSorted.get (1));
  assertEquals ("c", aSorted.get (2));
  assertEquals ("d", aSorted.get (3));
}
 
Example #2
Source File: CXML.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IComparator <QName> getComparatorQNameLocalPartBeforeNamespaceURI ()
{
  return (o1, o2) -> {
    int ret = o1.getLocalPart ().compareTo (o2.getLocalPart ());
    if (ret == 0)
      ret = CompareHelper.compare (o1.getNamespaceURI (), o2.getNamespaceURI (), true);
    return ret;
  };
}
 
Example #3
Source File: CXML.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IComparator <QName> getComparatorQNameForNamespacePrefix ()
{
  return (o1, o2) -> {
    String sPrefix1 = o1.getPrefix ();
    if (XMLConstants.XMLNS_ATTRIBUTE.equals (sPrefix1))
      sPrefix1 = o1.getLocalPart ();
    String sPrefix2 = o2.getPrefix ();
    if (XMLConstants.XMLNS_ATTRIBUTE.equals (sPrefix2))
      sPrefix2 = o2.getLocalPart ();
    return sPrefix1.compareTo (sPrefix2);
  };
}
 
Example #4
Source File: CXML.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static IComparator <QName> getComparatorQNameNamespaceURIBeforeLocalPart ()
{
  return (o1, o2) -> {
    int ret = CompareHelper.compare (o1.getNamespaceURI (), o2.getNamespaceURI (), true);
    if (ret == 0)
      ret = o1.getLocalPart ().compareTo (o2.getLocalPart ());
    return ret;
  };
}
 
Example #5
Source File: TreeSorterTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testTreeString ()
{
  final DefaultTree <String> aTree = new DefaultTree <> ();
  assertNotNull (aTree.getRootItem ());
  final DefaultTreeItem <String> i1 = aTree.getRootItem ().createChildItem ("Windows");
  i1.createChildItem ("sxs");
  i1.createChildItem ("temp");
  i1.createChildItem ("System32");
  final DefaultTreeItem <String> i2 = aTree.getRootItem ().createChildItem ("Program Files");
  i2.createChildItem ("Eclipse");
  i2.createChildItem ("Apache Software Foundation");

  // Sort all items by String
  TreeSorter.sort (aTree, IComparator.getComparatorCollating (Locale.US));

  assertEquals (2, aTree.getRootItem ().getChildCount ());
  final List <? extends DefaultTreeItem <String>> aChildren = aTree.getRootItem ().getAllChildren ();
  assertSame (i2, aChildren.get (0));
  assertSame (i1, aChildren.get (1));
  // Test Apache (children must also be sorted)
  assertEquals (2, i2.getChildCount ());
  assertEquals ("Apache Software Foundation", i2.getChildAtIndex (0).getData ());
  assertEquals ("Eclipse", i2.getChildAtIndex (1).getData ());
  // Test Windows
  assertEquals (3, i1.getChildCount ());
  assertEquals ("sxs", i1.getChildAtIndex (0).getData ());
  assertEquals ("System32", i1.getChildAtIndex (1).getData ());
  assertEquals ("temp", i1.getChildAtIndex (2).getData ());
}
 
Example #6
Source File: PSXPathQueryBinding.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsNavigableMap <String, String> getStringReplacementMap (@Nonnull final List <PSParam> aParams)
{
  final ICommonsNavigableMap <String, String> ret = new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ());
  for (final PSParam aParam : aParams)
    ret.put (PARAM_VARIABLE_PREFIX + aParam.getName (), aParam.getValue ());
  return ret;
}
 
Example #7
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Test for method getSortedByValue
 */
@Test
public void testGetSortedByValue ()
{
  assertNotNull (getSortedByValue ((Map <?, String>) null));
  assertNotNull (getSortedByValue (null, IComparator.getComparatorCollating (Locale.US).reversed ()));

  try
  {
    // null Comparator
    getSortedByValue (newMap (), (Comparator <String>) null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}

  final Map <String, String> aMap = newMap ("K1", "ValueB", "K2", "ValueC", "K3", "ValueA");
  assertNotNull (aMap);
  assertEquals (3, aMap.size ());

  Iterator <Map.Entry <String, String>> it = getSortedByValue (aMap).entrySet ().iterator ();
  assertEquals ("ValueA", it.next ().getValue ());
  assertEquals ("ValueB", it.next ().getValue ());
  assertEquals ("ValueC", it.next ().getValue ());

  // reverse sort
  it = getSortedByValue (aMap, IComparator.getComparatorCollating (Locale.US).reversed ()).entrySet ().iterator ();
  assertEquals ("ValueC", it.next ().getValue ());
  assertEquals ("ValueB", it.next ().getValue ());
  assertEquals ("ValueA", it.next ().getValue ());
}
 
Example #8
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Test for method getSortedByKey
 */
@Test
public void testGetSortedByKey ()
{
  assertNotNull (getSortedByKey ((Map <String, ?>) null));
  assertNotNull (getSortedByKey (null, IComparator.getComparatorCollating (Locale.US).reversed ()));

  try
  {
    // null Comparator
    getSortedByKey (newMap (), (Comparator <String>) null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}

  final Map <String, String> aMap = newMap ("K2", "ValueA", "K3", "ValueB", "K1", "ValueC");
  assertNotNull (aMap);
  assertEquals (3, aMap.size ());

  Iterator <Map.Entry <String, String>> it = getSortedByKey (aMap).entrySet ().iterator ();
  assertEquals ("K1", it.next ().getKey ());
  assertEquals ("K2", it.next ().getKey ());
  assertEquals ("K3", it.next ().getKey ());

  // reverse sort
  it = getSortedByKey (aMap, IComparator.getComparatorCollating (Locale.US).reversed ()).entrySet ().iterator ();
  assertEquals ("K3", it.next ().getKey ());
  assertEquals ("K2", it.next ().getKey ());
  assertEquals ("K1", it.next ().getKey ());
}
 
Example #9
Source File: JavaListAllLocalesFuncTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore ("Too verbose")
public void testListAllCountries ()
{
  for (final Locale aLocale : CollectionHelper.getSorted (Locale.getAvailableLocales (),
                                                          IComparator.getComparatorCollating (Locale::getCountry,
                                                                                              Locale.US)))
    if (aLocale.getCountry ().length () > 0)
      LOGGER.info (aLocale.getCountry () +
                   " " +
                   aLocale.getDisplayCountry (Locale.US) +
                   " (" +
                   aLocale.toString () +
                   ")");
}
 
Example #10
Source File: PSXPathVariables.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
private static ICommonsNavigableMap <String, String> _createMap ()
{
  return new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ());
}
 
Example #11
Source File: IHasName.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
static Comparator <IHasName> getComparatorCollating (@Nullable final Locale aSortLocale)
{
  return IComparator.getComparatorCollating (IHasName::getName, aSortLocale);
}
 
Example #12
Source File: IDisplayNameProvider.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
default Comparator <DATATYPE> getComparatorCollating (@Nullable final Locale aSortLocale)
{
  return IComparator.getComparatorCollating (this::getDisplayName, aSortLocale);
}
 
Example #13
Source File: IHasDisplayName.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
static Comparator <IHasDisplayName> getComparatorCollating (@Nullable final Locale aSortLocale)
{
  return IComparator.getComparatorCollating (IHasDisplayName::getDisplayName, aSortLocale);
}
 
Example #14
Source File: IHasText.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
static Comparator <IHasText> getComparatorCollating (@Nonnull final Locale aContentLocale,
                                                     @Nullable final Locale aSortLocale)
{
  return IComparator.getComparatorCollating (x -> x.getText (aContentLocale), aSortLocale);
}
 
Example #15
Source File: IDisplayTextProvider.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
default Comparator <DATATYPE> getComparatorCollating (@Nonnull final Locale aContentLocale,
                                                      @Nullable final Locale aSortLocale)
{
  return IComparator.getComparatorCollating (x -> getDisplayText (x, aContentLocale), aSortLocale);
}
 
Example #16
Source File: IHasDisplayText.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
static Comparator <IHasDisplayText> getComparatorCollating (@Nonnull final Locale aContentLocale,
                                                            @Nullable final Locale aSortLocale)
{
  return IComparator.getComparatorCollating (x -> x.getDisplayText (aContentLocale), aSortLocale);
}
 
Example #17
Source File: TreeWithIDSorterTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testTreeWithIDValue ()
{
  final DefaultTreeWithID <String, String> aTree = new DefaultTreeWithID <> ();
  assertNotNull (aTree.getRootItem ());
  final DefaultTreeItemWithID <String, String> i1 = aTree.getRootItem ().createChildItem ("r1", "Windows");
  i1.createChildItem ("w1", "sxs");
  i1.createChildItem ("w2", "temp");
  i1.createChildItem ("w3", "System32");
  final DefaultTreeItemWithID <String, String> i2 = aTree.getRootItem ().createChildItem ("r2", "Program Files");
  i2.createChildItem ("p1", "Eclipse");
  i2.createChildItem ("p2", "Apache Software Foundation");

  // Sort all items by value
  TreeWithIDSorter.sortByValue (aTree, IComparator.getComparatorCollating (Locale.US));

  assertEquals (2, aTree.getRootItem ().getChildCount ());
  List <? extends DefaultTreeItemWithID <String, String>> aChildren = aTree.getRootItem ().getAllChildren ();
  assertSame (i2, aChildren.get (0));
  assertSame (i1, aChildren.get (1));
  // Test Apache (children must also be sorted)
  assertEquals (2, i2.getChildCount ());
  assertEquals ("Apache Software Foundation", i2.getChildAtIndex (0).getData ());
  assertEquals ("Eclipse", i2.getChildAtIndex (1).getData ());
  // Test Windows
  assertEquals (3, i1.getChildCount ());
  assertEquals ("sxs", i1.getChildAtIndex (0).getData ());
  assertEquals ("System32", i1.getChildAtIndex (1).getData ());
  assertEquals ("temp", i1.getChildAtIndex (2).getData ());

  // Sort all items by keys
  TreeWithIDSorter.sortByID (aTree, IComparator.getComparatorCollating (Locale.US));

  assertEquals (2, aTree.getRootItem ().getChildCount ());
  aChildren = aTree.getRootItem ().getAllChildren ();
  assertSame (i1, aChildren.get (0));
  assertSame (i2, aChildren.get (1));
  // Test Windows
  assertEquals (3, i1.getChildCount ());
  assertEquals ("sxs", i1.getChildAtIndex (0).getData ());
  assertEquals ("temp", i1.getChildAtIndex (1).getData ());
  assertEquals ("System32", i1.getChildAtIndex (2).getData ());
  // Test Apache (children must also be sorted)
  assertEquals (2, i2.getChildCount ());
  assertEquals ("Eclipse", i2.getChildAtIndex (0).getData ());
  assertEquals ("Apache Software Foundation", i2.getChildAtIndex (1).getData ());
}
 
Example #18
Source File: HelpFormatter.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Comparator used to sort the options when they output in help text. Defaults
 * to case-insensitive alphabetical sorting by option key.
 *
 * @return the {@link Comparator} currently in use to sort the options
 */
@Nullable
public IComparator <Option> getOptionComparator ()
{
  return m_aOptionComparator;
}
 
Example #19
Source File: HelpFormatter.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Set the comparator used to sort the options when they output in help text.
 * Passing in a null comparator will keep the options in the order they were
 * declared.
 *
 * @param aComparator
 *        the {@link Comparator} to use for sorting the options
 */
public void setOptionComparator (@Nullable final IComparator <Option> aComparator)
{
  m_aOptionComparator = aComparator;
}