com.helger.commons.collection.ArrayHelper Java Examples

The following examples show how to use com.helger.commons.collection.ArrayHelper. 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: SingleElementList.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public <ARRAYELEMENTTYPE> ARRAYELEMENTTYPE [] toArray (@Nonnull final ARRAYELEMENTTYPE [] aDest)
{
  ValueEnforcer.notNull (aDest, "Dest");

  if (!m_bHasElement)
    return aDest;
  if (m_aElement != null && !aDest.getClass ().getComponentType ().isAssignableFrom (m_aElement.getClass ()))
    throw new ArrayStoreException ("The array class " +
                                   aDest.getClass () +
                                   " cannot store the item of class " +
                                   m_aElement.getClass ());
  final ARRAYELEMENTTYPE [] ret = aDest.length < 1 ? ArrayHelper.newArraySameType (aDest, 1) : aDest;
  ret[0] = GenericReflection.uncheckedCast (m_aElement);
  if (ret.length > 1)
    ret[1] = null;
  return ret;
}
 
Example #2
Source File: JavaPrinterTrayFinderFuncTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testListPrinterTrays ()
{
  final PrintService [] aAllServices = PrintServiceLookup.lookupPrintServices (null, null);
  for (final PrintService aService : aAllServices)
  {
    LOGGER.info (aService.toString ());
    final Object aAttrs = aService.getSupportedAttributeValues (Media.class,
                                                                DocFlavor.SERVICE_FORMATTED.PAGEABLE,
                                                                null);
    if (ArrayHelper.isArray (aAttrs))
    {
      for (final Media aElement : (Media []) aAttrs)
        if (aElement instanceof MediaTray)
          LOGGER.info ("  " + aElement);
    }
  }
}
 
Example #3
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testIterable ()
{
  final Iterable <String> aCont = new MockIterable ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aCont, aCont));
  assertTrue (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b", "c")));
  assertTrue (EqualsHelper.equalsCollection (new MockIterable (), new MockIterable ()));

  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ()));
  assertFalse (EqualsHelper.equalsCollection (new MockIterable (), aCont));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("A", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "B", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b", "C")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new MockIterable ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aCont, new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont, ArrayHelper.newArray ("a", "b", "c")));
}
 
Example #4
Source File: JsonEscapeHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static String jsonUnescape (@Nonnull final char [] aInput)
{
  ValueEnforcer.notNull (aInput, "Input");

  if (!ArrayHelper.contains (aInput, MASK_CHAR))
  {
    // Nothing to unescape
    return new String (aInput);
  }

  // Perform unescape
  final StringBuilder aSB = new StringBuilder (aInput.length);
  jsonUnescapeToStringBuilder (aInput, aSB);
  return aSB.toString ();
}
 
Example #5
Source File: TextHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getFormattedText (@Nonnull final Locale aDisplayLocale,
                                       @Nullable final String sText,
                                       @Nullable final Object... aArgs)
{
  ValueEnforcer.notNull (aDisplayLocale, "DisplayLocale");

  if (sText == null)
  {
    // Avoid NPE in MessageFormat
    return null;
  }

  if (ArrayHelper.isEmpty (aArgs))
  {
    // Return text unchanged
    return sText;
  }

  final MessageFormat aMF = new MessageFormat (sText, aDisplayLocale);
  return aMF.format (aArgs);
}
 
Example #6
Source File: TextHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String getFormattedText (@Nullable final String sText, @Nullable final Object... aArgs)
{
  if (sText == null)
  {
    // Avoid NPE in MessageFormat
    return null;
  }

  if (ArrayHelper.isEmpty (aArgs))
  {
    // Return text unchanged
    return sText;
  }

  final MessageFormat aMF = new MessageFormat (sText, Locale.getDefault (Locale.Category.FORMAT));
  return aMF.format (aArgs);
}
 
Example #7
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testArray ()
{
  final String [] aArray = ArrayHelper.newArray ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aArray, aArray));
  assertTrue (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "c")));
  assertTrue (EqualsHelper.equalsCollection (new String [0], new String [] {}));

  assertFalse (EqualsHelper.equalsCollection (aArray, new String [0]));
  assertFalse (EqualsHelper.equalsCollection (new String [0], aArray));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("A", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "B", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "C")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "c", "d")));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsHashSet <> ("a", "b", "c")));
}
 
