Java Code Examples for com.helger.commons.wrapper.Wrapper

The following examples show how to use com.helger.commons.wrapper.Wrapper. 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: AbstractMapBasedWALDAO.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
protected EChange onRead (@Nonnull final IMicroDocument aDoc)
{
  // Read all child elements independent of the name - soft migration
  final Class <IMPLTYPE> aDataTypeClass = getDataTypeClass ();
  final Wrapper <EChange> aChange = new Wrapper <> (EChange.UNCHANGED);

  aDoc.getDocumentElement ().forAllChildElements (m_aReadElementFilter, eItem -> {
    final IMPLTYPE aItem = MicroTypeConverter.convertToNative (eItem, aDataTypeClass);
    _addItem (aItem, EDAOActionType.CREATE);
    if (aItem instanceof IDAOReadChangeAware)
      if (((IDAOReadChangeAware) aItem).isReadChanged ())
      {
        // Remember that something was changed while reading
        aChange.set (EChange.CHANGED);
      }
  });
  return aChange.get ();
}
 
Example 2
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Put a new value into the cache.
 *
 * @param aCacheKey
 *        The cache key. May not be <code>null</code>.
 * @param aCacheValue
 *        The cache value. May not be <code>null</code>.
 */
@MustBeLocked (ELockType.WRITE)
protected final void putInCacheNotLocked (@Nonnull final KEYSTORETYPE aCacheKey,
                                          @Nonnull final Wrapper <VALUETYPE> aCacheValue)
{
  ValueEnforcer.notNull (aCacheKey, "CacheKey");
  ValueEnforcer.notNull (aCacheValue, "CacheValue");

  // try again in write lock
  if (m_aCache == null)
  {
    // Lazily create a new map to cache the objects
    m_aCache = createCache ();
    if (m_aCache == null)
      throw new IllegalStateException (_getCacheLogText () + "Failed to create internal Map!");
  }
  m_aCache.put (aCacheKey, aCacheValue);
}
 
Example 3
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParseDouble ()
{
  final double dDefault = 3.145667;

  // Object
  CommonsAssert.assertEquals (dDefault, StringParser.parseDouble ((Object) null, dDefault));
  CommonsAssert.assertEquals (1, StringParser.parseDouble (BigDecimal.ONE, dDefault));
  CommonsAssert.assertEquals (dDefault, StringParser.parseDouble (new Wrapper <> ("any"), dDefault));

  // String
  CommonsAssert.assertEquals (dDefault, StringParser.parseDouble ((String) null, dDefault));
  CommonsAssert.assertEquals (dDefault, StringParser.parseDouble ("", dDefault));
  CommonsAssert.assertEquals (1.2, StringParser.parseDouble ("1.2", dDefault));
  CommonsAssert.assertEquals (-1.23456, StringParser.parseDouble ("-1.23456", dDefault));
  CommonsAssert.assertEquals (dDefault, StringParser.parseDouble ("bla", dDefault));
}
 
Example 4
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParseDoubleObj ()
{
  final Double aDefault = Double.valueOf (3.145667);

  // Object
  assertNull (StringParser.parseDoubleObj ((Object) null));
  assertEquals (aDefault, StringParser.parseDoubleObj ((Object) null, aDefault));
  CommonsAssert.assertEquals (1, StringParser.parseDoubleObj (BigDecimal.ONE, aDefault));
  assertEquals (aDefault, StringParser.parseDoubleObj (new Wrapper <> ("any"), aDefault));

  // String
  assertNull (StringParser.parseDoubleObj ("foo"));
  assertEquals (aDefault, StringParser.parseDoubleObj ((String) null, aDefault));
  assertEquals (aDefault, StringParser.parseDoubleObj ("", aDefault));
  CommonsAssert.assertEquals (1.2, StringParser.parseDoubleObj ("1.2", aDefault));
  CommonsAssert.assertEquals (-1.23456, StringParser.parseDoubleObj ("-1.23456", aDefault));
  assertEquals (aDefault, StringParser.parseDoubleObj ("bla", aDefault));
}
 
