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

The following examples show how to use com.helger.commons.collection.impl.CommonsTreeMap. 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: JsonWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap ()
{
  final ICommonsMap <String, Object> aMap = new CommonsHashMap <> ();
  aMap.put ("foo", "bar");
  assertEquals ("{\"foo\":\"bar\"}", JsonConverter.convertToJson (aMap).getAsJsonString ());

  final ICommonsNavigableMap <String, Object> aTreeMap = new CommonsTreeMap <> ();
  aTreeMap.put ("foo", "bar");
  aTreeMap.put ("foo2", Integer.valueOf (5));
  assertEquals ("{\"foo\":\"bar\",\"foo2\":5}", JsonConverter.convertToJson (aTreeMap).getAsJsonString ());

  final ICommonsOrderedMap <String, Object> aLinkedMap = new CommonsLinkedHashMap <> ();
  aLinkedMap.put ("foo", "bar");
  aLinkedMap.put ("foo2", Integer.valueOf (5));
  assertEquals ("{\"foo\":\"bar\",\"foo2\":5}", JsonConverter.convertToJson (aLinkedMap).getAsJsonString ());
  assertEquals ("{foo:\"bar\",foo2:5}",
                JsonConverter.convertToJson (aLinkedMap).getAsJsonString (new JsonWriterSettings ().setQuoteNames (false)));
}
 
Example #2
Source File: JsonWriterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteAndReadMap ()
{
  final ICommonsMap <String, Object> aMap = new CommonsHashMap <> ();
  aMap.put ("foo", "bar");
  _testWriteAndRead (aMap);

  final ICommonsNavigableMap <String, Object> aTreeMap = new CommonsTreeMap <> ();
  aTreeMap.put ("foo", "bar");
  aTreeMap.put ("foo2", Integer.valueOf (5));
  _testWriteAndRead (aTreeMap);

  final ICommonsOrderedMap <String, Object> aLinkedMap = new CommonsLinkedHashMap <> ();
  aLinkedMap.put ("foo", "bar");
  aLinkedMap.put ("foo2", Integer.valueOf (5));
  _testWriteAndRead (aLinkedMap);
}
 
Example #3
Source File: JsonConverterTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap ()
{
  final ICommonsMap <String, Object> aMap = new CommonsHashMap <> ();
  aMap.put ("foo", "bar");
  aMap.put ("foo2", Integer.valueOf (5));
  assertTrue (JsonConverter.convertToJson (aMap) instanceof JsonObject);

  final ICommonsNavigableMap <String, Object> aTreeMap = new CommonsTreeMap <> ();
  aTreeMap.put ("foo", "bar");
  aTreeMap.put ("foo2", Integer.valueOf (5));
  assertTrue (JsonConverter.convertToJson (aTreeMap) instanceof JsonObject);

  final ICommonsOrderedMap <String, Object> aLinkedMap = new CommonsLinkedHashMap <> ();
  aLinkedMap.put ("foo", "bar");
  aLinkedMap.put ("foo2", Integer.valueOf (5));
  assertTrue (JsonConverter.convertToJson (aLinkedMap) instanceof JsonObject);
}
 
Example #4
Source File: EqualsHashcodeFuncTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap ()
{
  final ICommonsMap <String, Object> aMap = new CommonsHashMap <> ();
  aMap.put ("foo", "bar");
  _testEqualsHashcode (aMap);

  final ICommonsMap <String, Object> aTreeMap = new CommonsTreeMap <> ();
  aTreeMap.put ("foo", "bar");
  aTreeMap.put ("foo2", Integer.valueOf (5));
  _testEqualsHashcode (aTreeMap);

  final ICommonsMap <String, Object> aLinkedMap = new CommonsLinkedHashMap <> ();
  aLinkedMap.put ("foo", "bar");
  aLinkedMap.put ("foo2", Integer.valueOf (5));
  _testEqualsHashcode (aLinkedMap);
}
 
