Java Code Examples for com.helger.commons.collection.impl.CommonsHashSet

The following examples show how to use com.helger.commons.collection.impl.CommonsHashSet. 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-ubl   Source File: EUBL23DocumentTypeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAll ()
{
  final ICommonsSet <Class <?>> aClasses = new CommonsHashSet <> ();
  final ICommonsSet <String> aFilenames = new CommonsHashSet <> ();
  for (final EUBL23DocumentType e : EUBL23DocumentType.values ())
  {
    assertNotNull (e.getImplementationClass ());
    assertTrue (StringHelper.hasText (e.getLocalName ()));
    assertTrue (StringHelper.hasText (e.getNamespaceURI ()));
    assertTrue (e.getAllXSDResources ().size () >= 1);
    for (final IReadableResource aRes : e.getAllXSDResources ())
      assertTrue (e.name (), aRes.exists ());
    assertNotNull (e.getSchema ());
    assertSame (e.getSchema (), e.getSchema ());
    assertSame (e, EUBL23DocumentType.valueOf (e.name ()));
    assertTrue (aClasses.add (e.getImplementationClass ()));
    assertTrue (aFilenames.add (e.getAllXSDResources ().getFirst ().getPath ()));
  }
}
 
Example 2
Source Project: ph-commons   Source File: IntObjectMapTest.java    License: Apache License 2.0 6 votes vote down vote up
private void _testPutRandom (final float fillFactor)
{
  final Random aRandom = new Random ();
  final int SIZE = 100 * 1000;
  final ICommonsSet <Integer> set = new CommonsHashSet <> (SIZE);
  final int [] vals = new int [SIZE];
  while (set.size () < SIZE)
    set.add (Integer.valueOf (aRandom.nextInt ()));
  int i = 0;
  for (final Integer v : set)
    vals[i++] = v.intValue ();

  final IntObjectMap <String> map = _makeMap (100, fillFactor);
  for (i = 0; i < vals.length; ++i)
  {
    assertNull ("Inserting " + vals[i], map.put (vals[i], _make (vals[i])));
    assertEquals (i + 1, map.size ());
    assertEquals (_make (vals[i]), map.get (vals[i]));
  }
  // now check the final state
  for (i = 0; i < vals.length; ++i)
    assertEquals (_make (vals[i]), map.get (vals[i]));
}
 
Example 3
Source Project: ph-commons   Source File: IntFloatMapTest.java    License: Apache License 2.0 6 votes vote down vote up
private void _testPutRandom (final float fillFactor)
{
  final Random aRandom = new Random ();
  final int SIZE = 100 * 1000;
  final Set <Integer> set = new CommonsHashSet <> (SIZE);
  final int [] vals = new int [SIZE];
  while (set.size () < SIZE)
    set.add (Integer.valueOf (aRandom.nextInt ()));
  int i = 0;
  for (final Integer v : set)
    vals[i++] = v.intValue ();

  final IntFloatMap map = _makeMap (100, fillFactor);
  for (i = 0; i < vals.length; ++i)
  {
    assertEquals ("Inserting " + vals[i], IntFloatMap.NO_VALUE, map.put (vals[i], vals[i]), DELTA);
    assertEquals (i + 1, map.size ());
    assertEquals (vals[i], map.get (vals[i]), DELTA);
  }
  // now check the final state
  for (i = 0; i < vals.length; ++i)
    assertEquals (vals[i], map.get (vals[i]), DELTA);
}
 
Example 4
Source Project: ph-commons   Source File: IntDoubleMapTest.java    License: Apache License 2.0 6 votes vote down vote up
private void _testPutRandom (final float fillFactor)
{
  final Random aRandom = new Random ();
  final int SIZE = 100 * 1000;
  final Set <Integer> set = new CommonsHashSet <> (SIZE);
  final int [] vals = new int [SIZE];
  while (set.size () < SIZE)
    set.add (Integer.valueOf (aRandom.nextInt ()));
  int i = 0;
  for (final Integer v : set)
    vals[i++] = v.intValue ();

  final IntDoubleMap map = _makeMap (100, fillFactor);
  for (i = 0; i < vals.length; ++i)
  {
    assertEquals ("Inserting " + vals[i], IntDoubleMap.NO_VALUE, map.put (vals[i], vals[i]), DELTA);
    assertEquals (i + 1, map.size ());
    assertEquals (vals[i], map.get (vals[i]), DELTA);
  }
  // now check the final state
  for (i = 0; i < vals.length; ++i)
    assertEquals (vals[i], map.get (vals[i]), DELTA);
}
 
