Java Code Examples for com.helger.commons.collection.ArrayHelper#newArray()

The following examples show how to use com.helger.commons.collection.ArrayHelper#newArray() . 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: EqualsHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testArray ()
{
  final String [] aArray = ArrayHelper.newArray ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aArray, aArray));
  assertTrue (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "c")));
  assertTrue (EqualsHelper.equalsCollection (new String [0], new String [] {}));

  assertFalse (EqualsHelper.equalsCollection (aArray, new String [0]));
  assertFalse (EqualsHelper.equalsCollection (new String [0], aArray));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("A", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "B", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "C")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsHashSet <> ("a", "b", "c")));
}
 
Example 2
Source File: ArrayIteratorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testStdMethods ()
{
  final ArrayIterator <String> ae = new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                "Welt",
                                                                                "from",
                                                                                "Copenhagen"));
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (ae,
                                                                     new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                                                 "Welt",
                                                                                                                 "from",
                                                                                                                 "Copenhagen")));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (ae,
                                                                         new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                                                     "Welt",
                                                                                                                     "from")));
  ae.next ();
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (ae,
                                                                         new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                                                     "Welt",
                                                                                                                     "from",
                                                                                                                     "Copenhagen")));
}
 
Example 3
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayComplex ()
{
  final ICommonsList <String> [] aArray = ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"), new CommonsArrayList <> ("c", "d"));
  assertTrue (EqualsHelper.equalsCollection (aArray, aArray));
  assertTrue (EqualsHelper.equalsCollection (aArray,
                                             ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"),
                                                                   new CommonsArrayList <> ("c", "d"))));
  assertTrue (EqualsHelper.equalsCollection (new ICommonsList <?> [0], new ICommonsList <?> [] {}));

  assertFalse (EqualsHelper.equalsCollection (aArray, new ICommonsList <?> [0]));
  assertFalse (EqualsHelper.equalsCollection (new ICommonsList <?> [0], aArray));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("A", "b"),
                                                                    new CommonsArrayList <> ("c", "d"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"),
                                                                    new CommonsArrayList <> ("c", "D"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"),
                                                                    new CommonsArrayList <> ("c", "d"),
                                                                    new CommonsArrayList <> ("e", "f"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"), (ICommonsList <String>) null)));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", null, "c")));
}
 
Example 4
Source File: IterableIteratorTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasic ()
{
  assertSame (IterableIterator.createEmpty (), IterableIterator.createEmpty ());
  IIterableIterator <String> iit = new IterableIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                  "Welt",
                                                                                  "from",
                                                                                  "Copenhagen"));
  assertNotNull (iit);
  assertNotNull (iit.iterator ());
  assertTrue (iit.hasNext ());
  assertEquals ("Hallo", iit.next ());

  iit = new IterableIterator <> (CollectionHelper.newList ("Hallo", "Welt", "from", "Copenhagen"));
  iit.next ();
  iit.remove ();

  assertEquals (3, CollectionHelper.newList (new IterableIterator <> (new String [] { "a", "b", "c" })).size ());
  assertEquals (3,
                CollectionHelper.newList (new IterableIterator <> (CollectionHelper.newList ("a", "b", "c")))
                                .size ());
  assertEquals (3,
                CollectionHelper.newList (new IterableIterator <> (CollectionHelper.newList ("a", "b", "c")
                                                                                   .iterator ()))
                                .size ());
  CommonsTestHelper.testToStringImplementation (iit);

  try
  {
    new IterableIterator <> ((Iterator <String>) null);
    fail ();
  }
  catch (final NullPointerException ex)
  {}
}
 
