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

The following examples show how to use com.helger.commons.ValueEnforcer#isBetweenInclusive() . 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: CharsetHelper.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get the number of bytes necessary to represent the passed character.
 *
 * @param c
 *        The character to be evaluated.
 * @return A non-negative value.
 */
@Nonnegative
public static int getUTF8ByteCount (@Nonnegative final int c)
{
  ValueEnforcer.isBetweenInclusive (c, "c", Character.MIN_VALUE, Character.MAX_VALUE);

  // see JVM spec 4.4.7, p 111
  // http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html
  // #1297
  if (c == 0)
    return 2;

  // Source: http://icu-project.org/apiref/icu4c/utf8_8h_source.html
  if (c <= 0x7f)
    return 1;
  if (c <= 0x7ff)
    return 2;
  if (c <= 0xd7ff)
    return 3;

  // It's a surrogate...
  return 0;
}
 
Example 2
Source File: CombinationGenerator.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Ctor
 *
 * @param aElements
 *        the elements to fill into the slots for creating all combinations
 *        (must not be empty!)
 * @param nSlotCount
 *        the number of slots to use (must not be greater than the element
 *        count!)
 */
public CombinationGenerator (@Nonnull @Nonempty final ICommonsList <DATATYPE> aElements,
                             @Nonnegative final int nSlotCount)
{
  ValueEnforcer.notEmpty (aElements, "Elements");
  ValueEnforcer.isBetweenInclusive (nSlotCount, "SlotCount", 0, aElements.size ());

  m_aElements = GenericReflection.uncheckedCast (aElements.toArray ());
  m_aIndexResult = new int [nSlotCount];
  final BigInteger aElementFactorial = FactorialHelper.getAnyFactorialLinear (m_aElements.length);
  final BigInteger aSlotFactorial = FactorialHelper.getAnyFactorialLinear (nSlotCount);
  final BigInteger aOverflowFactorial = FactorialHelper.getAnyFactorialLinear (m_aElements.length - nSlotCount);
  m_aTotalCombinations = aElementFactorial.divide (aSlotFactorial.multiply (aOverflowFactorial));
  // Can we use the fallback to long? Is much faster than using BigInteger
  m_bUseLong = m_aTotalCombinations.compareTo (CGlobal.BIGINT_MAX_LONG) < 0;
  m_nTotalCombinations = m_bUseLong ? m_aTotalCombinations.longValue () : CGlobal.ILLEGAL_ULONG;
  reset ();
}
 
Example 3
Source File: IntObjectMap.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public IntObjectMap (final int nSize, final float fFillFactor)
{
  ValueEnforcer.isBetweenInclusive (fFillFactor, "FillFactor", 0f, 1f);
  ValueEnforcer.isGT0 (nSize, "Size");
  final int nCapacity = MapHelper.arraySize (nSize, fFillFactor);
  m_nMask = nCapacity - 1;
  m_fFillFactor = fFillFactor;

  m_aKeys = new int [nCapacity];
  m_aValues = _createValueArray (nCapacity);
  m_nThreshold = (int) (nCapacity * fFillFactor);
}
 
Example 4
Source File: IntFloatMap.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public IntFloatMap (final int nSize, final float fFillFactor)
{
  ValueEnforcer.isBetweenInclusive (fFillFactor, "FillFactor", 0f, 1f);
  ValueEnforcer.isGT0 (nSize, "Size");
  final int nCapacity = MapHelper.arraySize (nSize, fFillFactor);
  m_nMask = nCapacity - 1;
  m_fFillFactor = fFillFactor;

  m_aKeys = new int [nCapacity];
  m_aValues = _createValueArray (nCapacity);
  m_nThreshold = (int) (nCapacity * fFillFactor);
}
 
Example 5
Source File: IntIntMap.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public IntIntMap (final int nSize, final float fFillFactor)
{
  ValueEnforcer.isBetweenInclusive (fFillFactor, "FillFactor", 0f, 1f);
  ValueEnforcer.isGT0 (nSize, "Size");
  final int nCapacity = MapHelper.arraySize (nSize, fFillFactor);
  m_nMask = nCapacity - 1;
  m_fFillFactor = fFillFactor;

  m_aKeys = new int [nCapacity];
  m_aValues = _createValueArray (nCapacity);
  m_nThreshold = (int) (nCapacity * fFillFactor);
}
 