Example 5
Source Project: ph-commons   Source File: IntIntMapTest.java    License: Apache License 2.0 6 votes vote down vote up
private void _testPutRandom (final float fillFactor)
{
  final Random aRandom = new Random ();
  final int SIZE = 100 * 1000;
  final ICommonsSet <Integer> set = new CommonsHashSet <> (SIZE);
  final int [] vals = new int [SIZE];
  while (set.size () < SIZE)
    set.add (Integer.valueOf (aRandom.nextInt ()));
  int i = 0;
  for (final Integer v : set)
    vals[i++] = v.intValue ();

  final IntIntMap map = _makeMap (100, fillFactor);
  for (i = 0; i < vals.length; ++i)
  {
    assertEquals (0, map.put (vals[i], vals[i]));
    assertEquals (i + 1, map.size ());
    assertEquals (vals[i], map.get (vals[i]));
  }
  // now check the final state
  for (i = 0; i < vals.length; ++i)
    assertEquals (vals[i], map.get (vals[i]));
}
 
Example 6
Source Project: ph-commons   Source File: MapBasedNamespaceContext.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
private MapBasedNamespaceContext _addMapping (@Nonnull final String sPrefix,
                                              @Nonnull final String sNamespaceURI,
                                              final boolean bAllowOverwrite)
{
  ValueEnforcer.notNull (sPrefix, "Prefix");
  ValueEnforcer.notNull (sNamespaceURI, "NamespaceURI");
  if (!bAllowOverwrite && m_aPrefix2NS.containsKey (sPrefix))
    throw new IllegalArgumentException ("The prefix '" +
                                        sPrefix +
                                        "' is already registered to '" +
                                        m_aPrefix2NS.get (sPrefix) +
                                        "'!");

  if (sPrefix.equals (XMLConstants.DEFAULT_NS_PREFIX))
    m_sDefaultNamespaceURI = sNamespaceURI;
  m_aPrefix2NS.put (sPrefix, sNamespaceURI);
  m_aNS2Prefix.computeIfAbsent (sNamespaceURI, x -> new CommonsHashSet <> ()).add (sPrefix);
  return this;
}
 
Example 7
Source Project: ph-commons   Source File: CombinationGeneratorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStringCombination2 ()
{
  final ICommonsList <String> aElements = new CommonsArrayList <> (A, B, B, C);
  final CombinationGenerator <String> x = new CombinationGenerator <> (aElements, 0);
  assertEquals (BigInteger.ONE, x.getTotalCombinations ());
  assertEquals (BigInteger.ONE, x.getCombinationsLeft ());

  final ICommonsList <ICommonsList <String>> aResultsWithDuplicates = new CommonsArrayList <> ();
  final ICommonsSet <ICommonsList <String>> aResultsWithoutDuplicates = new CommonsHashSet <> ();
  while (x.hasNext ())
  {
    final ICommonsList <String> aResult = x.next ();
    aResultsWithDuplicates.add (aResult);
    aResultsWithoutDuplicates.add (aResult);
  }
  assertEquals (1, aResultsWithDuplicates.size ());
  assertEquals (1, aResultsWithoutDuplicates.size ());
}
 
Example 8
@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 9
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 10
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 11
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 12
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: 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 13
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIterable ()
{
  final Iterable <String> aCont = new MockIterable ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aCont, aCont));
  assertTrue (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b", "c")));
  assertTrue (EqualsHelper.equalsCollection (new MockIterable (), new MockIterable ()));

  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ()));
  assertFalse (EqualsHelper.equalsCollection (new MockIterable (), aCont));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("A", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "B", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b", "C")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, ArrayHelper.newArray ("a", "b", "c")));
}
 
Example 14
Source Project: ph-commons   Source File: NonBlockingStackTest.java    License: 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 15
Source Project: ph-commons   Source File: CollectionHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDifference ()
{
  final ICommonsList <String> l1 = newList ("Hello", "Welt", "from", "Vienna");
  final ICommonsList <String> l2 = newList ("Welt", "from");

  // Result should be "Hello" and "Vienna"
  final Set <String> ret = getDifference (l1, l2);
  assertNotNull (ret);
  assertEquals (2, ret.size ());
  assertTrue (ret.contains ("Hello"));
  assertFalse (ret.contains ("Welt"));
  assertFalse (ret.contains ("from"));
  assertTrue (ret.contains ("Vienna"));

  assertEquals (4, getDifference (l1, new CommonsVector <String> ()).size ());
  assertEquals (4, getDifference (l1, null).size ());
  assertEquals (0, getDifference (new CommonsHashSet <String> (), l2).size ());
  assertEquals (0, getDifference (null, l2).size ());
}
 
