com.helger.commons.collection.impl.CommonsArrayList Java Examples

The following examples show how to use com.helger.commons.collection.impl.CommonsArrayList. 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 testList ()
{
  final ICommonsList <String> aCont = new CommonsArrayList <> ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aCont, aCont));
  assertTrue (EqualsHelper.equalsCollection (aCont, CollectionHelper.makeUnmodifiable (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, Collections.synchronizedList (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new CommonsLinkedList <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new CommonsVector <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new NonBlockingStack <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (new CommonsArrayList <String> (), new CommonsLinkedList <String> ()));
  assertTrue (EqualsHelper.equalsCollection (new NonBlockingStack <String> (), new CommonsVector <String> ()));
  assertTrue (EqualsHelper.equalsCollection (new NonBlockingStack <String> (), new Stack <String> ()));

  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsLinkedList <String> ()));
  assertFalse (EqualsHelper.equalsCollection (new CommonsLinkedList <String> (), aCont));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <String> ()));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> ("A", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> ("a", "B", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> ("a", "b", "C")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, ArrayHelper.newArray ("a", "b", "c")));
}
 
Example #2
Source File: NonBlockingStackTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testCtor ()
{
  NonBlockingStack <String> st = new NonBlockingStack <> ();
  assertTrue (st.isEmpty ());
  assertFalse (st.iterator ().hasNext ());
  st = new NonBlockingStack <> ("s", "t");
  assertEquals (2, st.size ());
  assertEquals ("s", st.firstElement ());
  assertTrue (st.iterator ().hasNext ());
  st = new NonBlockingStack <> ((String []) null);
  assertTrue (st.isEmpty ());
  assertFalse (st.iterator ().hasNext ());
  st = new NonBlockingStack <> (new CommonsArrayList <> ("s", "t"));
  assertEquals (2, st.size ());
  assertEquals ("s", st.firstElement ());
  assertTrue (st.iterator ().hasNext ());
  st = new NonBlockingStack <> (new CommonsHashSet <String> ());
  assertTrue (st.isEmpty ());
  assertFalse (st.iterator ().hasNext ());
  st = new NonBlockingStack <> (new NonBlockingStack <> ("s", "t"));
  assertEquals (2, st.size ());
  assertEquals ("s", st.firstElement ());
  assertTrue (st.iterator ().hasNext ());
}
 
Example #3
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a sublist excerpt of the passed list.
 *
 * @param <ELEMENTTYPE>
 *        Type of elements in list
 * @param aCont
 *        The backing list. May not be <code>null</code>.
 * @param nStartIndex
 *        The start index to use. Needs to be &ge; 0.
 * @param nSectionLength
 *        the length of the desired subset. If list is shorter than that,
 *        aIter will return a shorter section
 * @return The specified section of the passed list, or a shorter list if
 *         nStartIndex + nSectionLength is an invalid index. Never
 *         <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE> CommonsArrayList <ELEMENTTYPE> getSubList (@Nullable final List <ELEMENTTYPE> aCont,
                                                                       @Nonnegative final int nStartIndex,
                                                                       @Nonnegative final int nSectionLength)
{
  ValueEnforcer.isGE0 (nStartIndex, "StartIndex");
  ValueEnforcer.isGE0 (nSectionLength, "SectionLength");

  final int nSize = getSize (aCont);
  if (nSize == 0 || nStartIndex >= nSize)
    return newList (0);

  int nEndIndex = nStartIndex + nSectionLength;
  if (nEndIndex > nSize)
    nEndIndex = nSize;

  // Create a copy of the list because "subList" only returns a view of the
  // original list!
  return newList (aCont.subList (nStartIndex, nEndIndex));
}
 
Example #4
Source File: StringTrieFuncTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public ICommonsCollection <String> prefixMatch (@Nonnull @Nonempty final String sPrefix)
{
  if (StringHelper.hasNoText (sPrefix))
    throw new IllegalArgumentException ("prefix must have length >= 1");

  final ICommonsList <String> aList = new CommonsArrayList <> ();
  final char [] aPrefix = sPrefix.toCharArray ();
  final Node <DATATYPE> aNode = _get (m_aRoot, aPrefix, 0);
  if (aNode != null)
  {
    if (aNode.m_aValue != null)
      aList.add (sPrefix);
    _collect (aNode.m_aMid, sPrefix, aList);
  }
  return aList;
}
 
