com.helger.commons.id.IHasID Java Examples

The following examples show how to use com.helger.commons.id.IHasID. 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: ChildrenProviderHasChildrenSortingTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAll ()
{
  final ChildrenProviderHasChildrenSorting <MockHasChildren> cr = new ChildrenProviderHasChildrenSorting <> (IHasID.getComparatorID ());
  assertFalse (cr.hasChildren (null));
  assertEquals (0, cr.getChildCount (null));
  assertNull (cr.getAllChildren (null));
  final MockHasChildren hca = new MockHasChildren ("a");
  final MockHasChildren hcb = new MockHasChildren ("b");
  final MockHasChildren hc1 = new MockHasChildren ("1", hcb, hca);
  assertTrue (cr.hasChildren (hc1));
  assertFalse (cr.hasChildren (hca));
  assertEquals (2, cr.getChildCount (hc1));
  assertEquals (0, cr.getChildCount (hca));
  assertNotNull (cr.getAllChildren (hc1));
  assertNotNull (cr.getAllChildren (hca));
}
 
Example #2
Source File: ChildrenProviderSortingWithIDTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAll ()
{
  final ChildrenProviderSortingWithID <String, MockHasChildren> cr = new ChildrenProviderSortingWithID <> (new MockChildrenProviderWithID (),
                                                                                                           IHasID.getComparatorID ());
  assertFalse (cr.hasChildren (null));
  assertEquals (0, cr.getChildCount (null));
  assertNull (cr.getAllChildren (null));
  final MockHasChildren hca = new MockHasChildren ("a");
  final MockHasChildren hcb = new MockHasChildren ("b");
  final MockHasChildren hc1 = new MockHasChildren ("1", hcb, hca);
  assertTrue (cr.hasChildren (hc1));
  assertFalse (cr.hasChildren (hca));
  assertEquals (2, cr.getChildCount (hc1));
  assertEquals (0, cr.getChildCount (hca));
  assertNotNull (cr.getAllChildren (hc1));
  assertNotNull (cr.getAllChildren (hca));
  assertSame (hca, cr.getChildWithID (hc1, "a"));
  assertSame (hcb, cr.getChildWithID (hc1, "b"));
  assertNull (cr.getChildWithID (hc1, "anyid"));
}
 
Example #3
Source File: ChildrenProviderSortingTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAll ()
{
  final ChildrenProviderSorting <MockHasChildren> cr = new ChildrenProviderSorting <> (new MockChildrenProvider (),
                                                                                       IHasID.getComparatorID ());
  assertFalse (cr.hasChildren (null));
  assertEquals (0, cr.getChildCount (null));
  assertNull (cr.getAllChildren (null));
  final MockHasChildren hca = new MockHasChildren ("a");
  final MockHasChildren hcb = new MockHasChildren ("b");
  final MockHasChildren hc1 = new MockHasChildren ("1", hcb, hca);
  assertTrue (cr.hasChildren (hc1));
  assertFalse (cr.hasChildren (hca));
  assertEquals (2, cr.getChildCount (hc1));
  assertEquals (0, cr.getChildCount (hca));
  assertNotNull (cr.getAllChildren (hc1));
  assertNotNull (cr.getAllChildren (hca));
}
 
Example #4
Source File: TreeWithIDBuilder.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static <KEYTYPE, DATATYPE extends IHasID <KEYTYPE>> DefaultTreeWithID <KEYTYPE, DATATYPE> buildTree (@Nonnull final IChildrenProvider <DATATYPE> aChildrenResolver)
{
  ValueEnforcer.notNull (aChildrenResolver, "ChildrenResolver");

  final DefaultTreeWithID <KEYTYPE, DATATYPE> aTree = new DefaultTreeWithID <> ();

  // get all root objects
  if (aChildrenResolver.hasChildren (null))
    for (final DATATYPE aRootObject : aChildrenResolver.getAllChildren (null))
    {
      // it is a root item
      final DefaultTreeItemWithID <KEYTYPE, DATATYPE> aItem = aTree.getRootItem ()
                                                                   .createChildItem (aRootObject.getID (),
                                                                                     aRootObject);
      _buildTreeRecursive (aItem, aChildrenResolver);
    }
  return aTree;
}
 