Example 16
Source Project: ph-ubl   Source File: EUBL22DocumentTypeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAll ()
{
  final ICommonsSet <Class <?>> aClasses = new CommonsHashSet <> ();
  final ICommonsSet <String> aFilenames = new CommonsHashSet <> ();
  for (final EUBL22DocumentType e : EUBL22DocumentType.values ())
  {
    assertNotNull (e.getImplementationClass ());
    assertTrue (StringHelper.hasText (e.getLocalName ()));
    assertTrue (StringHelper.hasText (e.getNamespaceURI ()));
    assertTrue (e.getAllXSDResources ().size () >= 1);
    for (final IReadableResource aRes : e.getAllXSDResources ())
      assertTrue (e.name (), aRes.exists ());
    assertNotNull (e.getSchema ());
    assertSame (e.getSchema (), e.getSchema ());
    assertSame (e, EUBL22DocumentType.valueOf (e.name ()));
    assertTrue (aClasses.add (e.getImplementationClass ()));
    assertTrue (aFilenames.add (e.getAllXSDResources ().getFirst ().getPath ()));
  }
}
 
Example 17
Source Project: ph-commons   Source File: SingleElementMap.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@CodingStyleguideUnaware
public Set <Map.Entry <KEYTYPE, VALUETYPE>> entrySet ()
{
  final ICommonsSet <Map.Entry <KEYTYPE, VALUETYPE>> aSet = new CommonsHashSet <> (size ());
  if (m_bHasElement)
    aSet.add (new MapEntry <> (m_aKey, m_aValue));
  return aSet;
}
 
Example 18
@Override
@Nonnull
@ReturnsMutableCopy
protected final CommonsHashSet <VALUETYPE> createNewCollection ()
{
  return new CommonsHashSet <> ();
}
 
Example 19
Source Project: ph-commons   Source File: MultiHashMapHashSetBased.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected final CommonsHashSet <VALUETYPE> createNewCollection ()
{
  return new CommonsHashSet <> ();
}
 
Example 20
Source Project: ph-commons   Source File: MultiLinkedHashMapHashSetBased.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected final CommonsHashSet <VALUETYPE> createNewCollection ()
{
  return new CommonsHashSet <> ();
}
 
Example 21
Source Project: ph-commons   Source File: MultiTreeMapHashSetBased.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected final CommonsHashSet <VALUETYPE> createNewCollection ()
{
  return new CommonsHashSet <> ();
}
 
Example 22
Source Project: ph-commons   Source File: MultiWeakHashMapHashSetBased.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected final CommonsHashSet <VALUETYPE> createNewCollection ()
{
  return new CommonsHashSet <> ();
}
 
Example 23
Source Project: ph-commons   Source File: DirectedGraphNode.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsSet <IMutableDirectedGraphNode> getAllFromNodes ()
{
  final ICommonsSet <IMutableDirectedGraphNode> ret = new CommonsHashSet <> ();
  if (m_aIncoming != null)
    CollectionHelper.findAllMapped (m_aIncoming.values (), IMutableDirectedGraphRelation::getFrom, ret::add);
  return ret;
}
 
Example 24
Source Project: ph-commons   Source File: DirectedGraphNode.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsSet <IMutableDirectedGraphNode> getAllToNodes ()
{
  final ICommonsSet <IMutableDirectedGraphNode> ret = new CommonsHashSet <> ();
  if (m_aOutgoing != null)
    CollectionHelper.findAllMapped (m_aOutgoing.values (), IMutableDirectedGraphRelation::getTo, ret::add);
  return ret;
}
 
Example 25
Source Project: ph-commons   Source File: SimpleDirectedGraphTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCycles2 ()
{
  final SimpleDirectedGraph sg = new SimpleDirectedGraph ();
  for (int i = 1; i <= 6; ++i)
    sg.createNode (Integer.toString (i));

  // first cycle
  sg.createRelation ("1", "2");
  assertFalse (sg.containsCycles ());
  sg.createRelation ("2", "3");
  assertFalse (sg.containsCycles ());
  sg.createRelation ("3", "1");
  assertTrue (sg.containsCycles ());

  // Second cycle
  sg.createRelation ("4", "5");
  assertTrue (sg.containsCycles ());
  sg.createRelation ("5", "6");
  assertTrue (sg.containsCycles ());
  sg.createRelation ("6", "4");
  assertTrue (sg.containsCycles ());

  final ICommonsSet <String> aRelationIDs1 = sg.getAllRelationIDs ();
  final ICommonsSet <String> aRelationIDs2 = new CommonsHashSet <> ();
  sg.forEachRelation (x -> aRelationIDs2.add (x.getID ()));
  assertTrue (EqualsHelper.equalsCollection (aRelationIDs1, aRelationIDs2));
}
 