Example #5
Source File: IAggregatorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseLast ()
{
  final IAggregator <String, String> a1 = CollectionHelper::getLastElement;
  assertEquals (a1, a1);
  assertNotEquals (a1, null);
  assertNotEquals (a1, "any other");
  assertEquals (a1.hashCode (), a1.hashCode ());
  assertNotEquals (a1.hashCode (), 0);
  assertNotEquals (a1.hashCode (), "any other".hashCode ());
  assertNotNull (a1.toString ());
  final ICommonsList <String> l = new CommonsArrayList <> ("a", null, "b", "", "c");
  assertEquals ("c", a1.apply (l));
  assertNull (a1.apply (new CommonsArrayList <String> ()));
  assertNull (a1.apply ((Collection <String>) null));
}
 
Example #6
Source File: CombinationGeneratorFlexibleTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Ignore ("Simply wrong assumption")
@Test
public void testRedundancy ()
{
  final ICommonsList <String> aInputList = new CommonsArrayList <> ("a", "b", "c", "d", "e", "f", "g", "h");

  // Build all permutations of the input list, using all available slots
  final ICommonsSet <ICommonsList <String>> aSimplePermutations = new CommonsHashSet <> ();
  CombinationGenerator.addAllPermutations (aInputList, aInputList.size (), aSimplePermutations);

  // Flexible combination generator
  final ICommonsSet <ICommonsList <String>> aFlexiblePermutations = CombinationGeneratorFlexible.getCombinations (aInputList,
                                                                                                                  true);
  assertTrue (aFlexiblePermutations.size () >= aSimplePermutations.size ());

  // Now the assumptions: I assume that all permutations from the flexible
  // generator are also contained in all permutations
  for (final ICommonsList <String> aList : aFlexiblePermutations)
    assertTrue (aList.toString (), aSimplePermutations.contains (aList));
}
 
Example #7
Source File: ArrayHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewArrayFromCollection ()
{
  String [] x = newArray (CollectionHelper.newList ("s1", "s2", "s3"), String.class);
  assertNotNull (x);
  assertEquals (3, x.length);

  x = newArray (new CommonsArrayList <String> (), String.class);
  assertNotNull (x);

  x = newArray ((List <String>) null, String.class);
  assertNotNull (x);

  CharSequence [] y = newArray (CollectionHelper.newList ("s1", "s2", "s3"), CharSequence.class);
  assertNotNull (y);
  assertEquals (3, y.length);

  y = newArray (CollectionHelper.newSet ("s1", "s2", "s3"), CharSequence.class);
  assertNotNull (y);
  assertEquals (3, y.length);
}
 
Example #8
Source File: CmdLineParserTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testValuesValueSep () throws CmdLineParseException
{
  final Options aOptions = new Options ().addOption (Option.builder ("D")
                                                           .args (2)
                                                           .valueSeparator ('=')
                                                           .repeatable (false)
                                                           .required (true)
                                                           .build ());
  final CmdLineParser p = new CmdLineParser (aOptions);

  // With value separator
  ParsedCmdLine aPCL = p.parse (new String [] { "-D", "name=value" });
  assertTrue (aPCL.hasOption ("D"));
  assertEquals ("name", aPCL.getValue ("D"));
  assertEquals (new CommonsArrayList <> ("name", "value"), aPCL.values ("D"));

  // Without value separator
  aPCL = p.parse (new String [] { "-D", "name", "value" });
  assertTrue (aPCL.hasOption ("D"));
  assertEquals ("name", aPCL.getValue ("D"));
  assertEquals (new CommonsArrayList <> ("name", "value"), aPCL.values ("D"));
}
 