Example 5
Source File: CommonsMock.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
private Object _mock (@Nonnull final Class <?> aClass,
                      @Nullable final Object [] aParams,
                      final int nLevel) throws Exception
{
  // Check for static supplier
  final MockSupplier aStatic = s_aStaticSupplier.get (aClass);
  if (aStatic != null)
    return aStatic.getMockedValue (aParams);

  // Check for per-instance supplier
  final MockSupplier aInstance = m_aPerInstanceSupplier.get (aClass);
  if (aInstance != null)
    return aInstance.getMockedValue (aParams);

  // Is it an array?
  if (aClass.isArray ())
  {
    final Class <?> aArrayType = aClass.getComponentType ();

    if (aArrayType == boolean.class)
      return ArrayHelper.newBooleanArray ();
    if (aArrayType == byte.class)
      return ArrayHelper.newByteArray ();
    if (aArrayType == char.class)
      return ArrayHelper.newCharArray ();
    if (aArrayType == double.class)
      return ArrayHelper.newDoubleArray ();
    if (aArrayType == float.class)
      return ArrayHelper.newFloatArray ();
    if (aArrayType == int.class)
      return ArrayHelper.newIntArray ();
    if (aArrayType == long.class)
      return ArrayHelper.newLongArray ();
    if (aArrayType == short.class)
      return ArrayHelper.newShortArray ();

    final Object [] ret = ArrayHelper.newArray (aArrayType, 1);
    ret[0] = _mock (aArrayType, null, nLevel + 1);
    return ret;
  }

  // As enums have no constructors use the first enum constant
  if (aClass.isEnum ())
  {
    return aClass.getEnumConstants ()[0];
  }

  // Find constructor
  for (final Constructor <?> c : aClass.getConstructors ())
  {
    try
    {
      // c.setAccessible (true);
      final Object [] aCtorParams = new Object [c.getParameterCount ()];
      int nParam = 0;
      for (final Class <?> aParamClass : c.getParameterTypes ())
      {
        // Avoid infinite recursion
        if (EqualsHelper.identityEqual (aParamClass, aClass))
          aCtorParams[nParam++] = null;
        else
          aCtorParams[nParam++] = _mock (aParamClass, null, nLevel + 1);
      }
      return c.newInstance (aCtorParams);
    }
    catch (final Exception ex)
    {
      // continue to exception below
    }
  }

  // Ooops
  throw new IllegalStateException ("Class " + aClass.getName () + " has no mockable constructor!");
}
 
Example 6
Source File: MainCreateJAXBBinding22.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final List <String> x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}
 
Example 7
Source File: MainCreateJAXBBinding21.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final List <String> x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}
 
Example 8
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final ICommonsList <String> x = StringHelper.getExploded ('.', sHost);
    x.reverse ();

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}
 
Example 9
Source File: MainCreateJAXBBinding23.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String _convertToPackage (@Nonnull final String sNamespaceURI)
{
  // Lowercase everything
  String s = sNamespaceURI.toLowerCase (Locale.US);

  String [] aParts;
  final URL aURL = URLHelper.getAsURL (sNamespaceURI);
  if (aURL != null)
  {
    // Host
    String sHost = aURL.getHost ();

    // Kick static prefix: www.helger.com -> helger.com
    sHost = StringHelper.trimStart (sHost, "www.");

    // Reverse domain: helger.com -> com.helger
    final List <String> x = CollectionHelper.getReverseList (StringHelper.getExploded ('.', sHost));

    // Path in regular order:
    final String sPath = StringHelper.trimStart (aURL.getPath (), '/');
    x.addAll (StringHelper.getExploded ('/', sPath));

    // Convert to array
    aParts = ArrayHelper.newArray (x, String.class);
  }
  else
  {
    // Kick known prefixes
    for (final String sPrefix : new String [] { "urn:", "http://" })
      if (s.startsWith (sPrefix))
      {
        s = s.substring (sPrefix.length ());
        break;
      }

    // Replace all illegal characters
    s = StringHelper.replaceAll (s, ':', '.');
    s = StringHelper.replaceAll (s, '-', '_');
    aParts = StringHelper.getExplodedArray ('.', s);
  }

  // Split into pieces and replace all illegal package parts (e.g. only
  // numeric) with valid ones
  for (int i = 0; i < aParts.length; ++i)
    aParts[i] = RegExHelper.getAsIdentifier (aParts[i]);

  return StringHelper.getImploded (".", aParts);
}