Example #8
Source File: StringHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Optimized remove method that removes a set of characters from an input
 * string!
 *
 * @param sInputString
 *        The input string.
 * @param aRemoveChars
 *        The characters to remove. May not be <code>null</code>.
 * @return The version of the string without the passed characters or an empty
 *         String if the input string was <code>null</code>.
 */
@Nonnull
public static String removeMultiple (@Nullable final String sInputString, @Nonnull final char [] aRemoveChars)
{
  ValueEnforcer.notNull (aRemoveChars, "RemoveChars");

  // Any input text?
  if (hasNoText (sInputString))
    return "";

  // Anything to remove?
  if (aRemoveChars.length == 0)
    return sInputString;

  final StringBuilder aSB = new StringBuilder (sInputString.length ());
  iterateChars (sInputString, cInput -> {
    if (!ArrayHelper.contains (aRemoveChars, cInput))
      aSB.append (cInput);
  });
  return aSB.toString ();
}
 
Example #9
Source File: ArrayIteratorTest.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testStdMethods ()
{
  final ArrayIterator <String> ae = new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                "Welt",
                                                                                "from",
                                                                                "Copenhagen"));
  CommonsTestHelper.testDefaultImplementationWithEqualContentObject (ae,
                                                                     new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                                                 "Welt",
                                                                                                                 "from",
                                                                                                                 "Copenhagen")));
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (ae,
                                                                         new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                                                     "Welt",
                                                                                                                     "from")));
  ae.next ();
  CommonsTestHelper.testDefaultImplementationWithDifferentContentObject (ae,
                                                                         new ArrayIterator <> (ArrayHelper.newArray ("Hallo",
                                                                                                                     "Welt",
                                                                                                                     "from",
                                                                                                                     "Copenhagen")));
}
 
Example #10
Source File: CSSReader30SpecialFuncTest.java    From ph-css with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWithBOM ()
{
  final String sCSSBase = "/* comment */.class{color:red}.class{color:blue}";
  for (final EUnicodeBOM eBOM : EUnicodeBOM.values ())
  {
    final Charset aDeterminedCharset = eBOM.getCharset ();
    if (aDeterminedCharset != null)
    {
      final CascadingStyleSheet aCSS = CSSReader.readFromStream (new ByteArrayInputStreamProvider (ArrayHelper.getConcatenated (eBOM.getAllBytes (),
                                                                                                                                sCSSBase.getBytes (aDeterminedCharset))),
                                                                 aDeterminedCharset,
                                                                 ECSSVersion.CSS30,
                                                                 new DoNothingCSSParseErrorHandler ());
      assertNotNull ("Failed to read with BOM " + eBOM, aCSS);
      assertEquals (".class{color:red}.class{color:blue}",
                    new CSSWriter (ECSSVersion.CSS30, true).getCSSAsString (aCSS));
    }
  }
}
 
Example #11
Source File: VectorHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public static <SRCTYPE, DSTTYPE> CommonsVector <DSTTYPE> newVectorMapped (@Nullable final SRCTYPE [] aArray,
                                                                          @Nonnull final Function <? super SRCTYPE, DSTTYPE> aMapper)
{
  if (ArrayHelper.isEmpty (aArray))
    return newVector (0);
  final CommonsVector <DSTTYPE> ret = newVector (aArray.length);
  ret.addAllMapped (aArray, aMapper);
  return ret;
}
 
Example #12
Source File: XMLMaskHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
private static char [] [] _createEmptyReplacement (@Nonnull final char [] aSrcMap)
{
  final char [] [] ret = new char [aSrcMap.length] [];
  for (int i = 0; i < aSrcMap.length; ++i)
    ret[i] = ArrayHelper.EMPTY_CHAR_ARRAY;
  return ret;
}
 
Example #13
Source File: IHasByteArray.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * @return A copy of all bytes contained. Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
default byte [] getAllBytes ()
{
  return ArrayHelper.getCopy (bytes ());
}
 