Example #5
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static <ELEMENTTYPE extends Comparable <? super ELEMENTTYPE>> CommonsTreeMap <ELEMENTTYPE, ELEMENTTYPE> newSortedMap (@Nullable final ELEMENTTYPE... aValues)
{
  if (ArrayHelper.isEmpty (aValues))
    return newSortedMap ();

  if ((aValues.length % 2) != 0)
    throw new IllegalArgumentException ("The passed array needs an even number of elements!");

  final CommonsTreeMap <ELEMENTTYPE, ELEMENTTYPE> ret = newSortedMap ();
  for (int i = 0; i < aValues.length; i += 2)
    ret.put (aValues[i], aValues[i + 1]);
  return ret;
}
 
Example #6
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final KEYTYPE [] aKeys,
                                                                                                                          @Nullable final VALUETYPE [] aValues)
{
  // Are both empty?
  if (ArrayHelper.isEmpty (aKeys) && ArrayHelper.isEmpty (aValues))
    return newSortedMap ();

  // keys OR values may be null here
  if (ArrayHelper.getSize (aKeys) != ArrayHelper.getSize (aValues))
    throw new IllegalArgumentException ("The passed arrays have different length!");

  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  for (int i = 0; i < aKeys.length; ++i)
    ret.put (aKeys[i], aValues[i]);
  return ret;
}
 
Example #7
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final Collection <? extends KEYTYPE> aKeys,
                                                                                                                          @Nullable final Collection <? extends VALUETYPE> aValues)
{
  // Are both empty?
  if (isEmpty (aKeys) && isEmpty (aValues))
    return newSortedMap ();

  // keys OR values may be null here
  if (getSize (aKeys) != getSize (aValues))
    throw new IllegalArgumentException ("Number of keys is different from number of values");

  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  final Iterator <? extends KEYTYPE> itk = aKeys.iterator ();
  final Iterator <? extends VALUETYPE> itv = aValues.iterator ();
  while (itk.hasNext ())
    ret.put (itk.next (), itv.next ());
  return ret;
}
 
Example #8
Source File: MultiHashMapTreeMapBased.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected CommonsTreeMap <KEYTYPE2, VALUETYPE> createNewInnerMap ()
{
  return new CommonsTreeMap <> ();
}
 
Example #9
Source File: PSXPathQueryBinding.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsNavigableMap <String, String> getStringReplacementMap (@Nonnull final List <PSParam> aParams)
{
  final ICommonsNavigableMap <String, String> ret = new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ());
  for (final PSParam aParam : aParams)
    ret.put (PARAM_VARIABLE_PREFIX + aParam.getName (), aParam.getValue ());
  return ret;
}
 
Example #10
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressFBWarnings ("NP_NONNULL_PARAM_VIOLATION")
public void testMakeUnmodifiableNotNull ()
{
  assertNotNull (makeUnmodifiableNotNull ((Collection <?>) null));
  assertNotNull (makeUnmodifiableNotNull ((ICommonsList <?>) null));
  assertNotNull (makeUnmodifiableNotNull ((Set <?>) null));
  assertNotNull (makeUnmodifiableNotNull ((SortedSet <?>) null));
  assertNotNull (makeUnmodifiableNotNull ((Map <?, ?>) null));
  assertNotNull (makeUnmodifiableNotNull ((SortedMap <?, ?>) null));

  final ICommonsCollection <String> c = newList ("s1", "s2");
  assertNotNull (makeUnmodifiableNotNull (c));
  assertNotSame (c, makeUnmodifiableNotNull (c));
  final ICommonsList <String> l = newList ("s1", "s2");
  assertNotNull (makeUnmodifiableNotNull (l));
  assertNotSame (l, makeUnmodifiableNotNull (l));
  final ICommonsSet <String> s = newSet ("s1", "s2");
  assertNotNull (makeUnmodifiableNotNull (s));
  assertNotSame (s, makeUnmodifiableNotNull (s));
  final ICommonsSortedSet <String> ss = new CommonsTreeSet <> (s);
  assertNotNull (makeUnmodifiableNotNull (ss));
  assertNotSame (ss, makeUnmodifiableNotNull (ss));
  final ICommonsMap <String, String> m = newMap ("s1", "s2");
  assertNotNull (makeUnmodifiableNotNull (m));
  assertNotSame (m, makeUnmodifiableNotNull (m));
  final ICommonsSortedMap <String, String> sm = new CommonsTreeMap <> (m);
  assertNotNull (makeUnmodifiableNotNull (sm));
  assertNotSame (sm, makeUnmodifiableNotNull (sm));
}
 
