Java Code Examples for com.helger.commons.collection.CollectionHelper

The following examples show how to use com.helger.commons.collection.CollectionHelper. These examples are extracted from open source projects. 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 Project: ph-commons   Source File: EqualsHelperTest.java    License: 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 Project: ph-commons   Source File: IAggregatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUseFirst ()
{
  final IAggregator <String, String> a1 = CollectionHelper::getFirstElement;
  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 ("a", a1.apply (l));
  assertNull (a1.apply (new CommonsArrayList <String> ()));
  assertNull (a1.apply ((Collection <String>) null));
}
 
Example 3
Source Project: ph-commons   Source File: IComparatorTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test for constructors using a locale
 */
@Test
public void testLocaleGerman ()
{
  final String S1 = "bbc";
  final String S2 = "abc";
  final String S3 = "äbc";
  final String [] x = new String [] { S1, S2, S3 };

  // default: sort ascending
  ICommonsList <String> l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.GERMAN));
  assertArrayEquals (new String [] { S2, S3, S1 }, l.toArray ());

  // sort ascending manually
  l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.GERMAN));
  assertArrayEquals (new String [] { S2, S3, S1 }, l.toArray ());

  // sort descending manually
  l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.GERMAN).reversed ());
  assertArrayEquals (new String [] { S1, S3, S2 }, l.toArray ());

  // null locale allowed
  IComparator.getComparatorCollating ((Locale) null);
  assertArrayEquals (new String [] { S1, S3, S2 }, l.toArray ());
}
 
Example 4
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMap ()
{
  final StringMap aMap = new StringMap ("a", "b").add ("c", "d");
  assertTrue (EqualsHelper.equalsCollection (aMap, aMap));
  assertTrue (EqualsHelper.equalsCollection (aMap, CollectionHelper.makeUnmodifiable (aMap)));
  assertTrue (EqualsHelper.equalsCollection (aMap, Collections.synchronizedMap (aMap)));
  assertTrue (EqualsHelper.equalsCollection (aMap, new StringMap ("a", "b").add ("c", "d")));
  assertTrue (EqualsHelper.equalsCollection (new CommonsHashMap <Integer, Integer> (), new CommonsHashMap <Double, Float> ()));

  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsHashMap <Integer, Integer> ()));
  assertFalse (EqualsHelper.equalsCollection (new CommonsHashMap <Integer, Integer> (), aMap));
  assertFalse (EqualsHelper.equalsCollection (aMap, new StringMap ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new StringMap ("A", "b").add ("c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new StringMap ("a", "B").add ("c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new StringMap ("a", "b").add ("C", "d")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new StringMap ("a", "b").add ("c", "D")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new StringMap ("a", "b").add ("c", "d").add ("e", "f")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aMap, ArrayHelper.newArray ("a", "b", "c")));
}
 
Example 5
Source Project: ph-commons   Source File: IAggregatorTest.java    License: 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
@Test
public void testChildProvder ()
{
  final DefaultTreeWithGlobalUniqueID <String, String> aTestTree2 = new DefaultTreeWithGlobalUniqueID <> ();

  assertEquals (0, aTestTree2.getChildCount (null));
  assertFalse (aTestTree2.hasChildren (null));
  assertTrue (CollectionHelper.isEmpty (aTestTree2.getAllChildren (null)));

  final DefaultTreeItemWithID <String, String> x12 = aTestTree2.getRootItem ().createChildItem ("x1", "1");
  x12.createChildItem ("x2", "a");
  x12.createChildItem ("x3", "b");
  x12.createChildItem ("x4", "c");

  assertEquals (1, aTestTree2.getChildCount (null));
  assertTrue (aTestTree2.hasChildren (null));
  assertEquals (1, aTestTree2.getAllChildren (null).size ());

  assertEquals (3, aTestTree2.getChildCount (x12));
  assertTrue (aTestTree2.hasChildren (x12));
  assertEquals (3, aTestTree2.getAllChildren (x12).size ());
}
 