Example #14
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 #15
Source File: FilenameHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the passed filename into a valid filename by performing the
 * following actions:
 * <ol>
 * <li>Remove everything after a potential \0 character</li>
 * <li>Remove all characters that are invalid at the end of a file name</li>
 * <li>Replace all characters that are invalid inside a filename with a
 * underscore</li>
 * <li>If the filename is invalid on Windows platforms it is prefixed with an
 * underscore.</li>
 * </ol>
 * Note: this method does not handle Windows full path like
 * "c:\autoexec.bat"<br>
 *
 * @param sFilename
 *        The filename to be made value. May be <code>null</code>.
 * @return <code>null</code> if the input filename was <code>null</code> or if
 *         it consisted only of characters invalid for a filename; the
 *         potentially modified filename otherwise but <b>never</b> an empy
 *         string.
 * @see #getSecureFilename(String)
 */
@Nullable
@Nonempty
public static String getAsSecureValidFilename (@Nullable final String sFilename)
{
  // First secure it, by cutting everything behind the '\0'
  String ret = getSecureFilename (sFilename);

  // empty not allowed
  if (StringHelper.hasText (ret))
  {
    // Remove all trailing invalid suffixes
    while (ret.length () > 0 && StringHelper.endsWithAny (ret, ILLEGAL_SUFFIXES))
      ret = ret.substring (0, ret.length () - 1);

    // Replace all characters that are illegal inside a filename
    for (final char cIllegal : ILLEGAL_CHARACTERS)
      ret = StringHelper.replaceAll (ret, cIllegal, ILLEGAL_FILENAME_CHAR_REPLACEMENT);

    // Check if a file matches an illegal prefix
    final String sTempRet = ret;
    if (ArrayHelper.containsAny (ILLEGAL_PREFIXES, sTempRet::equalsIgnoreCase))
      ret = ILLEGAL_FILENAME_CHAR_REPLACEMENT + ret;

    // check if filename is prefixed with an illegal prefix
    // Note: we can use the default locale, since all fixed names are pure
    // ANSI names
    final String sUCFilename = ret.toUpperCase (SystemHelper.getSystemLocale ());
    if (ArrayHelper.containsAny (ILLEGAL_PREFIXES, x -> sUCFilename.startsWith (x + ".")))
      ret = ILLEGAL_FILENAME_CHAR_REPLACEMENT + ret;
  }

  // Avoid returning an empty string as valid file name
  return StringHelper.hasNoText (ret) ? null : ret;
}
 
Example #16
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayComplex ()
{
  final ICommonsList <String> [] aArray = ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"), new CommonsArrayList <> ("c", "d"));
  assertTrue (EqualsHelper.equalsCollection (aArray, aArray));
  assertTrue (EqualsHelper.equalsCollection (aArray,
                                             ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"),
                                                                   new CommonsArrayList <> ("c", "d"))));
  assertTrue (EqualsHelper.equalsCollection (new ICommonsList <?> [0], new ICommonsList <?> [] {}));

  assertFalse (EqualsHelper.equalsCollection (aArray, new ICommonsList <?> [0]));
  assertFalse (EqualsHelper.equalsCollection (new ICommonsList <?> [0], aArray));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("A", "b"),
                                                                    new CommonsArrayList <> ("c", "d"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"),
                                                                    new CommonsArrayList <> ("c", "D"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"),
                                                                    new CommonsArrayList <> ("c", "d"),
                                                                    new CommonsArrayList <> ("e", "f"))));
  assertFalse (EqualsHelper.equalsCollection (aArray,
                                              ArrayHelper.newArray (new CommonsArrayList <> ("a", "b"), (ICommonsList <String>) null)));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsArrayList <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aArray, ArrayHelper.newArray ("a", null, "c")));
}
 