Example 5
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParseFloat ()
{
  final float fDefault = 3.145667f;

  // Object
  CommonsAssert.assertEquals (fDefault, StringParser.parseFloat ((Object) null, fDefault));
  CommonsAssert.assertEquals (1, StringParser.parseFloat (BigDecimal.ONE, fDefault));
  CommonsAssert.assertEquals (fDefault, StringParser.parseFloat (new Wrapper <> ("any"), fDefault));

  // String
  CommonsAssert.assertEquals (fDefault, StringParser.parseFloat ((String) null, fDefault));
  CommonsAssert.assertEquals (fDefault, StringParser.parseFloat ("", fDefault));
  CommonsAssert.assertEquals (1.2, StringParser.parseFloat ("1.2", fDefault));
  CommonsAssert.assertEquals (-1.23456, StringParser.parseFloat ("-1.23456", fDefault));
  CommonsAssert.assertEquals (fDefault, StringParser.parseFloat ("bla", fDefault));
}
 
Example 6
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParseFloatObj ()
{
  final Float aDefault = Float.valueOf (3.145667f);

  // Object
  assertNull (StringParser.parseFloatObj ((Object) null));
  assertEquals (aDefault, StringParser.parseFloatObj ((Object) null, aDefault));
  CommonsAssert.assertEquals (1, StringParser.parseFloatObj (BigDecimal.ONE, aDefault));
  assertEquals (aDefault, StringParser.parseFloatObj (new Wrapper <> ("any"), aDefault));

  // String
  assertNull (StringParser.parseFloatObj ("foo"));
  assertEquals (aDefault, StringParser.parseFloatObj ((String) null, aDefault));
  assertEquals (aDefault, StringParser.parseFloatObj ("", aDefault));
  CommonsAssert.assertEquals (1.2f, StringParser.parseFloatObj ("1.2", aDefault));
  CommonsAssert.assertEquals (-1.23456f, StringParser.parseFloatObj ("-1.23456", aDefault));
  assertEquals (aDefault, StringParser.parseFloatObj ("bla", aDefault));
}
 
Example 7
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a new cache map. This is the internal map that is used to store the
 * items.
 *
 * @return Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
@OverrideOnDemand
@CodingStyleguideUnaware
protected ICommonsMap <KEYSTORETYPE, Wrapper <VALUETYPE>> createCache ()
{
  return hasMaxSize () ? new SoftLinkedHashMap <> (m_nMaxSize) : new SoftHashMap <> ();
}
 
Example 8
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private Wrapper <VALUETYPE> _getCacheValue (final KEYTYPE aKey, final VALUETYPE aValue)
{
  if (aValue == null)
  {
    if (!m_bAllowNullValues)
      throw new IllegalStateException (_getCacheLogText () +
                                       "The created cache value of key '" +
                                       aKey +
                                       "' is null. null values are not allowed in this cache.");
    return new Wrapper <> ();
  }
  return new Wrapper <> (aValue);
}
 
Example 9
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@IsLocked (ELockType.READ)
protected final Wrapper <VALUETYPE> getFromCacheNoStats (@Nullable final KEYSTORETYPE aCacheKey)
{
  // null cache keys can never be in the cache
  if (aCacheKey == null)
    return null;
  return m_aRWLock.readLockedGet ( () -> getFromCacheNoStatsNotLocked (aCacheKey));
}
 
Example 10
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 5 votes vote down vote up
public VALUETYPE getFromCache (final KEYTYPE aKey)
{
  // Determine the internal key
  final KEYSTORETYPE aCacheKey = _getCacheKeyNonnull (aKey);

  Wrapper <VALUETYPE> aCacheValue = getFromCacheNoStats (aCacheKey);
  if (aCacheValue == null)
  {
    // No old value in the cache
    m_aRWLock.writeLock ().lock ();
    try
    {
      // Read again, in case the value was set between the two locking
      // sections
      // Note: do not increase statistics in this second try
      aCacheValue = getFromCacheNoStatsNotLocked (aCacheKey);
      if (aCacheValue == null)
      {
        // Call the value provide to create the value to cache
        final VALUETYPE aValue = m_aValueProvider.apply (aKey);
        aCacheValue = _getCacheValue (aKey, aValue);

        // Put the new value into the cache
        putInCacheNotLocked (aCacheKey, aCacheValue);
        m_aStatsCacheAccess.cacheMiss ();
      }
      else
        m_aStatsCacheAccess.cacheHit ();
    }
    finally
    {
      m_aRWLock.writeLock ().unlock ();
    }
  }
  else
    m_aStatsCacheAccess.cacheHit ();

  // the get() may resolve to a null value
  return aCacheValue.get ();
}
 
