Java Code Examples for com.helger.commons.collection.impl.ICommonsList#sort()

The following examples show how to use com.helger.commons.collection.impl.ICommonsList#sort() . 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: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get a map sorted by its keys. The comparison order is defined by the passed
 * comparator object.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        The map to sort. May not be <code>null</code>.
 * @param aKeyComparator
 *        The comparator to be used. May not be <code>null</code>.
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByKey (@Nullable final Map <KEYTYPE, VALUETYPE> aMap,
                                                                                           @Nonnull final Comparator <? super KEYTYPE> aKeyComparator)
{
  ValueEnforcer.notNull (aKeyComparator, "KeyComparator");

  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted Map.Entry list by Entry.getValue ()
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getKey, aKeyComparator));
  return newOrderedMap (aList);
}
 
Example 2
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get a map sorted by aIter's values. The comparison order is defined by the
 * passed comparator object.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        The map to sort. May not be <code>null</code>.
 * @param aValueComparator
 *        The comparator to be used. May not be <code>null</code>.
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByValue (@Nullable final Map <KEYTYPE, VALUETYPE> aMap,
                                                                                             @Nonnull final Comparator <? super VALUETYPE> aValueComparator)
{
  ValueEnforcer.notNull (aValueComparator, "ValueComparator");

  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted Map.Entry list by Entry.getValue ()
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getValue, aValueComparator));
  return newOrderedMap (aList);
}
 
Example 3
Source File: HelpFormatter.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Appends the usage clause for an OptionGroup to a StringBuilder. The clause
 * is wrapped in square brackets if the group is required. The display of the
 * options is handled by appendOption
 *
 * @param aSB
 *        the StringBuilder to append to
 * @param aGroup
 *        the group to append
 * @see #_appendOption(StringBuilder,Option,boolean)
 */
private void _appendOptionGroup (final StringBuilder aSB, final OptionGroup aGroup)
{
  if (!aGroup.isRequired ())
    aSB.append ('[');

  final ICommonsList <Option> optList = aGroup.getAllOptions ();
  if (m_aOptionComparator != null)
    optList.sort (m_aOptionComparator);

  // for each option in the OptionGroup
  final Iterator <Option> it = optList.iterator ();
  while (it.hasNext ())
  {
    // whether the option is required or not is handled at group level
    _appendOption (aSB, it.next (), true);

    if (it.hasNext ())
      aSB.append (" | ");
  }

  if (!aGroup.isRequired ())
    aSB.append (']');
}
 
Example 4
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get a map sorted by aIter's keys. Because no comparator is defined, the key
 * type needs to implement the {@link java.lang.Comparable} interface.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        the map to sort
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByKey (@Nullable final Map <KEYTYPE, VALUETYPE> aMap)
{
  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted entry list
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getKey));
  return newOrderedMap (aList);
}
 
Example 5
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get a map sorted by its values. Because no comparator is defined, the value
 * type needs to implement the {@link java.lang.Comparable} interface.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        The map to sort. May not be <code>null</code>.
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE extends Comparable <? super VALUETYPE>> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByValue (@Nullable final Map <KEYTYPE, VALUETYPE> aMap)
{
  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted entry list
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getValue));
  return newOrderedMap (aList);
}
 
Example 6
Source File: HelpFormatter.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Prints the usage statement for the specified application.
 *
 * @param aPW
 *        The PrintWriter to print the usage statement
 * @param nWidth
 *        The number of characters to display per line
 * @param sAppName
 *        The application name
 * @param aOptions
 *        The command line Options
 */
public void printUsage (@Nonnull final PrintWriter aPW,
                        final int nWidth,
                        final String sAppName,
                        final Options aOptions)
{
  // initialise the string buffer
  final StringBuilder aSB = new StringBuilder (getSyntaxPrefix ()).append (sAppName).append (' ');

  // create a list for processed option groups
  final ICommonsSet <OptionGroup> aProcessedGroups = new CommonsHashSet <> ();

  final ICommonsList <Option> aOptList = aOptions.getAllResolvedOptions ();
  if (m_aOptionComparator != null)
    aOptList.sort (m_aOptionComparator);

  // iterate over the options
  for (final Iterator <Option> aIt = aOptList.iterator (); aIt.hasNext ();)
  {
    // get the next Option
    final Option aOption = aIt.next ();

    // check if the option is part of an OptionGroup
    final OptionGroup group = aOptions.getOptionGroup (aOption);

    // if the option is part of a group
    if (group != null)
    {
      // and if the group has not already been processed
      if (aProcessedGroups.add (group))
      {
        // add the usage clause
        _appendOptionGroup (aSB, group);
      }

      // otherwise the option was displayed in the group
      // previously so ignore it.
    }
    else
    {
      // if the Option is not part of an OptionGroup
      _appendOption (aSB, aOption, aOption.isRequired ());
    }

    if (aIt.hasNext ())
      aSB.append (' ');
  }

  // call printWrapped
  printWrapped (aPW, nWidth, aSB.toString ().indexOf (' ') + 1, aSB.toString ());
}