Java Code Examples for com.helger.commons.ValueEnforcer#isGE0()

The following examples show how to use com.helger.commons.ValueEnforcer#isGE0() . 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: LevenshteinDistance.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
public static int getDistance (@Nullable final char [] aStr1,
                               @Nullable final char [] aStr2,
                               @Nonnegative final int nCostInsert,
                               @Nonnegative final int nCostDelete,
                               @Nonnegative final int nCostSubstitution)
{
  ValueEnforcer.isGE0 (nCostInsert, "InsertionCost");
  ValueEnforcer.isGE0 (nCostDelete, "DeletionCost");
  ValueEnforcer.isGE0 (nCostSubstitution, "SubstitutionCost");

  final int nLen1 = aStr1 == null ? 0 : aStr1.length;
  final int nLen2 = aStr2 == null ? 0 : aStr2.length;

  if (nLen1 == 0)
    return nLen2 * nCostInsert;
  if (nLen2 == 0)
    return nLen1 * nCostInsert;

  // Fallback to more efficient variant?
  if (nCostInsert == 1 && nCostDelete == 1 && nCostSubstitution == 1)
    return _getDistance111 (aStr1, nLen1, aStr2, nLen2);

  return _getDistance (aStr1, nLen1, aStr2, nLen2, nCostInsert, nCostDelete, nCostSubstitution);
}
 
Example 2
Source File: StringParser.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the given {@link String} as {@link BigDecimal}.
 *
 * @param sStr
 *        The String to parse. May be <code>null</code>.
 * @param nScale
 *        The scaling (decimal places) to be used for the result. Must be &ge;
 *        0!
 * @param eRoundingMode
 *        The rounding mode to be used to achieve the scale. May not be
 *        <code>null</code>.
 * @param aDefault
 *        The default value to be returned if the passed string could not be
 *        converted to a valid value. May be <code>null</code>.
 * @return <code>aDefault</code> if the string does not represent a valid
 *         value.
 */
@Nullable
public static BigDecimal parseBigDecimal (@Nullable final String sStr,
                                          @Nonnegative final int nScale,
                                          @Nonnull final RoundingMode eRoundingMode,
                                          @Nullable final BigDecimal aDefault)
{
  ValueEnforcer.isGE0 (nScale, "Scale");
  ValueEnforcer.notNull (eRoundingMode, "RoundingMode");

  if (sStr != null && sStr.length () > 0)
    try
    {
      return new BigDecimal (_getUnifiedDecimal (sStr)).setScale (nScale, eRoundingMode);
    }
    catch (final NumberFormatException ex)
    {
      // Fall through
    }
  return aDefault;
}
 
