Java Code Examples for com.helger.commons.collection.CollectionHelper#getSorted()

The following examples show how to use com.helger.commons.collection.CollectionHelper#getSorted() . 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: IComparatorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testCollatingOrder ()
{
  final String S1 = "abc";
  final String S2 = "ABC";
  final String S3 = "ab";
  final String [] x = new String [] { S1, S2, S3 };

  // Explicitly sort ascending
  List <String> l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.US));
  assertArrayEquals (new String [] { S3, S1, S2 }, l.toArray ());

  // Explicitly sort descending
  l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.US).reversed ());
  assertArrayEquals (new String [] { S2, S1, S3 }, l.toArray ());
}
 
Example 2
Source File: IComparatorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Test for constructors using a locale
 */
@Test
public void testLocaleGerman ()
{
  final String S1 = "bbc";
  final String S2 = "abc";
  final String S3 = "äbc";
  final String [] x = new String [] { S1, S2, S3 };

  // default: sort ascending
  ICommonsList <String> l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.GERMAN));
  assertArrayEquals (new String [] { S2, S3, S1 }, l.toArray ());

  // sort ascending manually
  l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.GERMAN));
  assertArrayEquals (new String [] { S2, S3, S1 }, l.toArray ());

  // sort descending manually
  l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.GERMAN).reversed ());
  assertArrayEquals (new String [] { S1, S3, S2 }, l.toArray ());

  // null locale allowed
  IComparator.getComparatorCollating ((Locale) null);
  assertArrayEquals (new String [] { S1, S3, S2 }, l.toArray ());
}
 
Example 3
Source File: IComparatorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparatorStringLongestFirst ()
{
  final String S1 = "zzz";
  final String S2 = "zz";
  final String S3 = "aa";
  final String S4 = null;
  final String [] x = new String [] { S1, S2, S3, S4 };

  // Null values first
  ICommonsList <String> l = CollectionHelper.getSorted (x, IComparator.getComparatorStringLongestFirst ());
  assertArrayEquals (new String [] { S4, S1, S3, S2 }, l.toArray ());

  // Null values last
  l = CollectionHelper.getSorted (x, IComparator.getComparatorStringLongestFirst (false));
  assertArrayEquals (new String [] { S1, S3, S2, S4 }, l.toArray ());

  // Null values first
  l = CollectionHelper.getSorted (x, IComparator.getComparatorStringShortestFirst ());
  assertArrayEquals (new String [] { S4, S3, S2, S1 }, l.toArray ());

  // Null values last
  l = CollectionHelper.getSorted (x, IComparator.getComparatorStringShortestFirst (false));
  assertArrayEquals (new String [] { S3, S2, S1, S4 }, l.toArray ());
}
 
Example 4
Source File: SettingsPersistenceJson.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    final IJsonObject aProps = new JsonObject ();
    for (final Map.Entry <String, Object> aEntry : CollectionHelper.getSorted (aSettings.entrySet (),
                                                                               Comparator.comparing (Map.Entry::getKey)))
    {
      final String sName = aEntry.getKey ();
      final Object aValue = aEntry.getValue ();
      final String sValue = TypeConverter.convert (aValue, String.class);
      aProps.add (sName, sValue);
    }

    final JsonWriterSettings aJWS = new JsonWriterSettings ();
    aJWS.setIndentEnabled (true);
    aJWS.setQuoteNames (false);

    // Does not close the output stream!
    new JsonWriter (aJWS).writeToWriterAndClose (aProps, StreamHelper.createWriter (aOS, m_aCharset));
    return ESuccess.SUCCESS;
  }
  catch (final IOException ex)
  {
    LOGGER.error ("Failed to write settings to JSON file", ex);
    return ESuccess.FAILURE;
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example 5
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 6
Source File: JavaListAllLocalesFuncTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testListAllSerbianCountries ()
{
  for (final Locale aLocale : CollectionHelper.getSorted (Locale.getAvailableLocales (),
                                                          Comparator.comparing (Locale::toString)))
    if (aLocale.getLanguage ().equals ("sr") ||
        aLocale.getLanguage ().equals ("sh") ||
        aLocale.getLanguage ().equals ("bs"))
      LOGGER.info (aLocale.toString () + ": " + aLocale.getDisplayName (Locale.US));
}
 
Example 7
Source File: MainCreateJAXBBinding22.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Iterable <File> _getFileList (final String sPath)
{
  return CollectionHelper.getSorted (new FileSystemIterator (sPath).withFilter (IFileFilter.filenameEndsWith (".xsd"))
                                                                   .withFilter (IFileFilter.filenameMatchNoRegEx ("^CCTS.*",
                                                                                                                  ".*xmldsig.*",
                                                                                                                  ".*XAdES.*")),
                                     Comparator.comparing (File::getName));
}
 
Example 8
Source File: MainCreateJAXBBinding23.java    From ph-ubl with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Iterable <File> _getFileList (final String sPath)
{
  return CollectionHelper.getSorted (new FileSystemIterator (sPath).withFilter (IFileFilter.filenameEndsWith (".xsd"))
                                                                   .withFilter (IFileFilter.filenameMatchNoRegEx ("^CCTS.*",
                                                                                                                  ".*xmldsig.*",
                                                                                                                  ".*XAdES.*")),
                                     Comparator.comparing (File::getName));
}
 
Example 9
Source File: MockHasSortedChildren.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public MockHasSortedChildren (@Nonnull final String sID, @Nullable final MockHasSortedChildren... aList)
{
  m_sID = sID;
  m_aList = CollectionHelper.getSorted (aList, IHasID.getComparatorID ());
}
 
Example 10
Source File: MainCreateJAXBBinding21.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static Iterable <File> _getFileList (final String sPath)
{
  return CollectionHelper.getSorted (new FileSystemIterator (sPath).withFilter (IFileFilter.filenameEndsWith (".xsd")),
                                     Comparator.comparing (File::getName));
}
 
Example 11
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static Iterable <File> _getFileList (final String sPath)
{
  return CollectionHelper.getSorted (new FileSystemIterator (sPath).withFilter (IFileFilter.filenameEndsWith (".xsd")),
                                     Comparator.comparing (File::getName));
}