Example 11
Source Project: ph-commons   Source File: TypeConverterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the converter that can convert objects from aSrcClass to aDstClass. If
 * no exact match is found, the super-classes and interface of source and
 * destination class are searched for matching type converters. The first
 * match is returned.
 *
 * @param aSrcClass
 *        Source class. May not be <code>null</code>.
 * @param aDstClass
 *        Destination class. May not be <code>null</code>.
 * @return <code>null</code> if no such type converter exists, the converter
 *         object otherwise.
 */
@Nullable
ITypeConverter <?, ?> getFuzzyConverter (@Nullable final Class <?> aSrcClass, @Nullable final Class <?> aDstClass)
{
  if (aSrcClass == null || aDstClass == null)
    return null;

  return m_aRWLock.readLockedGet ( () -> {
    if (GlobalDebug.isDebugMode ())
    {
      // Perform a check, whether there is more than one potential converter
      // present!
      final ICommonsList <String> aAllConverters = new CommonsArrayList <> ();
      _iterateFuzzyConverters (aSrcClass, aDstClass, (aCurSrcClass, aCurDstClass, aConverter) -> {
        final boolean bExact = aSrcClass.equals (aCurSrcClass) && aDstClass.equals (aCurDstClass);
        aAllConverters.add ("[" + aCurSrcClass.getName () + "->" + aCurDstClass.getName () + "]");
        return bExact ? EContinue.BREAK : EContinue.CONTINUE;
      });
      if (aAllConverters.size () > 1)
        LOGGER.warn ("The fuzzy type converter resolver returned more than 1 match for the conversion from " +
                     aSrcClass +
                     " to " +
                     aDstClass +
                     ": " +
                     aAllConverters);
    }

    // Iterate and find the first matching type converter
    final Wrapper <ITypeConverter <?, ?>> ret = new Wrapper <> ();
    _iterateFuzzyConverters (aSrcClass, aDstClass, (aCurSrcClass, aCurDstClass, aConverter) -> {
      ret.set (aConverter);
      return EContinue.BREAK;
    });
    return ret.get ();
  });
}
 
Example 12
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 13
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable
@MustBeLocked (ELockType.READ)
protected final Wrapper <VALUETYPE> getFromCacheNoStatsNotLocked (@Nullable final KEYSTORETYPE aCacheKey)
{
  return m_aCache == null ? null : m_aCache.get (aCacheKey);
}
 
Example 14
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParseByte ()
{
  // Object
  assertEquals (1, StringParser.parseByte ((Object) "1", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "1.2", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "0", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "0000", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "-129", (byte) 0));
  assertEquals (-128, StringParser.parseByte ((Object) "-128", (byte) 0));
  assertEquals (-1, StringParser.parseByte ((Object) "-1", (byte) 0));
  assertEquals (44, StringParser.parseByte ((Object) "44", (byte) 0));
  assertEquals (127, StringParser.parseByte ((Object) "127", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "128", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "257", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "445566abcdef", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "abcdef445566", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) "", (byte) 0));
  assertEquals (0, StringParser.parseByte ((Object) null, (byte) 0));

  assertEquals (0, StringParser.parseByte (new Wrapper <> ("any"), (byte) 0));

  assertEquals (1, StringParser.parseByte (Byte.valueOf ((byte) 1), (byte) 0));
  assertEquals (1, StringParser.parseByte (Double.valueOf (1), (byte) 0));
  assertEquals (1, StringParser.parseByte (BigDecimal.ONE, (byte) 0));

  // String
  assertEquals (1, StringParser.parseByte ("1", (byte) 0));
  assertEquals (0, StringParser.parseByte ("1.2", (byte) 0));
  assertEquals (0, StringParser.parseByte ("0", (byte) 0));
  assertEquals (0, StringParser.parseByte ("0000", (byte) 0));
  assertEquals (0, StringParser.parseByte ("-129", (byte) 0));
  assertEquals (-128, StringParser.parseByte ("-128", (byte) 0));
  assertEquals (-1, StringParser.parseByte ("-1", (byte) 0));
  assertEquals (44, StringParser.parseByte ("44", (byte) 0));
  assertEquals (127, StringParser.parseByte ("127", (byte) 0));
  assertEquals (0, StringParser.parseByte ("128", (byte) 0));
  assertEquals (0, StringParser.parseByte ("257", (byte) 0));
  assertEquals (0, StringParser.parseByte ("445566", (byte) 0));
  assertEquals (0, StringParser.parseByte ("445566abcdef", (byte) 0));
  assertEquals (0, StringParser.parseByte ("abcdef445566", (byte) 0));
  assertEquals (0, StringParser.parseByte ("", (byte) 0));
  assertEquals (0, StringParser.parseByte (null, (byte) 0));

  final byte nDefault = 17;
  assertEquals (1, StringParser.parseByte ("1", nDefault));
  assertEquals (nDefault, StringParser.parseByte ("1.2", nDefault));
  assertEquals (0, StringParser.parseByte ("0", nDefault));
  assertEquals (0, StringParser.parseByte ("0000", nDefault));
  assertEquals (-1, StringParser.parseByte ("-1", nDefault));
  assertEquals (nDefault, StringParser.parseByte ("445566", nDefault));
  assertEquals (nDefault, StringParser.parseByte ("445566abcdef", nDefault));
  assertEquals (nDefault, StringParser.parseByte ("abcdef445566", nDefault));
  assertEquals (nDefault, StringParser.parseByte ("", nDefault));
  assertEquals (nDefault, StringParser.parseByte (null, nDefault));
}
 
