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

The following examples show how to use com.helger.commons.collection.impl.CommonsLinkedHashMap. 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: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static <ELEMENTTYPE> CommonsLinkedHashMap <ELEMENTTYPE, ELEMENTTYPE> newOrderedMap (@Nullable final ELEMENTTYPE... aValues)
{
  if (ArrayHelper.isEmpty (aValues))
    return newOrderedMap (0);

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

  final CommonsLinkedHashMap <ELEMENTTYPE, ELEMENTTYPE> ret = newOrderedMap (aValues.length / 2);
  for (int i = 0; i < aValues.length; i += 2)
    ret.put (aValues[i], aValues[i + 1]);
  return ret;
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
Source File: ConfigurationSourceJson.java    From ph-commons with 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 #7
Source File: GraphNode.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public EChange addRelation (@Nullable final IMutableGraphRelation aRelation)
{
  if (aRelation == null)
    return EChange.UNCHANGED;
  if (!aRelation.isRelatedTo (this))
    throw new IllegalArgumentException ("Relation is not suitable for this node!");

  final String sRelationID = aRelation.getID ();
  if (m_aRelations == null)
    m_aRelations = new CommonsLinkedHashMap <> ();
  else
    if (m_aRelations.containsKey (sRelationID))
      return EChange.UNCHANGED;

  m_aRelations.put (sRelationID, aRelation);
  return EChange.CHANGED;
}
 
Example #8
Source File: DirectedGraphNode.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public void addIncomingRelation (@Nonnull final IMutableDirectedGraphRelation aNewRelation)
{
  ValueEnforcer.notNull (aNewRelation, "NewRelation");
  ValueEnforcer.isTrue (aNewRelation.getTo () == this, "Passed incoming relation is not based on this node");
  if (m_aIncoming != null)
  {
    if (m_aIncoming.containsKey (aNewRelation.getID ()))
      throw new IllegalArgumentException ("The passed relation (" +
                                          aNewRelation +
                                          ") is already contained as an incoming relation");

    // check if the relation from-node is already contained
    for (final IMutableDirectedGraphRelation aRelation : m_aIncoming.values ())
      if (aRelation.getFrom () == aNewRelation.getFrom ())
        throw new IllegalArgumentException ("The from-node of the passed relation (" +
                                            aNewRelation +
                                            ") is already contained");
  }
  else
  {
    m_aIncoming = new CommonsLinkedHashMap <> ();
  }

  // Add!
  m_aIncoming.put (aNewRelation.getID (), aNewRelation);
}
 
Example #9
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve a map that is ordered in the way the parameter arrays are passed
 * in. Note that key and value arrays need to have the same length.
 *
 * @param <KEYTYPE>
 *        The key type.
 * @param <VALUETYPE>
 *        The value type.
 * @param aKeys
 *        The key array to use. May not be <code>null</code>.
 * @param aValues
 *        The value array to use. May not be <code>null</code>.
 * @return A {@link CommonsLinkedHashMap} containing the passed key-value
 *         entries. Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final KEYTYPE [] aKeys,
                                                                                            @Nullable final VALUETYPE [] aValues)
{
  final int nKeys = ArrayHelper.getSize (aKeys);
  final int nValues = ArrayHelper.getSize (aValues);

  // Check for identical size
  if (nKeys != nValues)
    throw new IllegalArgumentException ("The passed arrays have different length (" +
                                        nKeys +
                                        " keys and " +
                                        nValues +
                                        " values)!");

  // Are both empty?
  if (nKeys == 0)
    return newOrderedMap (0);

  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap (nKeys);
  for (int i = 0; i < aKeys.length; ++i)
    ret.put (aKeys[i], aValues[i]);
  return ret;
}
 
