Java Code Examples for com.helger.commons.collection.impl.ICommonsMap#put()

The following examples show how to use com.helger.commons.collection.impl.ICommonsMap#put() . 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: MultiConfigurationValueProviderTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic ()
{
  final MultiConfigurationValueProvider aMCSVP = new MultiConfigurationValueProvider ();

  // Lower priority
  final ICommonsMap <String, String> aMap1 = new CommonsHashMap <> ();
  aMap1.put ("key1", "value1");
  aMap1.put ("key2", "value2");
  aMCSVP.addConfigurationSource (new ConfigurationSourceFunction (110, aMap1::get));

  // Higher priority - should be returned
  final ICommonsMap <String, String> aMap2 = new CommonsHashMap <> ();
  aMap2.put ("key1", "value2");
  aMap2.put ("key3", "value3");
  aMCSVP.addConfigurationSource (new ConfigurationSourceFunction (111, aMap2::get));

  // Resolve
  assertEquals ("value2", aMCSVP.getConfigurationValue ("key1").getValue ());
  assertEquals ("value2", aMCSVP.getConfigurationValue ("key2").getValue ());
  assertEquals ("value3", aMCSVP.getConfigurationValue ("key3").getValue ());
  assertNull (aMCSVP.getConfigurationValue ("key4"));
}
 
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: StringHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testReplaceMultipleMap ()
{
  final ICommonsMap <String, String> aMap = new CommonsHashMap <> ();
  aMap.put ("Hallo", "Hi");
  aMap.put ("Welt", "world");
  aMap.put ("!", "???");
  assertEquals ("Abc die Katze lief im Schnee", StringHelper.replaceMultiple ("Abc die Katze lief im Schnee", aMap));
  assertEquals ("Hi Katze", StringHelper.replaceMultiple ("Hallo Katze", aMap));
  assertEquals ("Moin world", StringHelper.replaceMultiple ("Moin Welt", aMap));
  assertEquals ("Moin welt", StringHelper.replaceMultiple ("Moin welt", aMap));
  assertEquals ("Hi", StringHelper.replaceMultiple ("Hallo", aMap));
  assertEquals ("Hi Hi", StringHelper.replaceMultiple ("Hallo Hallo", aMap));
  assertEquals ("HiHiHi", StringHelper.replaceMultiple ("HalloHalloHallo", aMap));
  assertEquals ("Hi world???", StringHelper.replaceMultiple ("Hallo Welt!", aMap));
  assertEquals ("Hi world???Hi world???", StringHelper.replaceMultiple ("Hallo Welt!Hallo Welt!", aMap));
}
 
Example 7
Source File: Schematron2XSLTMojoFuncTest.java    From ph-schematron with Apache License 2.0 6 votes vote down vote up
@Test
public void testMavenPlugin () throws MojoExecutionException, MojoFailureException
{
  expect (project.getBasedir ()).andReturn (new File (".")).anyTimes ();
  replay (project);

  // Note: default values are not used here
  OUT.setSchematronDirectory (new File ("src/test/resources/schematron"));
  OUT.setSchematronPattern ("**/*.sch");
  OUT.setXsltDirectory (new File ("target/test/schematron-via-maven-plugin"));
  OUT.setXsltExtension (".xslt");

  final ICommonsMap <String, String> aParams = new CommonsHashMap <> ();
  aParams.put ("allow-foreign", "true");
  OUT.setParameters (aParams);

  OUT.execute ();

  verify (project);
}
 
Example 8
Source File: PropertiesHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static ICommonsMap <String, String> getAsStringMap (@Nonnull final Properties aProps)
{
  ValueEnforcer.notNull (aProps, "Props");

  final ICommonsMap <String, String> ret = new CommonsHashMap <> ();
  for (final Map.Entry <Object, Object> aEntry : aProps.entrySet ())
    ret.put ((String) aEntry.getKey (), (String) aEntry.getValue ());
  return ret;
}
 
Example 9
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplex ()
{
  final ICommonsMap <ICommonsList <String>, ICommonsSet <String>> aMap = new CommonsHashMap <> ();
  aMap.put (new CommonsArrayList <> ("a", "b", "c"), new CommonsHashSet <> ("a", "b", "c"));
  aMap.put (new CommonsArrayList <> ("a", "b", "d"), new CommonsHashSet <> ("a", "b", "d"));
  assertTrue (EqualsHelper.equalsCollection (aMap, CollectionHelper.newMap (aMap)));

  assertFalse (EqualsHelper.equalsCollection (aMap, ArrayHelper.newArray ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aMap, new CommonsHashSet <> ("a", "b", "c")));
  final ICommonsMap <String, String> aMap1a = new CommonsHashMap <> ();
  aMap1a.put ("a", "b");
  assertFalse (EqualsHelper.equalsCollection (aMap, aMap1a));
  final ICommonsMap <ICommonsList <String>, String> aMap2 = new CommonsHashMap <> ();
  aMap2.put (new CommonsArrayList <> ("a", "b", "c"), "d");
  aMap2.put (new CommonsArrayList <> ("a", "b", "d"), "e");
  aMap2.put (new CommonsArrayList <> ("a", "b", "e"), null);
  aMap2.put (null, "g");
  assertFalse (EqualsHelper.equalsCollection (aMap, aMap2));
  assertFalse (EqualsHelper.equalsCollection (aMap2, aMap));
  final ICommonsMap <String, ICommonsList <String>> aMap3 = new CommonsHashMap <> ();
  aMap3.put ("d", new CommonsArrayList <> ("a", "b", "c"));
  aMap3.put ("e", new CommonsArrayList <> ("a", "b", "d"));
  aMap3.put (null, new CommonsArrayList <> ("a", "b", "e"));
  aMap3.put ("g", null);
  assertFalse (EqualsHelper.equalsCollection (aMap, aMap3));
  assertFalse (EqualsHelper.equalsCollection (aMap3, aMap));
}
 