Example 6
Source File: PDTHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Convert from Calendar day of week to {@link DayOfWeek} enum.
 *
 * @param nCalendarDayOfWeek
 *        Day of week - must be between 1 and 7.
 * @return {@link DayOfWeek} and never <code>null</code>.
 * @since 8.6.3
 */
@Nonnull
public static DayOfWeek getAsDayOfWeek (final int nCalendarDayOfWeek)
{
  ValueEnforcer.isBetweenInclusive (nCalendarDayOfWeek, "DayOfWeek", 1, 7);
  // Convert Calendar DoW to enum DoW
  final int nIndex = (nCalendarDayOfWeek + 6) % 7;
  return DayOfWeek.of (nIndex == 0 ? 7 : nIndex);
}
 
Example 7
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 8
Source File: StringScanner.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nonnull
public StringScanner setIndex (@Nonnegative final int nIndex)
{
  ValueEnforcer.isBetweenInclusive (nIndex, "Index", 0, m_nMaxIndex);
  m_nCurIndex = nIndex;
  return this;
}
 
Example 9
Source File: IntDoubleMap.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public IntDoubleMap (final int nSize, final float fFillFactor)
{
  ValueEnforcer.isBetweenInclusive (fFillFactor, "FillFactor", 0f, 1f);
  ValueEnforcer.isGT0 (nSize, "Size");
  final int nCapacity = MapHelper.arraySize (nSize, fFillFactor);
  m_nMask = nCapacity - 1;
  m_fFillFactor = fFillFactor;

  m_aKeys = new int [nCapacity];
  m_aValues = _createValueArray (nCapacity);
  m_nThreshold = (int) (nCapacity * fFillFactor);
}
 
Example 10
Source File: LZWCodec.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public LZWNode (@Nonnegative final int nTableIndex)
{
  ValueEnforcer.isBetweenInclusive (nTableIndex, "TableIndex", 0, AbstractLZWDictionary.MAX_CODE);
  m_nTableIndex = nTableIndex;
}
 
Example 11
Source File: FactorialHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Calculate n! whereas n must be in the range of
 * {@value #PREDEFINED_MIN_INDEX} to {@link #PREDEFINED_MAX_INDEX}.
 *
 * @param n
 *        Input value
 * @return The factorial value.
 */
@Nonnegative
public static long getSmallFactorial (@Nonnegative final int n)
{
  ValueEnforcer.isBetweenInclusive (n, "n", PREDEFINED_MIN_INDEX, PREDEFINED_MAX_INDEX);
  return PREDEFINED_FACTORIALS_LONG[n];
}
 
Example 12
Source File: NonBlockingBufferedWriter.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Converts input data to a string.
 *
 * @param nLength
 *        The number of characters to convert. Must be &le; than
 *        {@link #getSize()}.
 * @return the string.
 */
@Nonnull
public String getAsString (@Nonnegative final int nLength)
{
  ValueEnforcer.isBetweenInclusive (nLength, "Length", 0, m_nNextChar);
  return new String (m_aBuf, 0, nLength);
}
 
Example 13
Source File: NonBlockingByteArrayOutputStream.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Converts the buffer's contents into a string by decoding the bytes using
 * the specified {@link java.nio.charset.Charset charsetName}. The length of
 * the new <tt>String</tt> is a function of the charset, and hence may not be
 * equal to the length of the byte array.
 * <p>
 * This method always replaces malformed-input and unmappable-character
 * sequences with this charset's default replacement string. The
 * {@link java.nio.charset.CharsetDecoder} class should be used when more
 * control over the decoding process is required.
 *
 * @param nLength
 *        The number of bytes to be converted to a String. Must be &ge; 0.
 * @param aCharset
 *        the charset to be used. May not be <code>null</code>.
 * @return String decoded from the buffer's contents.
 */
@Nonnull
public String getAsString (@Nonnegative final int nLength, @Nonnull final Charset aCharset)
{
  ValueEnforcer.isBetweenInclusive (nLength, "Length", 0, m_nCount);
  return new String (m_aBuf, 0, nLength, aCharset);
}
 
Example 14
Source File: NonBlockingByteArrayOutputStream.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Converts the buffer's contents into a string by decoding the bytes using
 * the specified {@link java.nio.charset.Charset charsetName}. The length of
 * the new <tt>String</tt> is a function of the charset, and hence may not be
 * equal to the length of the byte array.
 * <p>
 * This method always replaces malformed-input and unmappable-character
 * sequences with this charset's default replacement string. The
 * {@link java.nio.charset.CharsetDecoder} class should be used when more
 * control over the decoding process is required.
 *
 * @param nOfs
 *        The start index to use
 * @param nLength
 *        The number of bytes to be converted to a String. Must be &ge; 0.
 * @param aCharset
 *        the charset to be used. May not be <code>null</code>.
 * @return String decoded from the buffer's contents.
 */
@Nonnull
public String getAsString (@Nonnegative final int nOfs,
                           @Nonnegative final int nLength,
                           @Nonnull final Charset aCharset)
{
  ValueEnforcer.isGE0 (nOfs, "Index");
  ValueEnforcer.isBetweenInclusive (nLength, "Length", 0, m_nCount);
  return new String (m_aBuf, nOfs, nLength, aCharset);
}
 
Example 15
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 nLength
 *        The number of characters to convert. Must be &le; than
 *        {@link #getSize()}.
 * @return the string.
 */
@Nonnull
public String getAsString (@Nonnegative final int nLength)
{
  ValueEnforcer.isBetweenInclusive (nLength, "Length", 0, m_nCount);
  return new String (m_aBuf, 0, nLength);
}
 
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: NonBlockingBitInputStream.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Read a specified number of bits and return them combined as an integer
 * value. The bits are written to the integer starting at the highest bit (
 * &lt;&lt; aNumberOfBits ), going down to the lowest bit ( &lt;&lt; 0 ), so
 * the returned ByteOrder is always LITTLE_ENDIAN!
 *
 * @param aNumberOfBits
 *        defines how many bits to read from the stream.
 * @return integer value containing the bits read from the stream.
 * @throws IOException
 *         In case EOF is reached
 */
public int readBits (@Nonnegative final int aNumberOfBits) throws IOException
{
  ValueEnforcer.isBetweenInclusive (aNumberOfBits, "NumberOfBits", 1, CGlobal.BITS_PER_INT);

  int ret = 0;
  for (int i = aNumberOfBits - 1; i >= 0; i--)
    ret |= (readBit () << i);
  return ret;
}
 
Example 18
Source File: NonBlockingBitOutputStream.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Write the specified number of bits from the int value to the stream.
 * Corresponding to the InputStream, the bits are written starting at the
 * highest bit ( &gt;&gt; aNumberOfBits ), going down to the lowest bit (
 * &gt;&gt; 0 ).
 *
 * @param aValue
 *        the int containing the bits that should be written to the stream.
 * @param nNumBits
 *        how many bits of the integer should be written to the stream.
 * @throws IOException
 *         In case writing to the output stream failed
 */
public void writeBits (final int aValue, @Nonnegative final int nNumBits) throws IOException
{
  ValueEnforcer.isBetweenInclusive (nNumBits, "NumberOfBits", 1, CGlobal.BITS_PER_INT);

  for (int i = nNumBits - 1; i >= 0; i--)
    writeBit ((aValue >> i) & 1);
}
 
Example 19
Source File: RomanNumeral.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor. Creates the Roman number with the int value specified by the
 * parameter. Throws a IllegalArgumentException if arabic is not in the range
 * 1 to 3999 inclusive.
 *
 * @param nValue
 *        The value to be converted
 */
private RomanNumeral (@Nonnegative final int nValue)
{
  ValueEnforcer.isBetweenInclusive (nValue, "Value", MIN_VAL, MAX_VAL);
  m_nValue = nValue;
}
 
Example 20
Source File: NonBlockingByteArrayOutputStream.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Get the byte at the specified index
 *
 * @param nIndex
 *        The index to use. Must be &ge; 0 and &lt; count
 * @return The byte at the specified position
 */
public byte getByteAt (@Nonnegative final int nIndex)
{
  ValueEnforcer.isBetweenInclusive (nIndex, "Index", 0, m_nCount - 1);
  return m_aBuf[nIndex];
}