Example #9
Source File: CmdLineParserTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseUndefinedActions () throws CmdLineParseException
{
  final Options aOptions = new Options ().addOption (Option.builder ("D")
                                                           .longOpt ("def")
                                                           .args (2)
                                                           .valueSeparator ('=')
                                                           .build ());
  final CmdLineParser p = new CmdLineParser (aOptions);

  ParsedCmdLine aPCL = p.parse (new String [] { "-foo", "-bar" });
  assertFalse (aPCL.hasOption ("D"));

  aPCL = p.parse (new String [] { "-Da=b", "-foo", "-bar" });
  assertTrue (aPCL.hasOption ("D"));
  assertEquals (new CommonsArrayList <> ("a", "b"), aPCL.values ("D"));

  aPCL = p.parse (new String [] { "-D", "-foo", "-bar" });
  assertTrue (aPCL.hasOption ("D"));
  assertEquals (new CommonsArrayList <> ("-foo", "-bar"), aPCL.values ("D"));
}
 
Example #10
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testNewListIIterableIterator ()
{
  final ICommonsList <String> aSource = newList ("Hallo", "Welt", "from", "Vienna");

  ICommonsList <String> aList = newList (new IterableIterator <> (aSource));
  assertNotNull (aList);
  assertEquals (4, aList.size ());
  assertTrue (aList.contains ("Hallo"));
  assertTrue (aList.contains ("Welt"));
  assertTrue (aList.contains ("from"));
  assertTrue (aList.contains ("Vienna"));

  aList = newList (new IterableIterator <> (new CommonsArrayList <String> ()));
  assertNotNull (aList);

  aList = newList ((IIterableIterator <String>) null);
  assertNotNull (aList);
}
 
Example #11
Source File: MainBenchmarkIsValidSchematron.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
public static void main (final String [] args)
{
  logSystemInfo ();

  final ICommonsList <IReadableResource> aValidSchematrons = new CommonsArrayList <> ();
  for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles ())
    if (!aRes.getPath ().endsWith ("/BIICORE-UBL-T01.sch") &&
        !aRes.getPath ().endsWith ("/BIICORE-UBL-T10.sch") &&
        !aRes.getPath ().endsWith ("/BIICORE-UBL-T14.sch") &&
        !aRes.getPath ().endsWith ("/BIICORE-UBL-T15.sch") &&
        !aRes.getPath ().endsWith ("/CellarBook.sch") &&
        !aRes.getPath ().endsWith ("/pattern-example-with-includes.sch") &&
        !aRes.getPath ().endsWith ("/pattern-example.sch") &&
        !aRes.getPath ().endsWith ("/schematron-svrl.sch"))
      aValidSchematrons.add (aRes);

  LOGGER.info ("Starting");

  final double dTime1 = benchmarkTask (new ValidSchematronPure (aValidSchematrons));
  LOGGER.info ("Time pure: " + BigDecimal.valueOf (dTime1).toString () + " us");

  final double dTime2 = benchmarkTask (new ValidSchematronSCH (aValidSchematrons));
  LOGGER.info ("Time XSLT: " + BigDecimal.valueOf (dTime2).toString () + " us");

  LOGGER.info ("Time1 is " + BigDecimal.valueOf (dTime1 / dTime2 * 100).toString () + "% of time2");
}
 
Example #12
Source File: TreeWithIDSearcher.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Fill all items with the same ID by linearly scanning the tree.
 *
 * @param <KEYTYPE>
 *        tree ID type
 * @param <DATATYPE>
 *        tree data type
 * @param <ITEMTYPE>
 *        tree item type
 * @param aTreeItem
 *        The tree item to search. May not be <code>null</code>.
 * @param aSearchID
 *        The ID to search. May not be <code>null</code>.
 * @return A non-<code>null</code> list with all matching items.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, DATATYPE, ITEMTYPE extends ITreeItemWithID <KEYTYPE, DATATYPE, ITEMTYPE>> ICommonsList <ITEMTYPE> findAllItemsWithIDRecursive (@Nonnull final ITEMTYPE aTreeItem,
                                                                                                                                                       @Nullable final KEYTYPE aSearchID)
{
  final ICommonsList <ITEMTYPE> aRetList = new CommonsArrayList <> ();
  TreeVisitor.visitTreeItem (aTreeItem, new DefaultHierarchyVisitorCallback <ITEMTYPE> ()
  {
    @Override
    @Nonnull
    public EHierarchyVisitorReturn onItemBeforeChildren (@Nullable final ITEMTYPE aItem)
    {
      if (aItem != null && aItem.getID ().equals (aSearchID))
        aRetList.add (aItem);
      return EHierarchyVisitorReturn.CONTINUE;
    }
  });
  return aRetList;
}
 