Example 3
Source File: NonBlockingPushbackInputStream.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Skips over and discards <code>n</code> bytes of data from this input
 * stream. The <code>skip</code> method may, for a variety of reasons, end up
 * skipping over some smaller number of bytes, possibly zero. If
 * <code>n</code> is negative, no bytes are skipped.
 * <p>
 * The <code>skip</code> method of <code>PushbackInputStream</code> first
 * skips over the bytes in the pushback buffer, if any. It then calls the
 * <code>skip</code> method of the underlying input stream if more bytes need
 * to be skipped. The actual number of bytes skipped is returned.
 *
 * @param nSkip
 *        The number of bytes to skip. Must be &ge; 0.
 * @return The number of bytes actually skipped
 * @exception IOException
 *            if the stream does not support seek, or the stream has been
 *            closed by invoking its {@link #close()} method, or an I/O error
 *            occurs.
 * @see java.io.InputStream#skip(long n)
 * @since 1.2
 */
@Override
public long skip (final long nSkip) throws IOException
{
  ValueEnforcer.isGE0 (nSkip, "SkipValue");

  _ensureOpen ();

  if (nSkip == 0)
    return 0L;

  long nRealSkip = nSkip;
  final int nBufAvail = m_aBuf.length - m_nBufPos;
  if (nBufAvail > 0)
  {
    if (nRealSkip <= nBufAvail)
    {
      m_nBufPos += nRealSkip;
      return nRealSkip;
    }
    m_nBufPos = m_aBuf.length;
    nRealSkip -= nBufAvail;
  }
  return nBufAvail + super.skip (nRealSkip);
}
 
Example 4
Source File: CSSExpressionMemberMath.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
public CSSExpressionMemberMath addMember (@Nonnegative final int nIndex,
                                          @Nonnull final ICSSExpressionMathMember aMember)
{
  ValueEnforcer.isGE0 (nIndex, "Index");
  ValueEnforcer.notNull (aMember, "Member");

  if (nIndex >= getMemberCount ())
    m_aMembers.add (aMember);
  else
    m_aMembers.add (nIndex, aMember);
  return this;
}
 
Example 5
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get an array that contains all elements, except for the last <em>n</em>
 * elements.
 *
 * @param aArray
 *        The source array. May be <code>null</code>.
 * @param nElementsToSkip
 *        The number of elements to skip. Must be &gt;= 0!
 * @return <code>null</code> if the passed array is <code>null</code> or has
 *         &le; elements than elements to be skipped. A non-<code>null</code>
 *         copy of the array without the last elements otherwise.
 */
@Nullable
@ReturnsMutableCopy
public static double [] getAllExceptLast (@Nullable final double [] aArray, @Nonnegative final int nElementsToSkip)
{
  ValueEnforcer.isGE0 (nElementsToSkip, "ElementsToSkip");

  if (nElementsToSkip == 0)
    return aArray;
  if (aArray == null || nElementsToSkip >= aArray.length)
    return null;
  return getCopy (aArray, 0, aArray.length - nElementsToSkip);
}
 
Example 6
Source File: StreamHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Fully skip the passed amounts in the input stream. Only forward skipping is
 * possible!
 *
 * @param aIS
 *        The input stream to skip in.
 * @param nBytesToSkip
 *        The number of bytes to skip. Must be &ge; 0.
 * @throws IOException
 *         In case something goes wrong internally
 */
public static void skipFully (@Nonnull final InputStream aIS, @Nonnegative final long nBytesToSkip) throws IOException
{
  ValueEnforcer.notNull (aIS, "InputStream");
  ValueEnforcer.isGE0 (nBytesToSkip, "BytesToSkip");

  long nRemaining = nBytesToSkip;
  while (nRemaining > 0)
  {
    // May only return a partial skip
    final long nSkipped = aIS.skip (nRemaining);
    if (nSkipped == 0)
    {
      // Check if we're at the end of the file or not
      // -> blocking read!
      if (aIS.read () == -1)
      {
        throw new EOFException ("Failed to skip a total of " +
                                nBytesToSkip +
                                " bytes on input stream. Only skipped " +
                                (nBytesToSkip - nRemaining) +
                                " bytes so far!");
      }
      nRemaining--;
    }
    else
    {
      // Skipped at least one char
      nRemaining -= nSkipped;
    }
  }
}
 
Example 7
Source File: StringHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get the passed string element repeated for a certain number of times. Each
 * string element is simply appended at the end of the string.
 *
 * @param sElement
 *        The string to get repeated. May not be <code>null</code>.
 * @param nRepeats
 *        The number of repetitions to retrieve. May not be &lt; 0.
 * @return A non-<code>null</code> string containing the string element for
 *         the given number of times.
 */
@Nonnull
public static String getRepeated (@Nonnull final String sElement, @Nonnegative final int nRepeats)
{
  ValueEnforcer.notNull (sElement, "Element");
  ValueEnforcer.isGE0 (nRepeats, "Repeats");

  final int nElementLength = sElement.length ();

  // Check if result length would exceed int range
  if ((long) nElementLength * nRepeats > Integer.MAX_VALUE)
    throw new IllegalArgumentException ("Resulting string exceeds the maximum integer length");

  if (nElementLength == 0 || nRepeats == 0)
    return "";
  if (nRepeats == 1)
    return sElement;

  // use character version
  if (nElementLength == 1)
    return getRepeated (sElement.charAt (0), nRepeats);

  // combine via StringBuilder
  final StringBuilder ret = new StringBuilder (nElementLength * nRepeats);
  for (int i = 0; i < nRepeats; ++i)
    ret.append (sElement);
  return ret.toString ();
}
 
Example 8
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get an array that contains all elements, except for the last <em>n</em>
 * elements.
 *
 * @param aArray
 *        The source array. May be <code>null</code>.
 * @param nElementsToSkip
 *        The number of elements to skip. Must be &gt;= 0!
 * @return <code>null</code> if the passed array is <code>null</code> or has
 *         &le; elements than elements to be skipped. A non-<code>null</code>
 *         copy of the array without the last elements otherwise.
 */
@Nullable
@ReturnsMutableCopy
public static int [] getAllExceptLast (@Nullable final int [] aArray, @Nonnegative final int nElementsToSkip)
{
  ValueEnforcer.isGE0 (nElementsToSkip, "ElementsToSkip");

  if (nElementsToSkip == 0)
    return aArray;
  if (aArray == null || nElementsToSkip >= aArray.length)
    return null;
  return getCopy (aArray, 0, aArray.length - nElementsToSkip);
}
 
Example 9
Source File: CSSExpressionMemberMathProduct.java    From ph-css with Apache License 2.0 5 votes vote down vote up
@Nonnull
public CSSExpressionMemberMathProduct addMember (@Nonnegative final int nIndex,
                                                 @Nonnull final ICSSExpressionMathMember aMember)
{
  ValueEnforcer.isGE0 (nIndex, "Index");
  ValueEnforcer.notNull (aMember, "ExpressionMathMember");

  if (nIndex >= getMemberCount ())
    m_aMembers.add (aMember);
  else
    m_aMembers.add (nIndex, aMember);
  return this;
}
 
Example 10
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Get an array that contains all elements, except for the first <em>n</em>
 * elements.
 *
 * @param aArray
 *        The source array. May be <code>null</code>.
 * @param nElementsToSkip
 *        The number of elements to skip. Must be &gt;= 0!
 * @return <code>null</code> if the passed array is <code>null</code> or has
 *         &le; elements than elements to be skipped. A non-<code>null</code>
 *         copy of the array without the first elements otherwise.
 */
@Nullable
@ReturnsMutableCopy
public static long [] getAllExceptFirst (@Nullable final long [] aArray, @Nonnegative final int nElementsToSkip)
{
  ValueEnforcer.isGE0 (nElementsToSkip, "ElementsToSkip");

  if (nElementsToSkip == 0)
    return aArray;
  if (aArray == null || nElementsToSkip >= aArray.length)
    return null;
  return getCopy (aArray, nElementsToSkip, aArray.length - nElementsToSkip);
}
 
Example 11
Source File: JsonArray.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsMutableCopy
public JsonArray getSubArray (@Nonnegative final int nStartIndex, @Nonnegative final int nEndIndex)
{
  ValueEnforcer.isGE0 (nStartIndex, "StartIndex");
  ValueEnforcer.isBetweenInclusive (nEndIndex, "EndIndex", nStartIndex, m_aValues.size ());

  final int nLength = nEndIndex - nStartIndex;
  final JsonArray ret = new JsonArray (nLength);
  ret.addAll (m_aValues.subList (nStartIndex, nEndIndex));
  return ret;
}
 
Example 12
Source File: Dijkstra.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public WorkElement (@Nullable final N aFromNode, @Nonnegative final int nDistance, @Nonnull final N aToNode)
{
  ValueEnforcer.isGE0 (nDistance, "Distance");
  ValueEnforcer.notNull (aToNode, "ToNode");
  m_aFromNode = aFromNode;
  m_nDistance = nDistance;
  m_aToNode = aToNode;
}
 
Example 13
Source File: VendorInfo.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public static void setInceptionYear (@Nonnegative final int nInceptionYear)
{
  s_nInceptionYear = ValueEnforcer.isGE0 (nInceptionYear, "InceptionYear");
}
 
Example 14
Source File: CombinationGeneratorFlexible.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Ctor
 *
 * @param nSlotCount
 *        the number of slots to use
 * @param bAllowEmpty
 *        whether or not to include an empty result set (no slot filled)
 */
public CombinationGeneratorFlexible (@Nonnegative final int nSlotCount, final boolean bAllowEmpty)
{
  ValueEnforcer.isGE0 (nSlotCount, "SlotCount");
  m_nSlotCount = nSlotCount;
  m_bAllowEmpty = bAllowEmpty;
}
 
Example 15
Source File: ThreadHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Sleep the current thread for a certain amount of time
 *
 * @param nSeconds
 *        The seconds to sleep. Must be &ge; 0.
 * @return {@link ESuccess#SUCCESS} if sleeping was not interrupted,
 *         {@link ESuccess#FAILURE} if sleeping was interrupted
 */
@Nonnull
public static ESuccess sleepSeconds (@Nonnegative final long nSeconds)
{
  ValueEnforcer.isGE0 (nSeconds, "Seconds");
  return sleep (nSeconds * CGlobal.MILLISECONDS_PER_SECOND);
}
 
Example 16
Source File: NonBlockingCharArrayWriter.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Converts input data to a string.
 *
 * @param nOfs
 *        The offset to start at. Must be &ge; 0.
 * @param nLength
 *        The number of characters to convert. Must be &le; than
 *        {@link #getSize()}.
 * @return the string.
 */
@Nonnull
public String getAsString (@Nonnegative final int nOfs, @Nonnegative final int nLength)
{
  ValueEnforcer.isGE0 (nOfs, "Index");
  ValueEnforcer.isBetweenInclusive (nLength, "Length", 0, m_nCount);
  return new String (m_aBuf, nOfs, nLength);
}
 
Example 17
Source File: NonBlockingStringReader.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Marks the present position in the stream. Subsequent calls to reset() will
 * reposition the stream to this point.
 *
 * @param nReadAheadLimit
 *        Limit on the number of characters that may be read while still
 *        preserving the mark. Because the stream's input comes from a string,
 *        there is no actual limit, so this argument must not be negative, but
 *        is otherwise ignored.
 * @exception IllegalArgumentException
 *            If readAheadLimit is &lt; 0
 * @exception IOException
 *            If an I/O error occurs
 */
@Override
public void mark (final int nReadAheadLimit) throws IOException
{
  ValueEnforcer.isGE0 (nReadAheadLimit, "ReadAheadLimit");

  _ensureOpen ();
  m_nMark = m_nNext;
}
 
Example 18
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new array with a predefined number of elements containing the
 * passed value.
 *
 * @param <ELEMENTTYPE>
 *        The type of the array to be created.
 * @param nArraySize
 *        The size of the array to be created.
 * @param aValue
 *        The value to be set into each array element. May be
 *        <code>null</code>.
 * @param aClass
 *        The value class. May not be <code>null</code>. Must be present in
 *        case the passed value is <code>null</code>.
 * @return The created array filled with the given value.
 */
@Nonnull
@ReturnsMutableCopy
public static <ELEMENTTYPE> ELEMENTTYPE [] newArray (@Nonnegative final int nArraySize,
                                                     @Nonnull final ELEMENTTYPE aValue,
                                                     @Nonnull final Class <ELEMENTTYPE> aClass)
{
  ValueEnforcer.isGE0 (nArraySize, "ArraySize");
  ValueEnforcer.notNull (aClass, "class");

  final ELEMENTTYPE [] ret = newArray (aClass, nArraySize);
  Arrays.fill (ret, aValue);
  return ret;
}
 
Example 19
Source File: ThreadHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Sleep the current thread for a certain amount of time
 *
 * @param nMinutes
 *        The minutes to sleep. Must be &ge; 0.
 * @return {@link ESuccess#SUCCESS} if sleeping was not interrupted,
 *         {@link ESuccess#FAILURE} if sleeping was interrupted
 */
@Nonnull
public static ESuccess sleepMinutes (@Nonnegative final long nMinutes)
{
  ValueEnforcer.isGE0 (nMinutes, "Minutes");
  return sleep (nMinutes * CGlobal.MILLISECONDS_PER_MINUTE);
}
 
Example 20
Source File: XPathFunctionKey.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor
 *
 * @param aFunctionName
 *        Function name. May not be <code>null</code>.
 * @param nArity
 *        The number of parameters the function takes. Must be &ge; 0.
 */
public XPathFunctionKey (@Nonnull final QName aFunctionName, @Nonnegative final int nArity)
{
  m_aFunctionName = ValueEnforcer.notNull (aFunctionName, "FunctionName");
  m_nArity = ValueEnforcer.isGE0 (nArity, "Arity");
}