Example 26
Source Project: ph-commons   Source File: XMLListHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRead ()
{
  ICommonsList <String> aList = new CommonsArrayList <> ();
  final IReadableResource aRes = new ClassPathResource ("xml/list.xml");
  XMLListHandler.readList (aRes, aList);
  assertEquals (3, aList.size ());
  assertTrue (aList.contains ("item1"));

  assertNotNull (XMLListHandler.readList (aRes));
  assertNull (XMLListHandler.readList (new ClassPathResource ("test1.txt")));
  assertEquals (3, XMLListHandler.readList (aRes).size ());
  assertTrue (XMLListHandler.readList (aRes).contains ("item1"));

  assertTrue (XMLListHandler.writeList (aList, new NonBlockingByteArrayOutputStream ()).isSuccess ());

  final String sXML = "<root><item/><item value='a' /><item value='a' /></root>";
  aList = XMLListHandler.readList (new StringInputStreamProvider (sXML, StandardCharsets.ISO_8859_1));
  assertNotNull (aList);
  assertEquals (2, aList.size ());
  assertEquals ("a", aList.get (0));
  assertEquals ("a", aList.get (1));

  final ICommonsSet <String> aSet = new CommonsHashSet <> ();
  XMLListHandler.readList (new StringInputStreamProvider (sXML, StandardCharsets.ISO_8859_1), aSet);
  assertEquals (1, aSet.size ());
  assertTrue (aSet.contains ("a"));

  assertTrue (XMLListHandler.writeList (aList, new NonBlockingByteArrayOutputStream ()).isSuccess ());
  assertTrue (XMLListHandler.writeList (aList, new ByteArrayOutputStreamProvider ()).isSuccess ());
}
 
Example 27
Source Project: ph-commons   Source File: UTF7CharsetProviderSPITest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testIterator ()
{
  final Iterator <Charset> iterator = tested.charsets ();
  final ICommonsSet <Charset> found = new CommonsHashSet<> ();
  while (iterator.hasNext ())
    found.add (iterator.next ());
  assertEquals (3, found.size ());
  final Charset charset1 = tested.charsetForName ("x-IMAP4-modified-UTF7");
  final Charset charset2 = tested.charsetForName ("UTF-7");
  final Charset charset3 = tested.charsetForName ("X-UTF-7-OPTIONAL");
  assertTrue (found.contains (charset1));
  assertTrue (found.contains (charset2));
  assertTrue (found.contains (charset3));
}
 
Example 28
Source Project: ph-commons   Source File: CombinationGeneratorFlexible.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Generate all combinations without duplicates.
 *
 * @param aElements
 *        the elements to distribute to the specified slots (may be empty!)
 * @return a set of slot allocations representing all possible combinations
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsSet <ICommonsList <DATATYPE>> getCombinations (@Nonnull final ICommonsList <DATATYPE> aElements)
{
  ValueEnforcer.notNull (aElements, "Elements");

  final ICommonsSet <ICommonsList <DATATYPE>> aAllResults = new CommonsHashSet <> ();
  iterateAllCombinations (aElements, aAllResults::add);
  return aAllResults;
}
 
Example 29
Source Project: ph-commons   Source File: LocaleCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get all contained locales that consist only of a non-empty language.
 *
 * @return a set with all contained languages, except "all" and "independent"
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsSet <Locale> getAllLanguages ()
{
  final ICommonsSet <Locale> ret = new CommonsHashSet <> ();
  for (final Locale aLocale : getAllLocales ())
  {
    final String sLanguage = aLocale.getLanguage ();
    if (StringHelper.hasText (sLanguage))
      ret.add (getLocale (sLanguage, null, null));
  }
  return ret;
}
 
Example 30
Source Project: ph-commons   Source File: LanguageCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return a set with all contained language locales. Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsSet <Locale> getAllLanguageLocales ()
{
  return m_aRWLock.readLockedGet ( () -> {
    final LocaleCache aLC = LocaleCache.getInstance ();
    return new CommonsHashSet <> (m_aLanguages, sLanguage -> aLC.getLocale (sLanguage, "", ""));
  });
}