All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.commons.convert.collections.ContainerConversionHelper Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[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
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */
package com.phloc.commons.convert.collections;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.annotations.PresentForCodeCoverage;
import com.phloc.commons.annotations.ReturnsImmutableObject;
import com.phloc.commons.annotations.ReturnsMutableCopy;
import com.phloc.commons.collections.ArrayHelper;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.convert.IUnidirectionalConverter;
import com.phloc.commons.filter.IFilter;

/**
 * This utility class helps applying conversions onto collections.
 * 
 * @author Philip Helger
 */
@Immutable
public final class ContainerConversionHelper
{
  @PresentForCodeCoverage
  @SuppressWarnings ("unused")
  private static final ContainerConversionHelper s_aInstance = new ContainerConversionHelper ();

  private ContainerConversionHelper ()
  {}

  @Nonnull
  @ReturnsMutableCopy
  public static  Set  newSet (@Nonnull final Iterator  it,
                                                         @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final Set  ret = new HashSet  ();
    while (it.hasNext ())
      ret.add (aConverter.convert (it.next ()));
    return ret;
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  Set  newSet (@Nonnull final Iterable  aCont,
                                                         @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final Set  ret = new HashSet  ();
    for (final SRCTYPE aValue : aCont)
      ret.add (aConverter.convert (aValue));
    return ret;
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  Set  newSet (@Nonnull final Iterable  aCont,
                                                         @Nonnull final IFilter  aFilter,
                                                         @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final Set  ret = new HashSet  ();
    for (final SRCTYPE aIn : aCont)
      if (aFilter.matchesFilter (aIn))
        ret.add (aConverter.convert (aIn));
    return ret;
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  Set  newUnmodifiableSet (@Nonnull final Iterator  it,
                                                                     @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (newSet (it, aConverter));
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  Set  newUnmodifiableSet (@Nonnull final Iterable  aCont,
                                                                     @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (newSet (aCont, aConverter));
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  Set  newUnmodifiableSet (@Nonnull final Iterable  aCont,
                                                                     @Nonnull final IFilter  aFilter,
                                                                     @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (ContainerConversionHelper. newSet (aCont,
                                                                                                  aFilter,
                                                                                                  aConverter));
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  Set  newOrderedSet (@Nonnull final Iterator  it,
                                                                @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final Set  ret = new LinkedHashSet  ();
    while (it.hasNext ())
      ret.add (aConverter.convert (it.next ()));
    return ret;
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  Set  newOrderedSet (@Nonnull final Iterable  aCont,
                                                                @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final Set  ret = new LinkedHashSet  ();
    for (final SRCTYPE aValue : aCont)
      ret.add (aConverter.convert (aValue));
    return ret;
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  Set  newOrderedSet (@Nonnull final Iterable  aCont,
                                                                @Nonnull final IFilter  aFilter,
                                                                @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final Set  ret = new LinkedHashSet  ();
    for (final SRCTYPE aIn : aCont)
      if (aFilter.matchesFilter (aIn))
        ret.add (aConverter.convert (aIn));
    return ret;
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  Set  newUnmodifiableOrderedSet (@Nonnull final Iterator  it,
                                                                            @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (newOrderedSet (it, aConverter));
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  Set  newUnmodifiableOrderedSet (@Nonnull final Iterable  aCont,
                                                                            @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (newOrderedSet (aCont, aConverter));
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  Set  newUnmodifiableOrderedSet (@Nonnull final Iterable  aCont,
                                                                            @Nonnull final IFilter  aFilter,
                                                                            @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (ContainerConversionHelper. newOrderedSet (aCont,
                                                                                                         aFilter,
                                                                                                         aConverter));
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  List  newList (@Nullable final Iterable  aCont,
                                                           @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final List  ret = new ArrayList  ();
    if (aCont != null)
      for (final SRCTYPE aIn : aCont)
        ret.add (aConverter.convert (aIn));
    return ret;
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  List  newList (@Nullable final SRCTYPE [] aCont,
                                                           @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final List  ret = new ArrayList  (ArrayHelper.getSize (aCont));
    if (aCont != null)
      for (final SRCTYPE aIn : aCont)
        ret.add (aConverter.convert (aIn));
    return ret;
  }

  @Nonnull
  @ReturnsMutableCopy
  public static  List  newList (@Nullable final Iterable  aCont,
                                                           @Nonnull final IFilter  aFilter,
                                                           @Nonnull final IUnidirectionalConverter  aConverter)
  {
    final List  ret = new ArrayList  ();
    if (aCont != null)
      for (final SRCTYPE aIn : aCont)
        if (aFilter.matchesFilter (aIn))
          ret.add (aConverter.convert (aIn));
    return ret;
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  List  newUnmodifiableList (@Nullable final Iterable  aCont,
                                                                       @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (newList (aCont, aConverter));
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  List  newUnmodifiableList (@Nullable final SRCTYPE [] aCont,
                                                                       @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (newList (aCont, aConverter));
  }

  @Nonnull
  @ReturnsImmutableObject
  public static  List  newUnmodifiableList (@Nullable final Iterable  aCont,
                                                                       @Nonnull final IFilter  aFilter,
                                                                       @Nonnull final IUnidirectionalConverter  aConverter)
  {
    return ContainerHelper.makeUnmodifiable (ContainerConversionHelper. newList (aCont,
                                                                                                   aFilter,
                                                                                                   aConverter));
  }

  /**
   * Convert the given iterator to a sorted list.
   * 
   * @param 
   *        The type of elements to iterate (source).
   * @param 
   *        The type of elements to return (destination).
   * @param it
   *        Input iterator. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   * @return a non-null {@link ArrayList} based on the results of
   *         {@link ContainerHelper#getSortedInline(List)}.
   */
  @Nonnull
  @ReturnsMutableCopy
  public static > List  getSorted (@Nonnull final Iterator  it,
                                                                                                  @Nonnull final IUnidirectionalConverter  aConverter)
  {
    ValueEnforcer.notNull (it, "Iterator");
    ValueEnforcer.notNull (aConverter, "Converter");

    final List  ret = new ArrayList  ();
    while (it.hasNext ())
      ret.add (aConverter.convert (it.next ()));
    return ContainerHelper.getSortedInline (ret);
  }

  /**
   * Convert the given iterator to a sorted list.
   * 
   * @param 
   *        The type of elements to iterate (source).
   * @param 
   *        The type of elements to return (destination).
   * @param it
   *        Input iterator. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   * @param aComparator
   *        The comparator to use. May not be null.
   * @return a non-null {@link ArrayList} based on the results of
   *         {@link ContainerHelper#getSortedInline(List, Comparator)}.
   */
  @Nonnull
  @ReturnsMutableCopy
  public static  List  getSorted (@Nonnull final Iterator  it,
                                                             @Nonnull final IUnidirectionalConverter  aConverter,
                                                             @Nonnull final Comparator  aComparator)
  {
    ValueEnforcer.notNull (it, "Iterator");
    ValueEnforcer.notNull (aConverter, "Converter");
    ValueEnforcer.notNull (aComparator, "Comparator");

    final List  ret = new ArrayList  ();
    while (it.hasNext ())
      ret.add (aConverter.convert (it.next ()));
    return ContainerHelper.getSortedInline (ret, aComparator);
  }

  /**
   * Convert the given iterator to a sorted list.
   * 
   * @param 
   *        The type of elements to iterate (source).
   * @param 
   *        The type of elements to return (destination).
   * @param aCont
   *        Input container. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   * @return a non-null {@link ArrayList} based on the results of
   *         {@link ContainerHelper#getSortedInline(List)}.
   */
  @Nonnull
  @ReturnsMutableCopy
  public static > List  getSorted (@Nonnull final Iterable  aCont,
                                                                                                  @Nonnull final IUnidirectionalConverter  aConverter)
  {
    ValueEnforcer.notNull (aCont, "Container");
    ValueEnforcer.notNull (aConverter, "Converter");

    final List  ret = new ArrayList  ();
    for (final SRCTYPE aSrc : aCont)
      ret.add (aConverter.convert (aSrc));
    return ContainerHelper.getSortedInline (ret);
  }

  /**
   * Convert the given iterator to a sorted list.
   * 
   * @param 
   *        The type of elements to iterate (source).
   * @param 
   *        The type of elements to return (destination).
   * @param aCont
   *        Input iterator. May not be null.
   * @param aConverter
   *        The converter to be used. May not be null.
   * @param aComparator
   *        The comparator to use. May not be null.
   * @return a non-null {@link ArrayList} based on the results of
   *         {@link ContainerHelper#getSortedInline(List, Comparator)}.
   */
  @Nonnull
  @ReturnsMutableCopy
  public static  List  getSorted (@Nonnull final Iterable  aCont,
                                                             @Nonnull final IUnidirectionalConverter  aConverter,
                                                             @Nonnull final Comparator  aComparator)
  {
    ValueEnforcer.notNull (aCont, "Container");
    ValueEnforcer.notNull (aConverter, "Converter");
    ValueEnforcer.notNull (aComparator, "Comparator");

    final List  ret = new ArrayList  ();
    for (final SRCTYPE aSrc : aCont)
      ret.add (aConverter.convert (aSrc));
    return ContainerHelper.getSortedInline (ret, aComparator);
  }

  @Nonnull
  public static  Iterator  getIterator (@Nonnull final Iterable  aCont,
                                                                   @Nonnull final IUnidirectionalConverter  aConverter)
  {
    ValueEnforcer.notNull (aCont, "Container");
    ValueEnforcer.notNull (aConverter, "Converter");

    return new Iterator  ()
    {
      private final Iterator  m_aIT = aCont.iterator ();

      public boolean hasNext ()
      {
        return m_aIT.hasNext ();
      }

      public DSTTYPE next ()
      {
        return aConverter.convert (m_aIT.next ());
      }

      public void remove ()
      {
        m_aIT.remove ();
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy