Java Code Examples for com.helger.commons.lang.ClassHelper#isArrayClass()

The following examples show how to use com.helger.commons.lang.ClassHelper#isArrayClass() . 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
@Nullable
public static ECollectionBaseType getCollectionBaseTypeOfClass (@Nullable final Class <?> aClass)
{
  if (aClass != null)
  {
    // Query Set before Collection, because Set is derived from Collection!
    if (Set.class.isAssignableFrom (aClass))
      return ECollectionBaseType.SET;
    if (Collection.class.isAssignableFrom (aClass))
      return ECollectionBaseType.COLLECTION;
    if (Map.class.isAssignableFrom (aClass))
      return ECollectionBaseType.MAP;
    if (ClassHelper.isArrayClass (aClass))
      return ECollectionBaseType.ARRAY;
    if (Iterator.class.isAssignableFrom (aClass))
      return ECollectionBaseType.ITERATOR;
    if (Iterable.class.isAssignableFrom (aClass))
      return ECollectionBaseType.ITERABLE;
    if (Enumeration.class.isAssignableFrom (aClass))
      return ECollectionBaseType.ENUMERATION;
  }
  return null;
}
 
Example 2
Source File: EqualsImplementationRegistry.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public <T> IEqualsImplementation <T> getBestMatchingEqualsImplementation (@Nullable final Class <T> aClass)
{
  if (aClass != null)
  {
    IEqualsImplementation <T> aMatchingImplementation = null;
    Class <?> aMatchingClass = null;

    // No check required?
    if (_isUseDirectEquals (aClass))
      return null;

    m_aRWLock.readLock ().lock ();
    try
    {
      // Check for an exact match first
      aMatchingImplementation = GenericReflection.uncheckedCast (m_aMap.get (aClass));
      if (aMatchingImplementation != null)
        aMatchingClass = aClass;
      else
      {
        // Scan hierarchy in most efficient way
        for (final WeakReference <Class <?>> aCurWRClass : ClassHierarchyCache.getClassHierarchyIterator (aClass))
        {
          final Class <?> aCurClass = aCurWRClass.get ();
          if (aCurClass != null)
          {
            final IEqualsImplementation <?> aImpl = m_aMap.get (aCurClass);
            if (aImpl != null)
            {
              aMatchingImplementation = GenericReflection.uncheckedCast (aImpl);
              aMatchingClass = aCurClass;
              if (LOGGER.isDebugEnabled ())
                LOGGER.debug ("Found hierarchical match with class " + aMatchingClass + " when searching for " + aClass);
              break;
            }
          }
        }
      }
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }

    // Do this outside of the lock for performance reasons
    if (aMatchingImplementation != null)
    {
      // If the matching implementation is for an interface and the
      // implementation class implements equals, use the one from the class
      // Example: a converter for "Map" is registered, but "LRUCache" comes
      // with its own "equals" implementation
      if (aMatchingImplementation.implementationEqualsOverridesInterface () &&
          ClassHelper.isInterface (aMatchingClass) &&
          _implementsEqualsItself (aClass))
      {
        // Remember to use direct implementation
        m_aDirectEquals.setAnnotation (aClass, true);
        return null;
      }

      if (!aMatchingClass.equals (aClass))
      {
        // We found a match by walking the hierarchy -> put that match in the
        // direct hit list for further speed up
        registerEqualsImplementation (aClass, aMatchingImplementation);
      }

      return aMatchingImplementation;
    }

    // Handle arrays specially, because we cannot register a converter for
    // every potential array class (but we allow for special implementations)
    if (ClassHelper.isArrayClass (aClass))
      return GenericReflection.uncheckedCast (new ArrayEqualsImplementation ());

    // Remember to use direct implementation
    m_aDirectEquals.setAnnotation (aClass, true);
  }

  // No special handler found
  if (LOGGER.isTraceEnabled ())
    LOGGER.trace ("Found no equals implementation for " + aClass);

  // Definitely no special implementation
  return null;
}
 
Example 3
Source File: HashCodeImplementationRegistry.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public <T> IHashCodeImplementation <T> getBestMatchingHashCodeImplementation (@Nullable final Class <T> aClass)
{
  if (aClass != null)
  {
    IHashCodeImplementation <T> aMatchingImplementation = null;
    Class <?> aMatchingClass = null;

    // No check required?
    if (_isUseDirectHashCode (aClass))
      return null;

    m_aRWLock.readLock ().lock ();
    try
    {
      // Check for an exact match first
      aMatchingImplementation = GenericReflection.uncheckedCast (m_aMap.get (aClass));
      if (aMatchingImplementation != null)
        aMatchingClass = aClass;
      else
      {
        // Scan hierarchy in efficient way
        for (final WeakReference <Class <?>> aCurWRClass : ClassHierarchyCache.getClassHierarchyIterator (aClass))
        {
          final Class <?> aCurClass = aCurWRClass.get ();
          if (aCurClass != null)
          {
            final IHashCodeImplementation <?> aImpl = m_aMap.get (aCurClass);
            if (aImpl != null)
            {
              aMatchingImplementation = GenericReflection.uncheckedCast (aImpl);
              aMatchingClass = aCurClass;
              if (LOGGER.isDebugEnabled ())
                LOGGER.debug ("Found hierarchical match with class " +
                              aMatchingClass +
                              " when searching for " +
                              aClass);
              break;
            }
          }
        }
      }
    }
    finally
    {
      m_aRWLock.readLock ().unlock ();
    }

    // Do this outside of the lock for performance reasons
    if (aMatchingImplementation != null)
    {
      // If the matching implementation is for an interface and the
      // implementation class implements hashCode, use the one from the class
      // Example: a converter for "Map" is registered, but "LRUCache" comes
      // with its own "hashCode" implementation
      if (ClassHelper.isInterface (aMatchingClass) && _implementsHashCodeItself (aClass))
      {
        // Remember to use direct implementation
        m_aDirectHashCode.setAnnotation (aClass, true);
        return null;
      }

      if (!aMatchingClass.equals (aClass))
      {
        // We found a match by walking the hierarchy -> put that match in the
        // direct hit list for further speed up
        registerHashCodeImplementation (aClass, aMatchingImplementation);
      }

      return aMatchingImplementation;
    }

    // Handle arrays specially, because we cannot register a converter for
    // every potential array class (but we allow for special implementations)
    if (ClassHelper.isArrayClass (aClass))
      return x -> Arrays.deepHashCode ((Object []) x);

    // Remember to use direct implementation
    m_aDirectHashCode.setAnnotation (aClass, true);
  }

  // No special handler found
  if (LOGGER.isTraceEnabled ())
    LOGGER.trace ("Found no hashCode implementation for " + aClass);

  // Definitely no special implementation
  return null;
}
 
Example 4
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Check if the passed object is an array or not.
 *
 * @param aObject
 *        The object to be checked. May be <code>null</code>.
 * @return <code>true</code> if the passed object is not <code>null</code> and
 *         represents an array.
 */
public static boolean isArray (@Nullable final Object aObject)
{
  return aObject != null && ClassHelper.isArrayClass (aObject.getClass ());
}