Example 15
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParseByteObj ()
{
  final Byte b_1 = Byte.valueOf ((byte) -1);
  final Byte b0 = Byte.valueOf ((byte) 0);
  final Byte b1 = Byte.valueOf ((byte) 1);

  // Object
  assertEquals (b1, StringParser.parseByteObj ((Object) "1"));
  assertNull (StringParser.parseByteObj ((Object) "abc"));
  assertEquals (b1, StringParser.parseByteObj ((Object) "1", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) "1.2", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) "0", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) "0000", b0));
  assertEquals (b_1, StringParser.parseByteObj ((Object) "-1", b0));
  assertEquals (Byte.valueOf ((byte) 44), StringParser.parseByteObj ((Object) "44", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) "445566abcdef", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) "abcdef445566", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) "", b0));
  assertEquals (b0, StringParser.parseByteObj ((Object) null, b0));

  assertEquals (b0, StringParser.parseByteObj (new Wrapper <> ("any"), b0));

  assertEquals (b1, StringParser.parseByteObj (Byte.valueOf ((byte) 1), b0));
  assertEquals (b1, StringParser.parseByteObj (Double.valueOf (1), b0));
  assertEquals (b1, StringParser.parseByteObj (BigDecimal.ONE, b0));

  // String
  assertEquals (b1, StringParser.parseByteObj ("1"));
  assertNull (StringParser.parseByteObj ("abc"));
  assertEquals (b1, StringParser.parseByteObj ("1", b0));
  assertEquals (b0, StringParser.parseByteObj ("1.2", b0));
  assertEquals (b0, StringParser.parseByteObj ("0", b0));
  assertEquals (b0, StringParser.parseByteObj ("0000", b0));
  assertEquals (b_1, StringParser.parseByteObj ("-1", b0));
  assertEquals (Byte.valueOf ((byte) 44), StringParser.parseByteObj ("44", b0));
  assertEquals (b0, StringParser.parseByteObj ("445566abcdef", b0));
  assertEquals (b0, StringParser.parseByteObj ("abcdef445566", b0));
  assertEquals (b0, StringParser.parseByteObj ("", b0));
  assertEquals (b0, StringParser.parseByteObj (null, b0));

  final Byte aDefault = Byte.valueOf ((byte) 17);
  assertEquals (b1, StringParser.parseByteObj ("1", aDefault));
  assertEquals (aDefault, StringParser.parseByteObj ("1.2", aDefault));
  assertEquals (b0, StringParser.parseByteObj ("0", aDefault));
  assertEquals (b0, StringParser.parseByteObj ("0000", aDefault));
  assertEquals (b_1, StringParser.parseByteObj ("-1", aDefault));
  assertEquals (Byte.valueOf ((byte) 44), StringParser.parseByteObj ("44", aDefault));
  assertEquals (aDefault, StringParser.parseByteObj ("445566abcdef", aDefault));
  assertEquals (aDefault, StringParser.parseByteObj ("abcdef445566", aDefault));
  assertEquals (aDefault, StringParser.parseByteObj ("", aDefault));
  assertEquals (aDefault, StringParser.parseByteObj (null, aDefault));
}
 