Example #5
Source File: ClassHierarchyCacheTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetClassHierarchy ()
{
  // Very basic class
  Collection <Class <?>> aHierarchy = ClassHierarchyCache.getClassHierarchy (Object.class);
  assertEquals (1, aHierarchy.size ());
  assertTrue (aHierarchy.contains (Object.class));

  // More sophisticated static class (no interfaces)
  aHierarchy = ClassHierarchyCache.getClassHierarchy (CGlobal.class);
  // Is usually 2, but with Cobertura enabled, it is 3!
  assertTrue (aHierarchy.size () >= 2);
  assertTrue (aHierarchy.contains (CGlobal.class));
  assertTrue (aHierarchy.contains (Object.class));

  // More sophisticated static class (with interfaces)
  aHierarchy = ClassHierarchyCache.getClassHierarchy (TypedObject.class);
  assertTrue (aHierarchy.size () >= 6);
  assertTrue (aHierarchy.contains (TypedObject.class));
  assertTrue (aHierarchy.contains (IHasObjectType.class));
  assertTrue (aHierarchy.contains (ITypedObject.class));
  assertTrue (aHierarchy.contains (IHasID.class));
  assertTrue (aHierarchy.contains (Object.class));
  assertTrue (aHierarchy.contains (Serializable.class));

  try
  {
    ClassHierarchyCache.getClassHierarchy (null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}
}
 
Example #6
Source File: TreeWithIDBuilder.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static <KEYTYPE, DATATYPE extends IHasID <KEYTYPE>> void _buildTreeRecursive (@Nullable final DefaultTreeItemWithID <KEYTYPE, DATATYPE> aParentItem,
                                                                                      @Nonnull final IChildrenProvider <DATATYPE> aChildrenResolver)
{
  if (aParentItem != null)
  {
    final DATATYPE aParentObject = aParentItem.getData ();
    if (aChildrenResolver.hasChildren (aParentObject))
      for (final DATATYPE aChild : aChildrenResolver.getAllChildren (aParentObject))
      {
        final DefaultTreeItemWithID <KEYTYPE, DATATYPE> aItem = aParentItem.createChildItem (aChild.getID (), aChild);
        _buildTreeRecursive (aItem, aChildrenResolver);
      }
  }
}
 
Example #7
Source File: DefaultTreeItemWithIDTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testReorderByKey ()
{
  final DefaultTreeWithID <String, String> t = new DefaultTreeWithID <> ();

  // root item
  final DefaultTreeItemWithID <String, String> ti = t.getRootItem ().createChildItem ("root", "Hallo");

  // no items yet....
  assertFalse (ti.hasChildren ());

  // add 2 items
  assertNotNull (ti.createChildItem ("id2", "Welt2"));
  assertNotNull (ti.createChildItem ("id1", "Welt1"));
  assertTrue (ti.hasChildren ());

  // check current order
  assertEquals (2, ti.getChildCount ());
  assertEquals ("id2", ti.getAllChildren ().get (0).getID ());
  assertEquals ("Welt2", ti.getAllChildren ().get (0).getData ());
  assertEquals ("id1", ti.getAllChildren ().get (1).getID ());
  assertEquals ("Welt1", ti.getAllChildren ().get (1).getData ());

  // reorder
  ti.reorderChildrenByItems (IHasID.getComparatorID ());

  // check new order
  assertEquals (2, ti.getChildCount ());
  assertEquals ("id1", ti.getAllChildren ().get (0).getID ());
  assertEquals ("Welt1", ti.getAllChildren ().get (0).getData ());
  assertEquals ("id2", ti.getAllChildren ().get (1).getID ());
  assertEquals ("Welt2", ti.getAllChildren ().get (1).getData ());
}
 
Example #8
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 #9
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the enum value with the passed ID
 *
 * @param <KEYTYPE>
 *        The ID type
 * @param <ENUMTYPE>
 *        The enum type
 * @param aClass
 *        The enum class
 * @param aID
 *        The ID to search
 * @param eDefault
 *        The default value to be returned, if the ID was not found.
 * @return The default parameter if no enum item with the given ID is present.
 */
@Nullable
public static <KEYTYPE, ENUMTYPE extends Enum <ENUMTYPE> & IHasID <KEYTYPE>> ENUMTYPE getFromIDOrDefault (@Nonnull final Class <ENUMTYPE> aClass,
                                                                                                          @Nullable final KEYTYPE aID,
                                                                                                          @Nullable final ENUMTYPE eDefault)
{
  ValueEnforcer.notNull (aClass, "Class");

  if (aID == null)
    return eDefault;
  return findFirst (aClass, x -> x.getID ().equals (aID), eDefault);
}
 
Example #10
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the enum value with the passed string ID (case insensitive). If no such
 * ID is present, an {@link IllegalArgumentException} is thrown.
 *
 * @param <ENUMTYPE>
 *        The enum type
 * @param aClass
 *        The enum class
 * @param sID
 *        The ID to search
 * @return The enum item with the given ID. Never <code>null</code>.
 * @throws IllegalArgumentException
 *         if no enum item with the given ID is present
 */
@Nonnull
public static <ENUMTYPE extends Enum <ENUMTYPE> & IHasID <String>> ENUMTYPE getFromIDCaseInsensitiveOrThrow (@Nonnull final Class <ENUMTYPE> aClass,
                                                                                                             @Nullable final String sID)
{
  final ENUMTYPE aEnum = getFromIDCaseInsensitiveOrNull (aClass, sID);
  if (aEnum == null)
    throw new IllegalArgumentException ("Failed to resolve ID " + sID + " within class " + aClass);
  return aEnum;
}
 
Example #11
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the enum value with the passed string ID case insensitive
 *
 * @param <ENUMTYPE>
 *        The enum type
 * @param aClass
 *        The enum class
 * @param sID
 *        The ID to search
 * @param eDefault
 *        The default value to be returned, if the ID was not found.
 * @return The default parameter if no enum item with the given ID is present.
 */
@Nullable
public static <ENUMTYPE extends Enum <ENUMTYPE> & IHasID <String>> ENUMTYPE getFromIDCaseInsensitiveOrDefault (@Nonnull final Class <ENUMTYPE> aClass,
                                                                                                               @Nullable final String sID,
                                                                                                               @Nullable final ENUMTYPE eDefault)
{
  ValueEnforcer.notNull (aClass, "Class");

  if (sID == null)
    return eDefault;
  return findFirst (aClass, x -> x.getID ().equalsIgnoreCase (sID), eDefault);
}
 
Example #12
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the enum value with the passed string ID case insensitive
 *
 * @param <ENUMTYPE>
 *        The enum type
 * @param aClass
 *        The enum class
 * @param sID
 *        The ID to search
 * @return <code>null</code> if no enum item with the given ID is present.
 */
@Nullable
public static <ENUMTYPE extends Enum <ENUMTYPE> & IHasID <String>> ENUMTYPE getFromIDCaseInsensitiveOrNull (@Nonnull final Class <ENUMTYPE> aClass,
                                                                                                            @Nullable final String sID)
{
  return getFromIDCaseInsensitiveOrDefault (aClass, sID, null);
}
 
Example #13
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the enum value with the passed ID. If no such ID is present, an
 * {@link IllegalArgumentException} is thrown.
 *
 * @param <KEYTYPE>
 *        The ID type
 * @param <ENUMTYPE>
 *        The enum type
 * @param aClass
 *        The enum class
 * @param aID
 *        The ID to search
 * @return The enum item with the given ID. Never <code>null</code>.
 * @throws IllegalArgumentException
 *         if no enum item with the given ID is present
 */
@Nonnull
public static <KEYTYPE, ENUMTYPE extends Enum <ENUMTYPE> & IHasID <KEYTYPE>> ENUMTYPE getFromIDOrThrow (@Nonnull final Class <ENUMTYPE> aClass,
                                                                                                        @Nullable final KEYTYPE aID)
{
  final ENUMTYPE aEnum = getFromIDOrNull (aClass, aID);
  if (aEnum == null)
    throw new IllegalArgumentException ("Failed to resolve ID " + aID + " within class " + aClass);
  return aEnum;
}
 
Example #14
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the enum value with the passed ID
 *
 * @param <KEYTYPE>
 *        The ID type
 * @param <ENUMTYPE>
 *        The enum type
 * @param aClass
 *        The enum class
 * @param aID
 *        The ID to search
 * @return <code>null</code> if no enum item with the given ID is present.
 */
@Nullable
public static <KEYTYPE, ENUMTYPE extends Enum <ENUMTYPE> & IHasID <KEYTYPE>> ENUMTYPE getFromIDOrNull (@Nonnull final Class <ENUMTYPE> aClass,
                                                                                                       @Nullable final KEYTYPE aID)
{
  return getFromIDOrDefault (aClass, aID, null);
}
 
Example #15
Source File: TreeWithIDBuilder.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * A generic method to build a tree of objects.
 *
 * @param <KEYTYPE>
 *        The tree key type.
 * @param <DATATYPE>
 *        The tree item value type.
 * @param aAll
 *        A linear list of objects to build the tree from. May not be
 *        <code>null</code>.
 * @return A tree with all the objects. Never <code>null</code>.
 * @throws IllegalStateException
 *         if the hierarchy cannot be determined because an object references
 *         a parent that is not in the list!
 */
@Nonnull
public static <KEYTYPE, DATATYPE extends IHasParent <DATATYPE> & IHasID <KEYTYPE>> DefaultTreeWithID <KEYTYPE, DATATYPE> buildTree (@Nonnull final Collection <? extends DATATYPE> aAll)
{
  ValueEnforcer.notNull (aAll, "All");

  return buildTree (aAll, IParentProvider.parentProviderHasParent ());
}
 
Example #16
Source File: TreeWithIDBuilder.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * A generic method to build a tree of objects.
 *
 * @param <KEYTYPE>
 *        The tree key type.
 * @param <DATATYPE>
 *        The tree item value type.
 * @param aAll
 *        A linear list of objects to build the tree from. May not be
 *        <code>null</code>.
 * @param aParentResolver
 *        The callback method to determine the parental object of a given
 *        object. May not be <code>null</code>.
 * @return A tree with all the objects. Never <code>null</code>.
 * @throws IllegalStateException
 *         if the hierarchy cannot be determined because an object references
 *         a parent that is not in the list!
 */
@Nonnull
public static <KEYTYPE, DATATYPE extends IHasID <KEYTYPE>> DefaultTreeWithID <KEYTYPE, DATATYPE> buildTree (@Nonnull final DATATYPE [] aAll,
                                                                                                            @Nonnull final IParentProvider <DATATYPE> aParentResolver)
{
  ValueEnforcer.notNull (aAll, "All");
  ValueEnforcer.notNull (aParentResolver, "ParentResolver");

  return _buildTree (new CommonsArrayList <> (aAll), aParentResolver);
}
 
Example #17
Source File: TreeWithIDBuilder.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * A generic method to build a tree of objects.
 *
 * @param <KEYTYPE>
 *        The tree key type.
 * @param <DATATYPE>
 *        The tree item value type.
 * @param aAll
 *        A linear list of objects to build the tree from. May not be
 *        <code>null</code>.
 * @param aParentResolver
 *        The callback method to determine the parental object of a given
 *        object. May not be <code>null</code>.
 * @return A tree with all the objects. Never <code>null</code>.
 * @throws IllegalStateException
 *         if the hierarchy cannot be determined because an object references
 *         a parent that is not in the list!
 */
@Nonnull
public static <KEYTYPE, DATATYPE extends IHasID <KEYTYPE>> DefaultTreeWithID <KEYTYPE, DATATYPE> buildTree (@Nonnull final Collection <? extends DATATYPE> aAll,
                                                                                                            @Nonnull final IParentProvider <DATATYPE> aParentResolver)
{
  ValueEnforcer.notNull (aAll, "All");
  ValueEnforcer.notNull (aParentResolver, "ParentResolver");

  return _buildTree (new CommonsArrayList <> (aAll), aParentResolver);
}
 
Example #18
Source File: TreeXMLConverter.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Specialized conversion method for converting a tree with ID to a
 * standardized XML tree.
 *
 * @param <DATATYPE>
 *        tree item value type
 * @param <ITEMTYPE>
 *        tree item type
 * @param aTree
 *        The tree to be converted
 * @param aConverter
 *        The main data converter that converts the tree item values into XML
 * @return The created document.
 */
@Nonnull
public static <DATATYPE, ITEMTYPE extends ITreeItemWithID <String, DATATYPE, ITEMTYPE>> IMicroElement getTreeWithStringIDAsXML (@Nonnull final IBasicTree <DATATYPE, ITEMTYPE> aTree,
                                                                                                                                @Nonnull final IConverterTreeItemToMicroNode <? super DATATYPE> aConverter)
{
  return getTreeWithIDAsXML (aTree, IHasID.getComparatorID (), x -> x, aConverter);
}
 
Example #19
Source File: TreeWithIDSorter.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Sort each level of the passed tree on the ID with the specified comparator.
 *
 * @param <KEYTYPE>
 *        Tree item key type
 * @param <DATATYPE>
 *        Tree item data type
 * @param <ITEMTYPE>
 *        Tree item type
 * @param aTree
 *        The tree to be sorted.
 * @param aKeyComparator
 *        The comparator to be used for sorting the tree item keys on each
 *        level.
 */
public static <KEYTYPE, DATATYPE, ITEMTYPE extends ITreeItemWithID <KEYTYPE, DATATYPE, ITEMTYPE>> void sortByID (@Nonnull final IBasicTree <DATATYPE, ITEMTYPE> aTree,
                                                                                                                 @Nonnull final Comparator <? super KEYTYPE> aKeyComparator)
{
  _sort (aTree, Comparator.comparing (IHasID::getID, aKeyComparator));
}
 
Example #20
Source File: TreeWithIDSorter.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Sort each level of the passed tree on the ID with the specified comparator.
 * This method assumes that the IDs in the tree item implement the
 * {@link Comparable} interface.
 *
 * @param <KEYTYPE>
 *        Tree item key type
 * @param <DATATYPE>
 *        Tree item data type
 * @param <ITEMTYPE>
 *        Tree item type
 * @param aTree
 *        The tree to be sorted.
 */
public static <KEYTYPE extends Comparable <? super KEYTYPE>, DATATYPE, ITEMTYPE extends ITreeItemWithID <KEYTYPE, DATATYPE, ITEMTYPE>> void sortByID (@Nonnull final IBasicTree <DATATYPE, ITEMTYPE> aTree)
{
  _sort (aTree, IHasID.getComparatorID ());
}