Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger ( philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Handles the application of object filters.

Philip Helger
public final class SelectUtils
  private SelectUtils ()

Apply a comparator and a filter on that list.

<T> The type of the elements
aStartCollection The initial collection. May be null.
aComparator The comparator be used. May be null.
aLimit The limitation to be used. May be null.
The original collection, if it was empty or no comparator or no limit is provided, a new collection otherwise.
  public static <T> List <T> sortAndLimit (@Nullable final List <T> aStartCollection,
                                           @Nullable final Comparator <T> aComparator,
                                           @Nullable final SelectLimit aLimit)
    if ((aComparator == null && aLimit == null) || ContainerHelper.isEmpty (aStartCollection))
      return aStartCollection;
    List <T> c = new ArrayList <T> (aStartCollection);
    // apply sorting
    if (aComparator != null)
      ContainerHelper.getSortedInline (caComparator);
    // apply limit
    if (aLimit != null)
      int nFromIndex = aLimit.getStartIndex ();
      int nToIndex = Math.min (nFromIndex + aLimit.getResultCount (), c.size ());
      // handle overflow
      if (nFromIndex > nToIndex)
        nFromIndex = 0;
        nToIndex = Math.min (aLimit.getResultCount (), c.size ());
      if (nFromIndex == nToIndex)
        c.clear ();
        final List <T> c2 = new ArrayList <T> (nToIndex - nFromIndex - 1);
        for (int i = nFromIndexi < nToIndex; ++i)
          c2.add (c.get (i));
        c = c2;
    return c;
New to GrepCode? Check out our FAQ X