Example 16
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParseInt ()
{
  // Object
  assertEquals (1, StringParser.parseInt ((Object) "1", 0));
  assertEquals (0, StringParser.parseInt ((Object) "1.2", 0));
  assertEquals (0, StringParser.parseInt ((Object) "0", 0));
  assertEquals (0, StringParser.parseInt ((Object) "0000", 0));
  assertEquals (-1, StringParser.parseInt ((Object) "-1", 0));
  assertEquals (445566, StringParser.parseInt ((Object) "445566", 0));
  assertEquals (0, StringParser.parseInt ((Object) "445566abcdef", 0));
  assertEquals (0, StringParser.parseInt ((Object) "abcdef445566", 0));
  assertEquals (0, StringParser.parseInt ((Object) "", 0));
  assertEquals (0, StringParser.parseInt ((Object) null, 0));

  assertEquals (0, StringParser.parseInt (new Wrapper <> ("any"), 0));

  assertEquals (1, StringParser.parseInt (Integer.valueOf (1), 0));
  assertEquals (1, StringParser.parseInt (Double.valueOf (1), 0));
  assertEquals (1, StringParser.parseInt (BigDecimal.ONE, 0));

  // String
  assertEquals (1, StringParser.parseInt ("1", 0));
  assertEquals (0, StringParser.parseInt ("1.2", 0));
  assertEquals (0, StringParser.parseInt ("0", 0));
  assertEquals (0, StringParser.parseInt ("0000", 0));
  assertEquals (-1, StringParser.parseInt ("-1", 0));
  assertEquals (445566, StringParser.parseInt ("445566", 0));
  assertEquals (0, StringParser.parseInt ("445566abcdef", 0));
  assertEquals (0, StringParser.parseInt ("abcdef445566", 0));
  assertEquals (0, StringParser.parseInt ("", 0));
  assertEquals (0, StringParser.parseInt (null, 0));

  final int nDefault = 17;
  assertEquals (1, StringParser.parseInt ("1", nDefault));
  assertEquals (nDefault, StringParser.parseInt ("1.2", nDefault));
  assertEquals (0, StringParser.parseInt ("0", nDefault));
  assertEquals (0, StringParser.parseInt ("0000", nDefault));
  assertEquals (-1, StringParser.parseInt ("-1", nDefault));
  assertEquals (445566, StringParser.parseInt ("445566", nDefault));
  assertEquals (nDefault, StringParser.parseInt ("445566abcdef", nDefault));
  assertEquals (nDefault, StringParser.parseInt ("abcdef445566", nDefault));
  assertEquals (nDefault, StringParser.parseInt ("", nDefault));
  assertEquals (nDefault, StringParser.parseInt (null, nDefault));
}
 
