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

The following examples show how to use com.helger.commons.collection.impl.ICommonsOrderedMap. 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: ConfigurationSourceJson.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
private static ICommonsOrderedMap <String, String> _load (@Nonnull final IReadableResource aRes, @Nonnull final Charset aCharset)
{
  final JsonReader.Builder aBuilder = JsonReader.builder ()
                                                .setSource (aRes, aCharset)
                                                .setCustomizeCallback (aParser -> aParser.setRequireStringQuotes (false)
                                                                                         .setAllowSpecialCharsInStrings (true)
                                                                                         .setAlwaysUseBigNumber (true)
                                                                                         .setTrackPosition (true))
                                                .setCustomExceptionCallback (ex -> LOGGER.error ("Failed to parse '" +
                                                                                                 aRes.getPath () +
                                                                                                 "' to JSON: " +
                                                                                                 ex.getMessage ()));
  final IJsonObject aProps = aBuilder.hasSource () ? aBuilder.readAsObject () : null;
  if (aProps == null)
    return null;

  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  for (final Map.Entry <String, IJson> aEntry : aProps)
    _recursiveFlattenJson (aEntry.getKey (), aEntry.getValue (), ret);
  return ret;
}
 
Example 2
Source Project: ph-commons   Source File: JsonWriterTest.java    License: 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 3
Source Project: ph-commons   Source File: JsonWriterTest.java    License: 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 4
Source Project: ph-commons   Source File: JsonConverterTest.java    License: 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 5
Source Project: ph-commons   Source File: CollectionHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get a map sorted by its keys. The comparison order is defined by the passed
 * comparator object.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        The map to sort. May not be <code>null</code>.
 * @param aKeyComparator
 *        The comparator to be used. May not be <code>null</code>.
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByKey (@Nullable final Map <KEYTYPE, VALUETYPE> aMap,
                                                                                           @Nonnull final Comparator <? super KEYTYPE> aKeyComparator)
{
  ValueEnforcer.notNull (aKeyComparator, "KeyComparator");

  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted Map.Entry list by Entry.getValue ()
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getKey, aKeyComparator));
  return newOrderedMap (aList);
}
 
Example 6
Source Project: ph-commons   Source File: CollectionHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get a map sorted by aIter's values. The comparison order is defined by the
 * passed comparator object.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        The map to sort. May not be <code>null</code>.
 * @param aValueComparator
 *        The comparator to be used. May not be <code>null</code>.
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByValue (@Nullable final Map <KEYTYPE, VALUETYPE> aMap,
                                                                                             @Nonnull final Comparator <? super VALUETYPE> aValueComparator)
{
  ValueEnforcer.notNull (aValueComparator, "ValueComparator");

  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted Map.Entry list by Entry.getValue ()
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getValue, aValueComparator));
  return newOrderedMap (aList);
}
 
Example 7
Source Project: ph-commons   Source File: Graph.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, IMutableGraphRelation> getAllRelations ()
{
  final ICommonsOrderedMap <String, IMutableGraphRelation> ret = new CommonsLinkedHashMap <> ();
  for (final IMutableGraphNode aNode : m_aNodes.values ())
    aNode.forEachRelation (x -> ret.put (x.getID (), x));
  return ret;
}
 
Example 8
Source Project: ph-commons   Source File: DirectedGraph.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, IMutableDirectedGraphRelation> getAllRelations ()
{
  final ICommonsOrderedMap <String, IMutableDirectedGraphRelation> ret = new CommonsLinkedHashMap <> ();
  for (final IMutableDirectedGraphNode aNode : m_aNodes.values ())
    aNode.forEachRelation (x -> ret.put (x.getID (), x));
  return ret;
}
 
Example 9
Source Project: ph-commons   Source File: ConfigurationSourceJson.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public ESuccess reload ()
{
  // Main load
  final ICommonsOrderedMap <String, String> aProps = _load (getResource (), m_aCharset);
  // Replace in write-lock
  m_aRWLock.writeLockedGet ( () -> m_aProps = aProps);
  return ESuccess.valueOf (aProps != null);
}
 
Example 10
Source Project: ph-commons   Source File: XMLHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static ICommonsOrderedMap <String, String> getAllAttributesAsMap (@Nullable final Element aSrcNode)
{
  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  // Cast needed for Oracle JDK 8
  forAllAttributes (aSrcNode, (BiConsumer <? super String, ? super String>) ret::put);
  return ret;
}
 