Example 7
Source Project: ph-commons   Source File: KeyStoreHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadPeppolTrustStorePilot () throws Exception
{
  // Load trust store
  final KeyStore aTrustStore = KeyStoreHelper.loadKeyStoreDirect (EKeyStoreType.JKS,
                                                                  "keystores/truststore-peppol-pilot.jks",
                                                                  "peppol");
  assertNotNull (aTrustStore);

  // Additionally the STS certificate is contained
  assertEquals (4, CollectionHelper.getSize (aTrustStore.aliases ()));

  // Ensure all name entries are contained
  assertNotNull (aTrustStore.getCertificate (TRUSTSTORE_PILOT_ALIAS_ROOT));
  assertNotNull (aTrustStore.getCertificate (TRUSTSTORE_PILOT_ALIAS_AP));
  assertNotNull (aTrustStore.getCertificate (TRUSTSTORE_PILOT_ALIAS_SMP));

  final X509Certificate aCertAPOld = (X509Certificate) aTrustStore.getCertificate (TRUSTSTORE_PILOT_ALIAS_AP);
  final String sIssuerName = aCertAPOld.getIssuerX500Principal ().getName ();
  assertEquals ("CN=PEPPOL Root TEST CA,OU=FOR TEST PURPOSES ONLY,O=NATIONAL IT AND TELECOM AGENCY,C=DK",
                sIssuerName);
  final String sSubjectName = aCertAPOld.getSubjectX500Principal ().getName ();
  assertEquals ("CN=PEPPOL ACCESS POINT TEST CA,OU=FOR TEST PURPOSES ONLY,O=NATIONAL IT AND TELECOM AGENCY,C=DK",
                sSubjectName);
}
 
Example 8
Source Project: ph-commons   Source File: FilterIteratorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIteration5 ()
{
  // filtered elements at the beginning
  final List <String> aList = CollectionHelper.newList (null, null, "s1", "s2", "s3");
  final FilterIterator <String> it = new FilterIterator <> (aList, Objects::nonNull);
  assertNotNull (it);

  assertEquals ("s1", it.next ());
  assertEquals ("s2", it.next ());
  assertEquals ("s3", it.next ());

  CommonsTestHelper.testToStringImplementation (it);

  try
  {
    // can't call next if hasNext failed
    it.next ();
    fail ();
  }
  catch (final NoSuchElementException ex)
  {}
}
 
Example 9
Source Project: ph-commons   Source File: IComparatorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCollatingOrder ()
{
  final String S1 = "abc";
  final String S2 = "ABC";
  final String S3 = "ab";
  final String [] x = new String [] { S1, S2, S3 };

  // Explicitly sort ascending
  List <String> l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.US));
  assertArrayEquals (new String [] { S3, S1, S2 }, l.toArray ());

  // Explicitly sort descending
  l = CollectionHelper.getSorted (x, IComparator.getComparatorCollating (Locale.US).reversed ());
  assertArrayEquals (new String [] { S2, S1, S3 }, l.toArray ());
}
 
Example 10
Source Project: ph-ubl   Source File: MockUBLTRTestDocuments.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static List <String> getUBLTRTestDocuments (@Nonnull final EUBLTRDocumentType eType)
{
  List <String> aFiles = null;
  switch (eType)
  {
    case CANCEL_USER_ACCOUNT:
      aFiles = CollectionHelper.makeUnmodifiable (PREFIX +
                                                  "tr_useraccount/5_KULLANICI_SILME.xml",
                                                  PREFIX + "tr_useraccount/9_FATURA_SAKLAMA_KULLANICI_SILME.xml");
      break;
    case PROCESS_USER_ACCOUNT:
      aFiles = CollectionHelper.makeUnmodifiable (PREFIX +
                                                  "tr_useraccount/4_KULLANICI_ACMA.xml",
                                                  PREFIX + "tr_useraccount/8_FATURA_SAKLAMA_KULLANICI_ACMA.xml");
      break;
    default:
      throw new IllegalArgumentException ("No test files available for type " + eType);
  }

  return CollectionHelper.newList (aFiles);
}
 