Example 17
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParseIntObj ()
{
  // Object
  assertEquals (I1, StringParser.parseIntObj ((Object) "1"));
  assertNull (StringParser.parseIntObj ((Object) "abc"));
  assertEquals (I1, StringParser.parseIntObj ((Object) "1", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) "1.2", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) "0", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) "0000", I0));
  assertEquals (I_1, StringParser.parseIntObj ((Object) "-1", I0));
  assertEquals (Integer.valueOf (445566), StringParser.parseIntObj ((Object) "445566", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) "445566abcdef", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) "abcdef445566", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) "", I0));
  assertEquals (I0, StringParser.parseIntObj ((Object) null, I0));

  assertEquals (I0, StringParser.parseIntObj (new Wrapper <> ("any"), I0));

  assertEquals (I1, StringParser.parseIntObj (Integer.valueOf (1), I0));
  assertEquals (I1, StringParser.parseIntObj (Double.valueOf (1), I0));
  assertEquals (I1, StringParser.parseIntObj (BigDecimal.ONE, I0));

  // String
  assertEquals (I1, StringParser.parseIntObj ("1"));
  assertNull (StringParser.parseIntObj ("abc"));
  assertEquals (I1, StringParser.parseIntObj ("1", I0));
  assertEquals (I0, StringParser.parseIntObj ("1.2", I0));
  assertEquals (I0, StringParser.parseIntObj ("0", I0));
  assertEquals (I0, StringParser.parseIntObj ("0000", I0));
  assertEquals (I_1, StringParser.parseIntObj ("-1", I0));
  assertEquals (Integer.valueOf (445566), StringParser.parseIntObj ("445566", I0));
  assertEquals (I0, StringParser.parseIntObj ("445566abcdef", I0));
  assertEquals (I0, StringParser.parseIntObj ("abcdef445566", I0));
  assertEquals (I0, StringParser.parseIntObj ("", I0));
  assertEquals (I0, StringParser.parseIntObj (null, I0));

  final Integer aDefault = Integer.valueOf (17);
  assertEquals (I1, StringParser.parseIntObj ("1", aDefault));
  assertEquals (aDefault, StringParser.parseIntObj ("1.2", aDefault));
  assertEquals (I0, StringParser.parseIntObj ("0", aDefault));
  assertEquals (I0, StringParser.parseIntObj ("0000", aDefault));
  assertEquals (I_1, StringParser.parseIntObj ("-1", aDefault));
  assertEquals (Integer.valueOf (445566), StringParser.parseIntObj ("445566", aDefault));
  assertEquals (aDefault, StringParser.parseIntObj ("445566abcdef", aDefault));
  assertEquals (aDefault, StringParser.parseIntObj ("abcdef445566", aDefault));
  assertEquals (aDefault, StringParser.parseIntObj ("", aDefault));
  assertEquals (aDefault, StringParser.parseIntObj (null, aDefault));
}
 
Example 18
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParseLong ()
{
  // Object
  assertEquals (1L, StringParser.parseLong ((Object) "1", 0));
  assertEquals (0L, StringParser.parseLong ((Object) "1.2", 0));
  assertEquals (0L, StringParser.parseLong ((Object) "0", 0));
  assertEquals (0L, StringParser.parseLong ((Object) "0000", 0));
  assertEquals (-1L, StringParser.parseLong ((Object) "-1", 0));
  assertEquals (445566L, StringParser.parseLong ((Object) "445566", 0));
  assertEquals (0L, StringParser.parseLong ((Object) "445566abcdef", 0));
  assertEquals (0L, StringParser.parseLong ((Object) "abcdef445566", 0));
  assertEquals (0L, StringParser.parseLong ((Object) "", 0));
  assertEquals (0L, StringParser.parseLong ((Object) null, 0));

  assertEquals (0L, StringParser.parseLong (new Wrapper <> ("any"), 0));

  assertEquals (1L, StringParser.parseLong (Integer.valueOf (1), 0));
  assertEquals (1L, StringParser.parseLong (Double.valueOf (1), 0));
  assertEquals (1L, StringParser.parseLong (BigDecimal.ONE, 0));

  // String
  assertEquals (1L, StringParser.parseLong ("1", 0));
  assertEquals (0L, StringParser.parseLong ("1.2", 0));
  assertEquals (0L, StringParser.parseLong ("0", 0));
  assertEquals (0L, StringParser.parseLong ("0000", 0));
  assertEquals (-1L, StringParser.parseLong ("-1", 0));
  assertEquals (445566L, StringParser.parseLong ("445566", 0));
  assertEquals (445566445566L, StringParser.parseLong ("445566445566", 0));
  assertEquals (445566445566445566L, StringParser.parseLong ("445566445566445566", 0));
  assertEquals (0L, StringParser.parseLong ("445566abcdef", 0));
  assertEquals (0L, StringParser.parseLong ("abcdef445566", 0));
  assertEquals (0L, StringParser.parseLong ("", 0));
  assertEquals (0L, StringParser.parseLong (null, 0));

  final long nDefault = 171819171819171819L;
  assertEquals (1L, StringParser.parseLong ("1", nDefault));
  assertEquals (nDefault, StringParser.parseLong ("1.2", nDefault));
  assertEquals (0L, StringParser.parseLong ("0", nDefault));
  assertEquals (0L, StringParser.parseLong ("0000", nDefault));
  assertEquals (-1L, StringParser.parseLong ("-1", nDefault));
  assertEquals (445566L, StringParser.parseLong ("445566", nDefault));
  assertEquals (445566445566L, StringParser.parseLong ("445566445566", nDefault));
  assertEquals (445566445566445566L, StringParser.parseLong ("445566445566445566", nDefault));
  assertEquals (nDefault, StringParser.parseLong ("445566abcdef", nDefault));
  assertEquals (nDefault, StringParser.parseLong ("abcdef445566", nDefault));
  assertEquals (nDefault, StringParser.parseLong ("", nDefault));
  assertEquals (nDefault, StringParser.parseLong (null, nDefault));
}
 