Example 11
Source Project: ph-commons   Source File: MapBasedXPathFunctionResolver.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return A mutable copy of all contained functions. Never <code>null</code>
 *         but maybe empty.
 */
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <XPathFunctionKey, XPathFunction> getAllFunctions ()
{
  return m_aMap.getClone ();
}
 
Example 12
Source Project: ph-commons   Source File: XMLResourceBundle.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static ICommonsOrderedMap <String, String> readFromPropertiesXML (@Nonnull @WillClose final InputStream aIS)
{
  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  final IMicroDocument aDoc = MicroReader.readMicroXML (aIS);
  if (aDoc != null)
    for (final IMicroElement eChild : aDoc.getDocumentElement ().getAllChildElements ("entry"))
      ret.put (eChild.getAttributeValue ("key"), eChild.getTextContent ());
  return ret;
}
 
Example 13
Source Project: ph-commons   Source File: MicroElement.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@ReturnsMutableCopy
public ICommonsOrderedMap <IMicroQName, String> getAllQAttributes ()
{
  if (hasNoAttributes ())
    return null;
  return new CommonsLinkedHashMap <> (m_aAttrs.values (),
                                      IMicroAttribute::getAttributeQName,
                                      IMicroAttribute::getAttributeValue);
}
 
Example 14
Source Project: ph-commons   Source File: JsonObject.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, IJson> getClonedValues ()
{
  final ICommonsOrderedMap <String, IJson> ret = new CommonsLinkedHashMap <> ();
  for (final Map.Entry <String, IJson> aEntry : m_aValues.entrySet ())
    ret.put (aEntry.getKey (), aEntry.getValue ().getClone ());
  return ret;
}
 
Example 15
Source Project: ph-commons   Source File: IErrorList.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param aKeyExtractor
 *        the key extractor by which the result is grouped.
 * @return A map with all items mapped from a key to its occurrences.
 * @param <T>
 *        Return list key type
 */
@Nonnull
@ReturnsMutableCopy
default <T> ICommonsOrderedMap <T, ICommonsList <IError>> getGrouped (@Nonnull final Function <? super IError, T> aKeyExtractor)
{
  final ICommonsOrderedMap <T, ICommonsList <IError>> ret = new CommonsLinkedHashMap <> ();
  // create a list for each key, and add the respective entry
  forEach (x -> ret.computeIfAbsent (aKeyExtractor.apply (x), k -> new CommonsArrayList <> ()).add (x));
  return ret;
}
 
Example 16
Source Project: ph-commons   Source File: URLParameterList.java    License: 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 17
Source Project: ph-commons   Source File: CollectionHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get a map sorted by aIter's keys. Because no comparator is defined, the key
 * type needs to implement the {@link java.lang.Comparable} interface.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        the map to sort
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE extends Comparable <? super KEYTYPE>, VALUETYPE> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByKey (@Nullable final Map <KEYTYPE, VALUETYPE> aMap)
{
  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted entry list
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getKey));
  return newOrderedMap (aList);
}
 
Example 18
Source Project: ph-commons   Source File: CollectionHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get a map sorted by its values. Because no comparator is defined, the value
 * type needs to implement the {@link java.lang.Comparable} interface.
 *
 * @param <KEYTYPE>
 *        map key type
 * @param <VALUETYPE>
 *        map value type
 * @param aMap
 *        The map to sort. May not be <code>null</code>.
 * @return the sorted map and never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE extends Comparable <? super VALUETYPE>> ICommonsOrderedMap <KEYTYPE, VALUETYPE> getSortedByValue (@Nullable final Map <KEYTYPE, VALUETYPE> aMap)
{
  if (isEmpty (aMap))
    return newOrderedMap (0);

  // get sorted entry list
  final ICommonsList <Map.Entry <KEYTYPE, VALUETYPE>> aList = newList (aMap.entrySet ());
  aList.sort (Comparator.comparing (Map.Entry::getValue));
  return newOrderedMap (aList);
}
 
Example 19
Source Project: ph-commons   Source File: CharsetHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return An immutable collection of all available charsets from the standard
 *         charset provider.
 */
@Nonnull
@ReturnsMutableCopy
public static ICommonsOrderedMap <String, Charset> getAllCharsets ()
{
  return new CommonsLinkedHashMap <> (s_aAllCharsets);
}
 
