Java Code Examples for com.helger.commons.collection.ArrayHelper

The following examples show how to use com.helger.commons.collection.ArrayHelper. 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: SingleElementList.java    License: 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 Project: ph-commons   Source File: EqualsHelperTest.java    License: 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 3
Source Project: ph-commons   Source File: JsonEscapeHelper.java    License: 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 4
Source Project: ph-commons   Source File: StringHelper.java    License: 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 5
Source Project: ph-css   Source File: CSSReader30SpecialFuncTest.java    License: 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 6
Source Project: ph-commons   Source File: ArrayIteratorTest.java    License: 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 7
Source Project: ph-commons   Source File: TextHelper.java    License: 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 8
Source Project: ph-commons   Source File: TextHelper.java    License: 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 9
Source Project: ph-commons   Source File: JavaPrinterTrayFinderFuncTest.java    License: 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 10
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: 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 11
Source Project: ph-commons   Source File: SingleElementList.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public Object [] toArray ()
{
  if (m_bHasElement)
  {
    final Object [] aObjects = new Object [1];
    aObjects[0] = m_aElement;
    return aObjects;
  }
  return ArrayHelper.EMPTY_OBJECT_ARRAY;
}
 
Example 12
Source Project: ph-commons   Source File: ByteBufferOutputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get everything as a big byte array, without altering the ByteBuffer. This
 * works only if the contained ByteBuffer has a backing array.
 *
 * @return The content of the buffer as a byte array. Never <code>null</code>.
 */
@Nonnull
@ReturnsMutableCopy
public byte [] getAsByteArray ()
{
  final byte [] aArray = m_aBuffer.array ();
  final int nOfs = m_aBuffer.arrayOffset ();
  final int nLength = m_aBuffer.position ();

  return ArrayHelper.getCopy (aArray, nOfs, nLength);
}
 
Example 13
Source Project: ph-commons   Source File: QueueHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static <ELEMENTTYPE> PriorityQueue <ELEMENTTYPE> newQueue (@Nullable final ELEMENTTYPE... aValues)
{
  // Don't user Arrays.asQueue since aIter returns an unmodifiable list!
  if (ArrayHelper.isEmpty (aValues))
    return newQueue (0);

  final PriorityQueue <ELEMENTTYPE> ret = newQueue (aValues.length);
  Collections.addAll (ret, aValues);
  return ret;
}
 
Example 14
Source Project: ph-commons   Source File: VectorHelper.java    License: 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 15
Source Project: ph-commons   Source File: VectorHelper.java    License: 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 16
Source Project: ph-commons   Source File: XMLMaskHelper.java    License: 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 17
Source Project: ph-commons   Source File: ThreadDescriptorList.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ThreadDescriptorList createWithAllThreads ()
{
  // add dump of all threads
  final StopWatch aSW = StopWatch.createdStarted ();
  final ThreadDescriptorList ret = new ThreadDescriptorList ();
  try
  {
    // Get all stack traces, sorted by thread ID
    for (final Map.Entry <Thread, StackTraceElement []> aEntry : CollectionHelper.getSortedByKey (Thread.getAllStackTraces (),
                                                                                                  Comparator.comparing (Thread::getId))
                                                                                 .entrySet ())
    {
      final StackTraceElement [] aStackTrace = aEntry.getValue ();
      final String sStackTrace = ArrayHelper.isEmpty (aStackTrace) ? "No stack trace available!\n"
                                                                   : StackTraceHelper.getStackAsString (aStackTrace,
                                                                                                        false);
      ret.addDescriptor (new ThreadDescriptor (aEntry.getKey (), sStackTrace));
    }
  }
  catch (final Exception ex)
  {
    LOGGER.error ("Error collecting all thread descriptors", ex);
    ret.setError ("Error collecting all thread descriptors: " + _getAsString (ex));
  }
  finally
  {
    final long nMillis = aSW.stopAndGetMillis ();
    if (nMillis > 1000)
      if (LOGGER.isWarnEnabled ())
        LOGGER.warn ("Took " + nMillis + " ms to get all thread descriptors!");
  }
  return ret;
}
 
Example 18
Source Project: ph-commons   Source File: FilenameHelper.java    License: 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 19
Source Project: ph-commons   Source File: EqualsHelperTest.java    License: 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 20
Source Project: ph-commons   Source File: StringHelper.java    License: 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 21
Source Project: ph-commons   Source File: ICharArrayCodecTest.java    License: 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 22
@Override
public String toString ()
{
  return new ToStringGenerator (this).append ("Buf#", ArrayHelper.getSize (m_aBuf))
                                     .append ("Count", m_nCount)
                                     .getToString ();
}
 
Example 23
Source Project: ph-commons   Source File: TypeConverterTest.java    License: 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 24
Source Project: ph-commons   Source File: StringHelper.java    License: 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 25
Source Project: ph-commons   Source File: StringHelper.java    License: 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 26
Source Project: ph-commons   Source File: IErrorList.java    License: 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 27
Source Project: ph-commons   Source File: IErrorList.java    License: 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 28
Source Project: ph-commons   Source File: DynamicHasErrorTextWithArgs.java    License: 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);
}
 
Example 29
Source Project: ph-commons   Source File: ThreadDeadlockInfo.java    License: 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 30
Source Project: ph-commons   Source File: EnumHelper.java    License: 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);
}