Example #17
Source File: EqualsHelperTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testIterator ()
{
  final ICommonsList <String> aCont = new CommonsArrayList <> ("a", "b", "c");
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), aCont.iterator ()));
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), CollectionHelper.makeUnmodifiable (aCont).iterator ()));
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), Collections.synchronizedList (aCont).iterator ()));
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <> (aCont).iterator ()));
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsLinkedList <> (aCont).iterator ()));
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsVector <> (aCont).iterator ()));
  assertTrue (EqualsHelper.equalsCollection (aCont.iterator (), new NonBlockingStack <> (aCont).iterator ()));
  assertTrue (EqualsHelper.equalsCollection (new CommonsArrayList <String> ().iterator (),
                                             new CommonsLinkedList <String> ().iterator ()));
  assertTrue (EqualsHelper.equalsCollection (new NonBlockingStack <String> ().iterator (), new CommonsVector <String> ().iterator ()));
  assertTrue (EqualsHelper.equalsCollection (new NonBlockingStack <String> ().iterator (), new Stack <String> ().iterator ()));

  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsLinkedList <String> ().iterator ()));
  assertFalse (EqualsHelper.equalsCollection (new CommonsLinkedList <String> ().iterator (), aCont.iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <String> ().iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <> ("a", "b").iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <> ("A", "b", "c").iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <> ("a", "B", "c").iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <> ("a", "b", "C").iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsArrayList <> ("a", "b", "c", "d").iterator ()));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), new CommonsHashSet <> ("a", "b", "c")));
  assertFalse (EqualsHelper.equalsCollection (aCont.iterator (), ArrayHelper.newArray ("a", "b", "c")));
}
 
Example #18
Source File: LZWCodec.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (null).append ("Index", m_nTableIndex)
                                     .append ("Children#", ArrayHelper.getSize (m_aChildren))
                                     .getToString ();
}
 
Example #19
Source File: VectorHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static <ELEMENTTYPE> CommonsVector <ELEMENTTYPE> newVector (@Nullable final ELEMENTTYPE... aValues)
{
  // Don't user Arrays.asVector since aIter returns an unmodifiable list!
  if (ArrayHelper.isEmpty (aValues))
    return newVector (0);

  return new CommonsVector <> (aValues);
}
 
Example #20
Source File: EnumHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
public static <ENUMTYPE extends Enum <ENUMTYPE>> ENUMTYPE findFirst (@Nonnull final Class <ENUMTYPE> aClass,
                                                                     @Nullable final Predicate <? super ENUMTYPE> aFilter,
                                                                     @Nullable final ENUMTYPE eDefault)
{
  return ArrayHelper.findFirst (aClass.getEnumConstants (), aFilter, eDefault);
}
 
Example #21
Source File: ThreadDeadlockInfo.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * @return The stack trace at the time the dead lock was found. May be
 *         <code>null</code> for certain system threads. Use
 *         <code>getThread ().getStackTrace ()</code> to retrieve the current
 *         stack trace.
 */
@Nullable
@ReturnsMutableCopy
public StackTraceElement [] getAllStackTraceElements ()
{
  return ArrayHelper.getCopy (m_aStackTrace);
}
 
Example #22
Source File: StringHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public static boolean endsWithAny (@Nullable final CharSequence aCS, @Nullable final char [] aChars)
{
  if (hasText (aCS) && aChars != null)
    if (ArrayHelper.contains (aChars, getLastChar (aCS)))
      return true;
  return false;
}
 
Example #23
Source File: ICharArrayCodecTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private void _testCodec (@Nonnull final ICharArrayCodec c)
{
  _testCodec (c, ArrayHelper.EMPTY_CHAR_ARRAY);
  _testCodec (c, "Hallo JÜnit".toCharArray ());

  // Get random bytes
  final char [] aRandomBytes = new char [256];
  final ThreadLocalRandom aRandom = ThreadLocalRandom.current ();
  for (int i = 0; i < 256; ++i)
    aRandomBytes[i] = (char) aRandom.nextInt (Character.MIN_CODE_POINT, Character.MAX_CODE_POINT);
  _testCodec (c, aRandomBytes);

  for (int i = 0; i < 256; ++i)
  {
    final char [] aBuf = new char [i];

    // build ascending identity field
    for (int j = 0; j < i; ++j)
      aBuf[j] = (char) j;
    _testCodec (c, aBuf);

    // build constant field with all the same byte
    for (int j = 0; j < i; ++j)
      aBuf[j] = (char) i;
    _testCodec (c, aBuf);
  }
}
 
Example #24
Source File: NonBlockingByteArrayOutputStream.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Override
public String toString ()
{
  return new ToStringGenerator (this).append ("Buf#", ArrayHelper.getSize (m_aBuf))
                                     .append ("Count", m_nCount)
                                     .getToString ();
}
 
Example #25
Source File: TypeConverterTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromNonNumericString ()
{
  // Test non numeric string
  for (final Class <?> aDstClass : ArrayHelper.getConcatenated (CONVERTIBLE_CLASSES, new Class <?> [] { Enum.class }))
    try
    {
      TypeConverter.convert ("not a number", aDstClass);
    }
    catch (final TypeConverterException ex)
    {
      assertEquals (EReason.CONVERSION_FAILED, ex.getReason ());
    }
}
 