Example #13
Source File: IGetterByIndexTrait.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get a list of all attribute values with the same name.
 *
 * @param nIndex
 *        The index to be accessed. Should be &ge; 0.
 * @param aDefault
 *        The default value to be returned, if no such attribute is present.
 * @return <code>aDefault</code> if no such attribute value exists
 */
@Nullable
default ICommonsList <String> getAsStringList (@Nonnegative final int nIndex,
                                               @Nullable final ICommonsList <String> aDefault)
{
  final Object aValue = getValue (nIndex);
  if (aValue != null)
  {
    if (aValue instanceof String [])
    {
      // multiple values passed in the request
      return new CommonsArrayList <> ((String []) aValue);
    }
    if (aValue instanceof String)
    {
      // single value passed in the request
      return new CommonsArrayList <> ((String) aValue);
    }
  }
  return aDefault;
}
 
Example #14
Source File: CSVReaderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Tests iterating over a reader.
 *
 * @throws IOException
 *         if the reader fails.
 */
@Test
public void testIteratorFunctionality () throws IOException
{
  final ICommonsList <ICommonsList <String>> expectedResult = new CommonsArrayList <> ();
  expectedResult.add (CollectionHelper.newList ("a", "b", "aReader"));
  expectedResult.add (CollectionHelper.newList ("a", "b,b,b", "aReader"));
  expectedResult.add (CollectionHelper.newList ("", "", ""));
  expectedResult.add (CollectionHelper.newList ("a", "PO Box 123,\nKippax,ACT. 2615.\nAustralia", "d."));
  expectedResult.add (CollectionHelper.newList ("Glen \"The Man\" Smith", "Athlete", "Developer"));
  expectedResult.add (CollectionHelper.newList ("\"\"", "test"));
  expectedResult.add (CollectionHelper.newList ("a\nb", "b", "\nd", "e"));
  int idx = 0;
  try (final CSVReader aReader = _createCSVReader ())
  {
    for (final ICommonsList <String> line : aReader)
    {
      final ICommonsList <String> expectedLine = expectedResult.get (idx++);
      assertEquals (expectedLine, line);
    }
  }
}
 
Example #15
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressFBWarnings ("NP_NONNULL_PARAM_VIOLATION")
public void testNewSortedSetIIterableIterator ()
{
  SortedSet <String> aSet = newSortedSet (new IterableIterator <> (newList ("Hallo", "Welt", null)));
  assertNotNull (aSet);
  assertEquals (3, aSet.size ());
  assertNull (aSet.first ());
  assertTrue (aSet.contains ("Hallo"));
  assertTrue (aSet.contains ("Welt"));
  assertTrue (aSet.contains (null));

  aSet = newSortedSet (new IterableIterator <> (new CommonsArrayList <String> ()));
  assertNotNull (aSet);
  assertEquals (0, aSet.size ());
}
 
Example #16
Source File: XMLCharsetDeterminatorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllCharsetsDoubleQuotes ()
{
  for (final Charset c : XMLCharsetDeterminator.getAllSupportedCharsets ())
  {
    final ICommonsList <String> aNames = new CommonsArrayList <> (c.name ());
    aNames.addAll (c.aliases ());
    for (final String sAlias : aNames)
    {
      final String sXML = "<?xml version=\"1.0\" encoding=\"" + sAlias + "\"?><!-- " + c.name () + " --><root />";
      if (false)
        LOGGER.info (sXML);
      final byte [] aBytes = sXML.getBytes (c);
      final Charset aDetermined = XMLCharsetDeterminator.determineXMLCharset (aBytes);
      assertEquals (c, aDetermined);
    }
  }
}
 
Example #17
Source File: AbstractConcurrentCollector.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public final ICommonsList <DATATYPE> drainQueue ()
{
  // Drain all objects to this queue
  final ICommonsList <Object> aDrainedToList = new CommonsArrayList <> ();
  m_aRWLock.writeLockedInt ( () -> m_aQueue.drainTo (aDrainedToList));

  // Change data type
  final ICommonsList <DATATYPE> ret = new CommonsArrayList <> ();
  for (final Object aObj : aDrainedToList)
    if (!EqualsHelper.identityEqual (aObj, STOP_QUEUE_OBJECT))
      ret.add (GenericReflection.uncheckedCast (aObj));
    else
    {
      // Re-add the stop object, because loops in derived classes rely on this
      // object
      m_aRWLock.writeLockedBoolean ( () -> m_aQueue.add (aObj));
    }
  return ret;
}
 