Example 11
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSet ()
{
  final ICommonsSet <String> aCont = new CommonsHashSet <> ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aCont, aCont));
  assertTrue (EqualsHelper.equalsCollection (aCont, CollectionHelper.makeUnmodifiable (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, Collections.synchronizedSet (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new CommonsLinkedHashSet <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (aCont, new CommonsTreeSet <> (aCont)));
  assertTrue (EqualsHelper.equalsCollection (new CommonsHashSet <String> (), new CommonsLinkedHashSet <String> ()));
  assertTrue (EqualsHelper.equalsCollection (new CommonsTreeSet <String> (), new CommonsHashSet <String> ()));

  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <String> ()));
  assertFalse (EqualsHelper.equalsCollection (new CommonsHashSet <String> (), aCont));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsTreeSet <String> ()));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("A", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "B", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "b", "C")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, ArrayHelper.newArray ("a", "b", "c")));
}
 
Example 12
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualsTypeSpecific ()
{
  final StringBuffer aSB1 = new StringBuffer ("Hi");
  CommonsAssert.assertEquals (aSB1, new StringBuffer ("Hi"));
  CommonsAssert.assertNotEquals (aSB1, new StringBuffer ("Hallo"));

  CommonsAssert.assertEquals (aSB1, new StringBuffer ("Hi"));
  CommonsAssert.assertNotEquals (aSB1, new StringBuffer ("Hallo"));
  CommonsAssert.assertNotEquals (aSB1, null);

  CommonsAssert.assertEquals (new CommonsArrayList <> ("a", "b", "c"), new CommonsArrayList <> ("a", "b", "c"));
  CommonsAssert.assertEquals (StackHelper.newStack ("a", "b", "c"), StackHelper.newStack ("a", "b", "c"));
  CommonsAssert.assertEquals (new CommonsArrayList <> ("a", "b", "c").iterator (), new CommonsArrayList <> ("a", "b", "c").iterator ());
  CommonsAssert.assertEquals (IteratorHelper.getEnumeration ("a", "b", "c"), IteratorHelper.getEnumeration ("a", "b", "c"));
  CommonsAssert.assertNotEquals (CollectionHelper.makeUnmodifiable (new CommonsArrayList <> ("a", "b", "c")),
                                 new CommonsArrayList <> ("a", "b", "c"));
}
 
Example 13
Source Project: ph-schematron   Source File: PSSchema.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).appendIfNotNull ("resource", m_aResource)
                                     .appendIfNotNull ("id", m_sID)
                                     .appendIfNotNull ("rich", m_aRich)
                                     .appendIfNotNull ("schemaVersion", m_sSchemaVersion)
                                     .appendIfNotNull ("defaultPhase", m_sDefaultPhase)
                                     .appendIfNotNull ("queryBinding", m_sQueryBinding)
                                     .appendIfNotNull ("title", m_aTitle)
                                     .appendIf ("includes", m_aIncludes, CollectionHelper::isNotEmpty)
                                     .appendIf ("nss", m_aNSs, CollectionHelper::isNotEmpty)
                                     .appendIf ("startps", m_aStartPs, CollectionHelper::isNotEmpty)
                                     .appendIf ("lets", m_aLets, CollectionHelper::isNotEmpty)
                                     .appendIf ("phases", m_aPhases, CollectionHelper::isNotEmpty)
                                     .appendIf ("patterns", m_aPatterns, CollectionHelper::isNotEmpty)
                                     .appendIf ("endps", m_aEndPs, CollectionHelper::isNotEmpty)
                                     .appendIfNotNull ("diagnostics", m_aDiagnostics)
                                     .appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
                                     .appendIf ("foreignElements", m_aForeignElements, CollectionHelper::isNotEmpty)
                                     .getToString ();
}
 