Example 10
Source File: FontKerningFuncTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testKerning () throws IOException
{
  if (EOperatingSystem.getCurrentOS ().isWindowsBased ())
  {
    // required to get graphics up and running...
    GraphicsEnvironment.getLocalGraphicsEnvironment ();
    final int nFontSize = 25;

    final ICommonsMap <TextAttribute, Object> aTextAttributes = new CommonsHashMap <> ();
    aTextAttributes.put (TextAttribute.FAMILY, "Arial");
    aTextAttributes.put (TextAttribute.SIZE, Float.valueOf (nFontSize));
    final Font aFont = Font.getFont (aTextAttributes);

    final char [] aChars = "T,".toCharArray ();
    final GlyphVector aGlyphVector = aFont.layoutGlyphVector (new FontRenderContext (new AffineTransform (),
                                                                                     false,
                                                                                     true),
                                                              aChars,
                                                              0,
                                                              aChars.length,
                                                              Font.LAYOUT_LEFT_TO_RIGHT);
    final int tCode = aGlyphVector.getGlyphCode (0);
    final int commaCode = aGlyphVector.getGlyphCode (1);
    final Kerning aKerning = new Kerning (new FileInputStream (System.getenv ("windir") + "/fonts/ARIAL.TTF"));
    LOGGER.info (Float.toString (aKerning.getValue (tCode, commaCode, nFontSize)));
  }
  else
    LOGGER.warn ("Works only on Windows!");
}
 
Example 11
Source File: TextHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateMultilingualTextFromMap ()
{
  final ICommonsMap <String, String> aParamNames = new CommonsHashMap <> ();
  IMultilingualText aMLT = TextHelper.createMultilingualTextFromMap (aParamNames);
  assertEquals (0, aMLT.texts ().size ());

  aParamNames.put ("de", "x");
  aParamNames.put ("en", "y");
  aMLT = TextHelper.createMultilingualTextFromMap (aParamNames);
  assertEquals (2, aMLT.texts ().size ());
  assertEquals ("x", aMLT.getText (L_DE));
  assertEquals ("y", aMLT.getText (L_EN));
}
 
Example 12
Source File: PSXPathBoundSchema.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
@Nullable
private ICommonsMap <String, PSXPathBoundDiagnostic> _createBoundDiagnostics (@Nonnull final XPath aXPathContext,
                                                                              @Nonnull final PSXPathVariables aGlobalVariables)
{
  final ICommonsMap <String, PSXPathBoundDiagnostic> ret = new CommonsHashMap <> ();
  boolean bHasAnyError = false;

  final PSSchema aSchema = getOriginalSchema ();
  if (aSchema.hasDiagnostics ())
  {
    // For all contained diagnostic elements
    for (final PSDiagnostic aDiagnostic : aSchema.getDiagnostics ().getAllDiagnostics ())
    {
      final ICommonsList <PSXPathBoundElement> aBoundElements = _createBoundElements (aDiagnostic,
                                                                                      aXPathContext,
                                                                                      aGlobalVariables);
      if (aBoundElements == null)
      {
        // error already emitted
        bHasAnyError = true;
      }
      else
      {
        final PSXPathBoundDiagnostic aBoundDiagnostic = new PSXPathBoundDiagnostic (aDiagnostic, aBoundElements);
        if (ret.put (aDiagnostic.getID (), aBoundDiagnostic) != null)
        {
          error (aDiagnostic, "A diagnostic element with ID '" + aDiagnostic.getID () + "' was overwritten!");
          bHasAnyError = true;
        }
      }
    }
  }

  if (bHasAnyError)
    return null;

  return ret;
}
 
Example 13
Source File: CollectionHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get a map consisting only of a set of specified keys. If an element from
 * the key set is not contained in the original map, the key is ignored.
 *
 * @param <KEY>
 *        Source map key type.
 * @param <VALUE>
 *        Source map value type.
 * @param aValues
 *        Source map to filter. May not be <code>null</code>.
 * @param aKeys
 *        The filter set to filter the entries from. May not be
 *        <code>null</code>.
 * @return A non-<code>null</code> map containing only the elements from the
 *         specified key set.
 */
@Nullable
@ReturnsMutableCopy
public static <KEY, VALUE> ICommonsMap <KEY, VALUE> getFilteredMap (@Nullable final ICommonsMap <KEY, VALUE> aValues,
                                                                    @Nullable final Collection <? extends KEY> aKeys)
{
  if (isEmpty (aValues) || isEmpty (aKeys))
    return null;

  final ICommonsMap <KEY, VALUE> ret = newMap ();
  for (final KEY aKey : aKeys)
    if (aValues.containsKey (aKey))
      ret.put (aKey, aValues.get (aKey));
  return ret;
}