Example #18
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE> CommonsArrayList <ELEMENTTYPE> newList (@Nullable final ELEMENTTYPE aValue)
{
  final CommonsArrayList <ELEMENTTYPE> ret = newList (1);
  ret.add (aValue);
  return ret;
}
 
Example #19
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplex ()
{
  final ICommonsMap <ICommonsList <String>, ICommonsSet <String>> aMap = new CommonsHashMap <> ();
  aMap.put (new CommonsArrayList <> ("a", "b", "c"), new CommonsHashSet <> ("a", "b", "c"));
  aMap.put (new CommonsArrayList <> ("a", "b", "d"), new CommonsHashSet <> ("a", "b", "d"));
  assertTrue (EqualsHelper.equalsCollection (aMap, CollectionHelper.newMap (aMap)));

  assertFalse (EqualsHelper.equalsCollection (aMap, ArrayHelper.newArray ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsHashSet <> ("a", "b", "c")));
  final ICommonsMap <String, String> aMap1a = new CommonsHashMap <> ();
  aMap1a.put ("a", "b");
  assertFalse (EqualsHelper.equalsCollection (aMap, aMap1a));
  final ICommonsMap <ICommonsList <String>, String> aMap2 = new CommonsHashMap <> ();
  aMap2.put (new CommonsArrayList <> ("a", "b", "c"), "d");
  aMap2.put (new CommonsArrayList <> ("a", "b", "d"), "e");
  aMap2.put (new CommonsArrayList <> ("a", "b", "e"), null);
  aMap2.put (null, "g");
  assertFalse (EqualsHelper.equalsCollection (aMap, aMap2));
  assertFalse (EqualsHelper.equalsCollection (aMap2, aMap));
  final ICommonsMap <String, ICommonsList <String>> aMap3 = new CommonsHashMap <> ();
  aMap3.put ("d", new CommonsArrayList <> ("a", "b", "c"));
  aMap3.put ("e", new CommonsArrayList <> ("a", "b", "d"));
  aMap3.put (null, new CommonsArrayList <> ("a", "b", "e"));
  aMap3.put ("g", null);
  assertFalse (EqualsHelper.equalsCollection (aMap, aMap3));
  assertFalse (EqualsHelper.equalsCollection (aMap3, aMap));
}
 
Example #20
Source File: ClassPathHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * @return A non-<code>null</code> list of all directories and files currently
 *         in the class path.
 */
@Nonnull
@ReturnsMutableCopy
public static ICommonsList <String> getAllClassPathEntries ()
{
  final ICommonsList <String> ret = new CommonsArrayList <> ();
  forAllClassPathEntries (ret::add);
  return ret;
}
 