Example 14
Source Project: ph-commons   Source File: QueueHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE> PriorityQueue <ELEMENTTYPE> newQueue (@Nullable final Collection <? extends ELEMENTTYPE> aCont)
{
  if (CollectionHelper.isEmpty (aCont))
    return newQueue (0);
  return new PriorityQueue <> (aCont);
}
 
Example 15
Source Project: ph-commons   Source File: VectorHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <SRCTYPE, DSTTYPE> CommonsVector <DSTTYPE> newVectorMapped (@Nullable final Collection <? extends SRCTYPE> aCollection,
                                                                          @Nonnull final Function <? super SRCTYPE, DSTTYPE> aMapper)
{
  if (CollectionHelper.isEmpty (aCollection))
    return newVector (0);
  final CommonsVector <DSTTYPE> ret = newVector (aCollection.size ());
  ret.addAllMapped (aCollection, aMapper);
  return ret;
}
 
Example 16
Source Project: ph-commons   Source File: VectorHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE> CommonsVector <ELEMENTTYPE> newVector (@Nullable final Collection <? extends ELEMENTTYPE> aCont)
{
  if (CollectionHelper.isEmpty (aCont))
    return newVector (0);

  return new CommonsVector <> (aCont);
}
 
Example 17
Source Project: ph-schematron   Source File: PSRule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).appendIfNotNull ("flag", m_sFlag)
                                     .appendIfNotNull ("rich", m_aRich)
                                     .appendIfNotNull ("linkable", m_aLinkable)
                                     .append ("abstract", m_bAbstract)
                                     .appendIfNotNull ("context", m_sContext)
                                     .appendIfNotNull ("id", m_sID)
                                     .appendIf ("content", m_aContent, CollectionHelper::isNotEmpty)
                                     .appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
                                     .getToString ();
}
 
Example 18
Source Project: ph-schematron   Source File: PSDir.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).appendIfNotNull ("value", m_eValue)
                                     .appendIf ("content", m_aContent, CollectionHelper::isNotEmpty)
                                     .appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
                                     .getToString ();
}
 
Example 19
Source Project: ph-commons   Source File: Utf8ResourceBundleTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAll ()
{
  assertNotNull (Utf8ResourceBundle.getBundle ("properties/test-utf8"));
  assertNotNull (Utf8ResourceBundle.getBundle ("properties/test-utf8", L_DE));
  final ResourceBundle rb = Utf8ResourceBundle.getBundle ("properties/test-utf8",
                                                          L_DE,
                                                          ClassLoaderHelper.getDefaultClassLoader ());
  assertNotNull (rb);
  assertTrue (rb instanceof Utf8PropertyResourceBundle);
  assertEquals (2, CollectionHelper.newList (rb.getKeys ()).size ());
}
 
Example 20
Source Project: ph-schematron   Source File: PSName.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).appendIfNotNull ("path", m_sPath)
                                     .appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
                                     .getToString ();
}
 
Example 21
Source Project: ph-commons   Source File: XMLHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnegative
public static int getDirectChildElementCountNS (@Nullable final Element aParent,
                                                @Nullable final String sNamespaceURI,
                                                @Nonnull @Nonempty final String sLocalName)
{
  return aParent == null ? 0
                         : CollectionHelper.getSize (getChildElementIteratorNS (aParent, sNamespaceURI, sLocalName));
}
 
Example 22
Source Project: ph-commons   Source File: MimeTypeInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).append ("mimeTypes", m_aMimeTypes)
                                     .appendIfNotNull ("comment", m_sComment)
                                     .appendIf ("parentTypes", m_aParentTypes, CollectionHelper::isNotEmpty)
                                     .appendIf ("globs", m_aGlobs, CollectionHelper::isNotEmpty)
                                     .appendIf ("extensions", m_aExtensions, CollectionHelper::isNotEmpty)
                                     .appendIfNotNull ("source", m_sSource)
                                     .getToString ();
}
 