Example #10
Source File: DirectedGraphNode.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public void addOutgoingRelation (@Nonnull final IMutableDirectedGraphRelation aNewRelation)
{
  ValueEnforcer.notNull (aNewRelation, "NewRelation");
  ValueEnforcer.isTrue (aNewRelation.getFrom () == this, "Passed outgoing relation is not based on this node");
  if (m_aOutgoing != null)
  {
    if (m_aOutgoing.containsKey (aNewRelation.getID ()))
      throw new IllegalArgumentException ("The passed relation " +
                                          aNewRelation +
                                          " is already contained as an outgoing relation");
    // check if the relation to-node is already contained
    for (final IMutableDirectedGraphRelation aRelation : m_aOutgoing.values ())
      if (aRelation.getTo () == aNewRelation.getTo ())
        throw new IllegalArgumentException ("The to-node of the passed relation " +
                                            aNewRelation +
                                            " is already contained");
  }
  else
  {
    m_aOutgoing = new CommonsLinkedHashMap <> ();
  }

  // Add!
  m_aOutgoing.put (aNewRelation.getID (), aNewRelation);
}
 
Example #11
Source File: PSNS.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #12
Source File: PSSchema.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #13
Source File: MultiTreeMapLinkedHashMapBased.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@ReturnsMutableCopy
protected CommonsLinkedHashMap <KEYTYPE2, VALUETYPE> createNewInnerMap ()
{
  return new CommonsLinkedHashMap <> ();
}
 
Example #14
Source File: PSAssertReport.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #15
Source File: PSExtends.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #16
Source File: PSName.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #17
Source File: PSDiagnostic.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #18
Source File: PSDiagnostics.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #19
Source File: PSActive.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #20
Source File: PSRule.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #21
Source File: PSValueOf.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #22
Source File: PSDir.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #23
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final Collection <? extends KEYTYPE> aKeys,
                                                                                            @Nullable final Collection <? extends VALUETYPE> aValues)
{
  final int nKeys = getSize (aKeys);
  final int nValues = getSize (aValues);

  // Check for identical size
  if (nKeys != nValues)
    throw new IllegalArgumentException ("The passed arrays have different length (" +
                                        nKeys +
                                        " keys and " +
                                        nValues +
                                        " values)!");

  // Are both empty?
  if (nKeys == 0)
    return newOrderedMap (0);

  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap (nKeys);
  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 #24
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final Map <? extends KEYTYPE, ? extends VALUETYPE> aMap)
{
  if (isEmpty (aMap))
    return newOrderedMap (0);
  return new CommonsLinkedHashMap <> (aMap);
}
 
Example #25
Source File: PSPhase.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
{
  ValueEnforcer.notNull (sAttrName, "AttrName");
  ValueEnforcer.notNull (sAttrValue, "AttrValue");
  if (m_aForeignAttrs == null)
    m_aForeignAttrs = new CommonsLinkedHashMap <> ();
  m_aForeignAttrs.put (sAttrName, sAttrValue);
}
 
Example #26
Source File: CollectionHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsEmptyCollection ()
{
  assertTrue (isEmpty ((Collection <?>) null));
  assertTrue (isEmpty ((Map <?, ?>) null));
  assertTrue (isEmpty (new CommonsArrayList <String> ()));
  assertTrue (isEmpty (new CommonsVector <String> ()));
  assertTrue (isEmpty (new CommonsHashSet <String> ()));
  assertTrue (isEmpty (new CommonsTreeSet <String> ()));
  assertTrue (isEmpty (new CommonsHashMap <String, String> ()));
  assertTrue (isEmpty (new CommonsLinkedHashMap <String, String> ()));

  assertFalse (isEmpty (newList ("Hallo")));
  assertFalse (isEmpty (newMap ("Hallo", "Welt")));
}
 
Example #27
Source File: PSRule.java    From ph-schematron with 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 #28
Source File: CharsetHelper.java    From ph-commons with 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 #29
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final Iterable <? extends Map.Entry <KEYTYPE, VALUETYPE>> aCollection)
{
  if (isEmpty (aCollection))
    return newOrderedMap (0);

  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap ();
  ret.putAll (aCollection);
  return ret;
}
 
Example #30
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <KEYTYPE, VALUETYPE> CommonsLinkedHashMap <KEYTYPE, VALUETYPE> newOrderedMap (@Nullable final Map <? extends KEYTYPE, ? extends VALUETYPE> [] aMaps)
{
  if (ArrayHelper.isEmpty (aMaps))
    return newOrderedMap (0);

  final CommonsLinkedHashMap <KEYTYPE, VALUETYPE> ret = newOrderedMap ();
  for (final Map <? extends KEYTYPE, ? extends VALUETYPE> aMap : aMaps)
    ret.putAll (aMap);
  return ret;
}