Example #26
Source File: StringHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Optimized replace method that replaces a set of characters with another
 * character. This method was created for efficient unsafe character
 * replacements!
 *
 * @param sInputString
 *        The input string.
 * @param aSearchChars
 *        The characters to replace.
 * @param cReplacementChar
 *        The new char to be used instead of the search chars.
 * @param aTarget
 *        The target StringBuilder to write the result to. May not be
 *        <code>null</code>.
 */
public static void replaceMultipleTo (@Nullable final String sInputString,
                                      @Nonnull final char [] aSearchChars,
                                      final char cReplacementChar,
                                      @Nonnull final StringBuilder aTarget)
{
  ValueEnforcer.notNull (aSearchChars, "SearchChars");
  ValueEnforcer.notNull (aTarget, "Target");

  // Any input text?
  if (hasText (sInputString))
  {
    // Any search chars?
    if (aSearchChars.length == 0)
    {
      aTarget.append (sInputString);
    }
    else
    {
      // Perform the replacement
      for (final char c : sInputString.toCharArray ())
      {
        if (ArrayHelper.contains (aSearchChars, c))
          aTarget.append (cReplacementChar);
        else
          aTarget.append (c);
      }
    }
  }
}
 
Example #27
Source File: StringHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Optimized replace method that replaces a set of characters with another
 * character. This method was created for efficient unsafe character
 * replacements!
 *
 * @param sInputString
 *        The input string.
 * @param aSearchChars
 *        The characters to replace.
 * @param cReplacementChar
 *        The new char to be used instead of the search chars.
 * @param aTarget
 *        The target writer to write the result to. May not be
 *        <code>null</code>.
 * @throws IOException
 *         in case writing to the Writer fails
 * @since 8.6.3
 */
public static void replaceMultipleTo (@Nullable final String sInputString,
                                      @Nonnull final char [] aSearchChars,
                                      final char cReplacementChar,
                                      @Nonnull final Writer aTarget) throws IOException
{
  ValueEnforcer.notNull (aSearchChars, "SearchChars");
  ValueEnforcer.notNull (aTarget, "Target");

  // Any input text?
  if (hasText (sInputString))
  {
    // Any search chars?
    if (aSearchChars.length == 0)
    {
      aTarget.write (sInputString);
    }
    else
    {
      // Perform the replacement
      for (final char c : sInputString.toCharArray ())
      {
        if (ArrayHelper.contains (aSearchChars, c))
          aTarget.write (cReplacementChar);
        else
          aTarget.write (c);
      }
    }
  }
}
 
Example #28
Source File: IErrorList.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get a sub-list with all entries for the specified field names
 *
 * @param aSearchFieldNames
 *        The field names to search.
 * @return Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
default IErrorList getListOfFields (@Nullable final String... aSearchFieldNames)
{
  if (ArrayHelper.isEmpty (aSearchFieldNames))
  {
    // Empty sublist
    return getSubList (x -> false);
  }
  return getSubList (x -> x.hasErrorFieldName () && ArrayHelper.contains (aSearchFieldNames, x.getErrorFieldName ()));
}
 
Example #29
Source File: IErrorList.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get a sub-list with all entries that have field names starting with one of
 * the supplied names.
 *
 * @param aSearchFieldNames
 *        The field names to search.
 * @return Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
default IErrorList getListOfFieldsStartingWith (@Nullable final String... aSearchFieldNames)
{
  if (ArrayHelper.isEmpty (aSearchFieldNames))
  {
    // Empty sublist
    return getSubList (x -> false);
  }
  return getSubList (x -> x.hasErrorFieldName () &&
                          ArrayHelper.containsAny (aSearchFieldNames, y -> x.getErrorFieldName ().startsWith (y)));
}
 
Example #30
Source File: DynamicHasErrorTextWithArgs.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get all arguments from the constructor.
 *
 * @return a copy of all arguments. Neither <code>null</code> nor empty-
 */
@Nonnull
@Nonempty
@ReturnsMutableCopy
public Object [] getAllArgs ()
{
  return ArrayHelper.getCopy (m_aArgs);
}