Example 23
Source Project: ph-commons   Source File: MapBasedNamespaceContext.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public String getCustomPrefix (@Nonnull final String sNamespaceURI)
{
  final ICommonsSet <String> aAllPrefixes = m_aNS2Prefix.get (sNamespaceURI);
  return CollectionHelper.getFirstElement (aAllPrefixes);
}
 
Example 24
Source Project: ph-commons   Source File: ChildElementIteratorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRecursiveChildIter ()
{
  final Document doc = XMLFactory.newDocument ();

  // No children present
  assertFalse (new ChildElementIterator (doc).hasNext ());

  // 1 child
  final Element eRoot = (Element) doc.appendChild (doc.createElement ("root"));
  assertEquals (1, CollectionHelper.newList (new ChildElementIterator (doc)).size ());

  // 2 children
  eRoot.appendChild (doc.createElement ("Hallo"));
  eRoot.appendChild (doc.createTextNode (" - "));
  eRoot.appendChild (doc.createElement ("Welt"));
  assertEquals (2, CollectionHelper.newList (new ChildElementIterator (eRoot)).size ());
  assertEquals (1,
                CollectionHelper.newList (new ChildElementIterator (eRoot).withFilter (XMLHelper.filterElementWithTagName ("Hallo")))
                                .size ());

  try
  {
    new ChildElementIterator (doc).remove ();
    fail ();
  }
  catch (final UnsupportedOperationException ex)
  {}
  assertEquals (0, CollectionHelper.getSize (new ChildElementIterator (null)));
}
 
Example 25
Source Project: ph-commons   Source File: JsonWriterTest.java    License: 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 26
Source Project: ph-schematron   Source File: PSValueOf.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).appendIfNotNull ("select", m_sSelect)
                                     .appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
                                     .getToString ();
}
 
Example 27
Source Project: ph-commons   Source File: AbstractMapBasedWALDAO.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@IsLocked (ELockType.READ)
public final ICommonsList <INTERFACETYPE> getAll (@Nullable final Predicate <? super INTERFACETYPE> aFilter)
{
  if (aFilter == null)
    return getAll ();

  // Use new CommonsArrayList to get the return type to NOT use "? extends
  // INTERFACETYPE"
  final ICommonsList <INTERFACETYPE> ret = new CommonsArrayList <> ();
  // (Runnable) cast for Java 9
  m_aRWLock.readLocked ((Runnable) () -> CollectionHelper.findAll (m_aMap.values (), aFilter, ret::add));
  return ret;
}
 
Example 28
Source Project: ph-commons   Source File: AbstractMapBasedWALDAO.java    License: Apache License 2.0 5 votes vote down vote up
@IsLocked (ELockType.READ)
public final <RETTYPE> void findAllMapped (@Nullable final Predicate <? super INTERFACETYPE> aFilter,
                                           @Nonnull final Function <? super INTERFACETYPE, ? extends RETTYPE> aMapper,
                                           @Nonnull final Consumer <? super RETTYPE> aConsumer)
{
  // (Runnable) cast for Java 9
  m_aRWLock.readLocked ((Runnable) () -> CollectionHelper.findAllMapped (m_aMap.values (),
                                                                         aFilter,
                                                                         aMapper,
                                                                         aConsumer));
}
 
Example 29
Source Project: ph-commons   Source File: IterableIteratorTest.java    License: 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 30
Source Project: ph-commons   Source File: EqualsHelper.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean _areChildrenEqual (@Nullable final Object aObj1, @Nullable final Object aObj2)
{
  if (CollectionHelper.isCollectionObject (aObj1) && CollectionHelper.isCollectionObject (aObj2))
  {
    // It's a nested collection!
    // -> indirect recursive call
    return equalsCollection (aObj1, aObj2);
  }

  // Not collections
  return EqualsImplementationRegistry.areEqual (aObj1, aObj2);
}