Example #11
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testMakeUnmodifiable ()
{
  assertNull (makeUnmodifiable ((Collection <?>) null));
  assertNull (makeUnmodifiable ((ICommonsList <?>) null));
  assertNull (makeUnmodifiable ((Set <?>) null));
  assertNull (makeUnmodifiable ((SortedSet <?>) null));
  assertNull (makeUnmodifiable ((Map <?, ?>) null));
  assertNull (makeUnmodifiable ((SortedMap <?, ?>) null));

  final ICommonsCollection <String> c = newList ("s1", "s2");
  assertNotNull (makeUnmodifiable (c));
  assertNotSame (c, makeUnmodifiable (c));
  final ICommonsList <String> l = newList ("s1", "s2");
  assertNotNull (makeUnmodifiable (l));
  assertNotSame (l, makeUnmodifiable (l));
  final ICommonsSet <String> s = newSet ("s1", "s2");
  assertNotNull (makeUnmodifiable (s));
  assertNotSame (s, makeUnmodifiable (s));
  final ICommonsSortedSet <String> ss = new CommonsTreeSet <> (s);
  assertNotNull (makeUnmodifiable (ss));
  assertNotSame (ss, makeUnmodifiable (ss));
  final ICommonsMap <String, String> m = newMap ("s1", "s2");
  assertNotNull (makeUnmodifiable (m));
  assertNotSame (m, makeUnmodifiable (m));
  final ICommonsSortedMap <String, String> sm = new CommonsTreeMap <> (m);
  assertNotNull (makeUnmodifiable (sm));
  assertNotSame (sm, makeUnmodifiable (sm));
}
 
Example #12
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final Iterable <? extends Map.Entry <KEYTYPE, VALUETYPE>> aCollection)
{
  if (isEmpty (aCollection))
    return newSortedMap ();

  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  ret.putAll (aCollection);
  return ret;
}
 
Example #13
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final Collection <? extends Map.Entry <KEYTYPE, VALUETYPE>> aCollection)
{
  if (isEmpty (aCollection))
    return newSortedMap ();

  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  ret.putAll (aCollection);
  return ret;
}
 
Example #14
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final Map <? extends KEYTYPE, ? extends VALUETYPE> [] aMaps)
{
  if (aMaps == null || aMaps.length == 0)
    return newSortedMap ();

  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  for (final Map <? extends KEYTYPE, ? extends VALUETYPE> aMap : aMaps)
    ret.putAll (aMap);
  return ret;
}
 
Example #15
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final Map <? extends KEYTYPE, ? extends VALUETYPE> aMap)
{
  if (isEmpty (aMap))
    return newSortedMap ();

  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  ret.putAll (aMap);
  return ret;
}
 
Example #16
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final KEYTYPE aKey,
                                                                                                                          @Nullable final VALUETYPE aValue)
{
  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  ret.put (aKey, aValue);
  return ret;
}
 
Example #17
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap (@Nullable final Map <KEYTYPE, VALUETYPE> aMap,
                                                                                                                          @Nonnull final Predicate <? super Map.Entry <? extends KEYTYPE, ? extends VALUETYPE>> aFilter)
{
  final CommonsTreeMap <KEYTYPE, VALUETYPE> ret = newSortedMap ();
  ret.putAll (aMap, aFilter);
  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 <SRCKEYTYPE, SRCVALUETYPE, DSTKEYTYPE extends Comparable <? super DSTKEYTYPE>, DSTVALUETYPE> CommonsTreeMap <DSTKEYTYPE, DSTVALUETYPE> newSortedMapMapped (@Nullable final Map <? extends SRCKEYTYPE, ? extends SRCVALUETYPE> aMap,
                                                                                                                                                                         @Nonnull final Function <? super SRCKEYTYPE, DSTKEYTYPE> aKeyMapper,
                                                                                                                                                                         @Nonnull final Function <? super SRCVALUETYPE, DSTVALUETYPE> aValueMapper)
{
  return new CommonsTreeMap <> (aMap, aKeyMapper, aValueMapper);
}
 
Example #19
Source File: MultiLinkedHashMapTreeMapBased.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected CommonsTreeMap <KEYTYPE2, VALUETYPE> createNewInnerMap ()
{
  return new CommonsTreeMap <> ();
}
 
Example #20
Source File: MultiTreeMapTreeMapBased.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected CommonsTreeMap <KEYTYPE2, VALUETYPE> createNewInnerMap ()
{
  return new CommonsTreeMap <> ();
}
 
Example #21
Source File: BenchmarkTrie.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public StringMapTreeMap (final String [] aStrings)
{
  super (aStrings);
  m_aMap = new CommonsTreeMap <> ();
}
 
Example #22
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> CommonsTreeMap <KEYTYPE, VALUETYPE> newSortedMap ()
{
  return new CommonsTreeMap <> (Comparator.nullsFirst (Comparator.naturalOrder ()));
}
 
Example #23
Source File: PSXPathVariables.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
private static ICommonsNavigableMap <String, String> _createMap ()
{
  return new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ());
}
 