Example 20
Source Project: ph-commons   Source File: BenchmarkStringMultiReplace.java    License: Apache License 2.0 5 votes vote down vote up
public void run ()
{
  final ICommonsOrderedMap <String, String> aMap = new CommonsLinkedHashMap <> ();
  for (int i = 0; i < RSRC.length; ++i)
    aMap.put (RSRC[i], RDST[i]);

  String s = "";
  for (int i = 0; i < m_nRuns; i++)
    s = StringHelper.replaceMultiple (SRC, aMap);
  if (!s.equals (DST))
    throw new IllegalStateException (s);
}
 
Example 21
Source Project: ph-schematron   Source File: PSPhase.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getAllLetsAsMap ()
{
  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  for (final Object aElement : m_aContent)
    if (aElement instanceof PSLet)
    {
      final PSLet aLet = (PSLet) aElement;
      ret.put (aLet.getName (), aLet.getValue ());
    }
  return ret;
}
 
Example 22
Source Project: ph-schematron   Source File: PSSchema.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getAllLetsAsMap ()
{
  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  for (final PSLet aLet : m_aLets)
    ret.put (aLet.getName (), aLet.getValue ());
  return ret;
}
 
Example 23
Source Project: ph-schematron   Source File: PSPattern.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getAllLetsAsMap ()
{
  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  for (final Object aContent : m_aContent)
    if (aContent instanceof PSLet)
    {
      final PSLet aLet = (PSLet) aContent;
      ret.put (aLet.getName (), aLet.getValue ());
    }
  return ret;
}
 
Example 24
Source Project: ph-schematron   Source File: PSRule.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getAllLetsAsMap ()
{
  final ICommonsOrderedMap <String, String> ret = new CommonsLinkedHashMap <> ();
  for (final Object aContent : m_aContent)
    if (aContent instanceof PSLet)
    {
      final PSLet aLet = (PSLet) aContent;
      ret.put (aLet.getName (), aLet.getValue ());
    }
  return ret;
}
 
Example 25
Source Project: ph-css   Source File: MainReadAllCSSOnDisc.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressFBWarnings ("DMI_HARDCODED_ABSOLUTE_FILENAME")
public static void main (final String [] args)
{
  int nFilesOK = 0;
  int nFilesError = 0;
  final ICommonsOrderedMap <File, ParseException> aErrors = new CommonsLinkedHashMap<> ();
  final Wrapper <File> aCurrentFile = new Wrapper<> ();
  final ICSSParseExceptionCallback aHdl = ex -> aErrors.put (aCurrentFile.get (), ex);
  for (final File aFile : new FileSystemRecursiveIterator (new File ("/")).withFilter (IFileFilter.filenameEndsWith (".css")))
  {
    if (false)
      LOGGER.info (aFile.getAbsolutePath ());
    aCurrentFile.set (aFile);
    final CascadingStyleSheet aCSS = CSSReader.readFromFile (aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30, aHdl);
    if (aCSS == null)
    {
      nFilesError++;
      LOGGER.warn ("  " + aFile.getAbsolutePath () + " failed!");
    }
    else
      nFilesOK++;
  }

  LOGGER.info ("Done");
  for (final Map.Entry <File, ParseException> aEntry : aErrors.entrySet ())
    LOGGER.info ("  " + aEntry.getKey ().getAbsolutePath () + ":\n" + aEntry.getValue ().getMessage () + "\n");
  LOGGER.info ("OK: " + nFilesOK + "; Error: " + nFilesError);
}
 
Example 26
Source Project: ph-commons   Source File: AbstractBaseGraph.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, NODETYPE> getAllNodes ()
{
  return m_aNodes.getClone ();
}
 
Example 27
Source Project: ph-commons   Source File: ConfigurationSourceProperties.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getAllConfigItems ()
{
  return m_aRWLock.readLockedGet ( () -> new CommonsLinkedHashMap <> (m_aProps));
}
 
Example 28
Source Project: ph-commons   Source File: ConfigurationSourceJson.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getAllConfigItems ()
{
  return m_aRWLock.readLockedGet ( () -> new CommonsLinkedHashMap <> (m_aProps));
}
 
Example 29
Source Project: ph-commons   Source File: MapBasedNamespaceContext.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, String> getPrefixToNamespaceURIMap ()
{
  return m_aPrefix2NS.getClone ();
}
 
Example 30
Source Project: ph-commons   Source File: JsonObject.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public ICommonsOrderedMap <String, IJson> getAll ()
{
  return m_aValues.getClone ();
}