Example #21
Source File: StackHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressFBWarnings ("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
public void testNew ()
{
  StackHelper.newStack ();
  StackHelper.newStack ("a");
  StackHelper.newStack (new String [] { "a" });
  StackHelper.newStack (new CommonsArrayList <> ("a"));
  StackHelper.newStack (new IterableIterator <> (new CommonsArrayList <> ("a")));
  StackHelper.newStack ((Iterable <String>) new CommonsArrayList <> ("a"));
  StackHelper.newStack (new CommonsArrayList <> ("a").iterator ());
  StackHelper.newStack (new CommonsArrayList <> ("a"), Objects::nonNull);
  StackHelper.newStackMapped (new CommonsArrayList <Object> ("a"), Object::toString);
  StackHelper.newStackMapped (new Object [] { "a" }, Object::toString);
}
 
Example #22
Source File: URLParameterList.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * @return A new multi map (map from String to List of String) with all
 *         values. Order may be lost. Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, ICommonsList <String>> getAsMultiMap ()
{
  final ICommonsOrderedMap <String, ICommonsList <String>> ret = new CommonsLinkedHashMap <> ();
  forEach (aParam -> ret.computeIfAbsent (aParam.getName (), x -> new CommonsArrayList <> ())
                        .add (aParam.getValue ()));
  return ret;
}
 
Example #23
Source File: JsonWriterTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplex ()
{
  final ICommonsList <JsonObject> aObjs = new CommonsArrayList <> ();
  for (final ICommonsMap <String, String> aRow : new CommonsArrayList <> (CollectionHelper.newMap ("key", "value")))
  {
    final JsonObject aObj = new JsonObject ();
    for (final Map.Entry <String, String> aEntry : aRow.entrySet ())
      aObj.add (aEntry.getKey (), aEntry.getValue ());
    aObjs.add (aObj);
  }
  assertEquals ("{\"aa\":[{\"key\":\"value\"}]}", JsonConverter.convertToJson (new JsonObject ().add ("aa", aObjs)).getAsJsonString ());
}
 
Example #24
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSize_Iterable ()
{
  assertEquals (0, getSize ((Iterable <?>) null));
  assertEquals (0, getSize ((Iterable <String>) new CommonsArrayList <String> ()));
  assertEquals (1, getSize ((Iterable <String>) newList ("any")));
}
 
Example #25
Source File: PrimitiveCollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static CommonsArrayList <Long> newPrimitiveList (@Nullable final long... aValues)
{
  final CommonsArrayList <Long> ret = new CommonsArrayList <> ();
  if (aValues != null)
    for (final long aValue : aValues)
      ret.add (Long.valueOf (aValue));
  return ret;
}
 
Example #26
Source File: IAggregator.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Aggregate a array of input objects to a single output object.
 *
 * @param aObjects
 *        Source object array. May not be <code>null</code>.
 * @return The aggregated object. May be <code>null</code>.
 */
@Nullable
@SuppressWarnings ("unchecked")
// @SafeVarArgs does not work for default methods
default DSTTYPE apply (@Nullable final SRCTYPE... aObjects)
{
  return apply (new CommonsArrayList <> (aObjects));
}
 
Example #27
Source File: CSSNode.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
public Iterator <CSSNode> iterator ()
{
  final ICommonsList <CSSNode> aChildren = new CommonsArrayList <> (jjtGetNumChildren ());
  if (m_aChildren != null)
    for (final CSSNode aChildNode : m_aChildren)
      if (aChildNode != null)
        aChildren.add (aChildNode);
  return aChildren.iterator ();
}
 
Example #28
Source File: TypeConverterTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testBooleanArray ()
{
  final boolean [] aBooleans = new boolean [] { true, false, true };
  assertFalse (TypeConverter.convert (aBooleans, List.class).isEmpty ());
  assertFalse (TypeConverter.convert (aBooleans, CommonsArrayList.class).isEmpty ());
  assertFalse (TypeConverter.convert (aBooleans, Set.class).isEmpty ());
  assertFalse (TypeConverter.convert (aBooleans, CommonsHashSet.class).isEmpty ());
  assertFalse (TypeConverter.convert (aBooleans, CommonsLinkedHashSet.class).isEmpty ());
  assertEquals (3, TypeConverter.convert (aBooleans, String [].class).length);
}
 
Example #29
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testContainsNullElement ()
{
  assertFalse (containsAnyNullElement ((ICommonsList <String>) null));
  assertFalse (containsAnyNullElement (new CommonsArrayList <> ()));
  assertFalse (containsAnyNullElement (newList ("a")));
  assertFalse (containsAnyNullElement (newList ("a", "b", "c")));
  assertTrue (containsAnyNullElement (newList (null, "a")));
  assertTrue (containsAnyNullElement (newList ("a", null)));
  assertTrue (containsAnyNullElement (newList ((String) null)));
  assertTrue (containsAnyNullElement (newList (null, Integer.valueOf (5))));
}
 
Example #30
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
@ReturnsMutableCopy
public static <ELEMENTTYPE> CommonsArrayList <ELEMENTTYPE> getReverseList (@Nullable final Collection <? extends ELEMENTTYPE> aCollection)
{
  if (isEmpty (aCollection))
    return newList (0);

  final CommonsArrayList <ELEMENTTYPE> ret = newList (aCollection);
  ret.reverse ();
  return ret;
}