Example #24
Source File: MainCreateJAXBBinding20.java    From ph-ubl with Apache License 2.0 4 votes vote down vote up
private static void _generateExplicitEnumMapping (@Nonnull final IMicroDocument aDoc,
                                                  @Nonnull @Nonempty final String sFilename,
                                                  @Nonnull final IMicroElement eBindings)
{
  final ICommonsSet <String> aUsedNames = new CommonsHashSet <> ();
  final ICommonsNavigableMap <String, String> aValueToConstants = new CommonsTreeMap <> ();
  final IMicroElement eSimpleType = aDoc.getDocumentElement ().getFirstChildElement ();

  final IMicroElement eInnerBindings = eBindings.appendElement (JAXB_NS_URI, "bindings")
                                                .setAttribute ("node",
                                                               "xsd:simpleType[@name='" +
                                                                       eSimpleType.getAttributeValue ("name") +
                                                                       "']");
  final IMicroElement eTypesafeEnumClass = eInnerBindings.appendElement (JAXB_NS_URI, "typesafeEnumClass");

  final IMicroElement eRestriction = eSimpleType.getFirstChildElement ();
  for (final IMicroElement eEnumeration : eRestriction.getAllChildElements (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                            "enumeration"))
  {
    final IMicroElement eAnnotation = eEnumeration.getFirstChildElement (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                         "annotation");
    if (eAnnotation == null)
      throw new IllegalStateException ("annotation is missing");
    final IMicroElement eDocumentation = eAnnotation.getFirstChildElement (XMLConstants.W3C_XML_SCHEMA_NS_URI,
                                                                           "documentation");
    if (eDocumentation == null)
      throw new IllegalStateException ("documentation is missing");
    final IMicroElement eCodeName = eDocumentation.getFirstChildElement ("urn:un:unece:uncefact:documentation:2",
                                                                         "CodeName");
    if (eCodeName == null)
      throw new IllegalStateException ("CodeName is missing");

    final String sValue = eEnumeration.getAttributeValue ("value");
    // Create an upper case Java identifier, without duplicate "_"
    String sCodeName = RegExHelper.getAsIdentifier (eCodeName.getTextContent ().trim ().toUpperCase (Locale.US))
                                  .replaceAll ("_+", "_");

    if (!aUsedNames.add (sCodeName))
    {
      // Ensure uniqueness of the enum member name
      int nSuffix = 1;
      while (true)
      {
        final String sSuffixedCodeName = sCodeName + "_" + nSuffix;
        if (aUsedNames.add (sSuffixedCodeName))
        {
          sCodeName = sSuffixedCodeName;
          break;
        }
        ++nSuffix;
      }
    }

    eTypesafeEnumClass.appendElement (JAXB_NS_URI, "typesafeEnumMember")
                      .setAttribute ("value", sValue)
                      .setAttribute ("name", sCodeName);
    aValueToConstants.put (sValue, sCodeName);
  }

  // Write out the mapping file for easy later-on resolving
  XMLMapHandler.writeMap (aValueToConstants,
                          new FileSystemResource ("src/main/resources/schemas/" + sFilename + ".mapping"));
}