Example 19
Source Project: ph-commons   Source File: StringParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParseLongObj ()
{
  // Object
  assertEquals (L1, StringParser.parseLongObj ((Object) "1"));
  assertNull (StringParser.parseLongObj ((Object) "abc"));
  assertEquals (L1, StringParser.parseLongObj ((Object) "1", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) "1.2", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) "0", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) "0000", L0));
  assertEquals (L_1, StringParser.parseLongObj ((Object) "-1", L0));
  assertEquals (Long.valueOf (445566), StringParser.parseLongObj ((Object) "445566", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) "445566abcdef", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) "abcdef445566", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) "", L0));
  assertEquals (L0, StringParser.parseLongObj ((Object) null, L0));

  assertEquals (L0, StringParser.parseLongObj (new Wrapper <> ("any"), L0));

  assertEquals (L1, StringParser.parseLongObj (Integer.valueOf (1), L0));
  assertEquals (L1, StringParser.parseLongObj (Double.valueOf (1), L0));
  assertEquals (L1, StringParser.parseLongObj (BigDecimal.ONE, L0));

  // String
  assertEquals (L1, StringParser.parseLongObj ("1"));
  assertNull (StringParser.parseLongObj ("abc"));
  assertEquals (L1, StringParser.parseLongObj ("1", L0));
  assertEquals (L0, StringParser.parseLongObj ("1.2", L0));
  assertEquals (L0, StringParser.parseLongObj ("0", L0));
  assertEquals (L0, StringParser.parseLongObj ("0000", L0));
  assertEquals (L_1, StringParser.parseLongObj ("-1", L0));
  assertEquals (Long.valueOf (445566), StringParser.parseLongObj ("445566", L0));
  assertEquals (L0, StringParser.parseLongObj ("445566abcdef", L0));
  assertEquals (L0, StringParser.parseLongObj ("abcdef445566", L0));
  assertEquals (L0, StringParser.parseLongObj ("", L0));
  assertEquals (L0, StringParser.parseLongObj (null, L0));

  final Long aDefault = Long.valueOf (-173267823468L);
  assertEquals (L1, StringParser.parseLongObj ("1", aDefault));
  assertEquals (aDefault, StringParser.parseLongObj ("1.2", aDefault));
  assertEquals (L0, StringParser.parseLongObj ("0", aDefault));
  assertEquals (L0, StringParser.parseLongObj ("0000", aDefault));
  assertEquals (L_1, StringParser.parseLongObj ("-1", aDefault));
  assertEquals (Long.valueOf (445566), StringParser.parseLongObj ("445566", aDefault));
  assertEquals (aDefault, StringParser.parseLongObj ("445566abcdef", aDefault));
  assertEquals (aDefault, StringParser.parseLongObj ("abcdef445566", aDefault));
  assertEquals (aDefault, StringParser.parseLongObj ("", aDefault));
  assertEquals (aDefault, StringParser.parseLongObj (null, aDefault));
}
 
Example 20
Source Project: ph-commons   Source File: MappedCache.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Put a new value into the cache. Use this in derived classes to e.g. prefill
 * the cache with existing values.
 *
 * @param aKey
 *        The cache key. May be <code>null</code> depending on the cache key
 *        provider.
 * @param aValue
 *        The cache value. May be <code>null</code> depending on the settings.
 */
@IsLocked (ELockType.WRITE)
protected final void putInCache (final KEYTYPE aKey, final VALUETYPE aValue)
{
  final KEYSTORETYPE aCacheKey = _getCacheKeyNonnull (aKey);
  final Wrapper <VALUETYPE> aCacheValue = _getCacheValue (aKey, aValue);
  m_aRWLock.writeLocked ( () -> putInCacheNotLocked (aCacheKey, aCacheValue));
}