Java Long to longToPrefixCoded(final long val, final int shift, final char[] buffer)

Here you can find the source of longToPrefixCoded(final long val, final int shift, final char[] buffer)

Description

Expert: Returns prefix coded bits after reducing the precision by shift bits.

License

Apache License

Parameter

Parameter Description
val the numeric value
shift how many bits to strip from the right
buffer that will contain the encoded chars, must be at least of #BUF_SIZE_LONG length

Return

number of chars written to buffer

Declaration

public static int longToPrefixCoded(final long val, final int shift, final char[] buffer) 

Method Source Code

//package com.java2s;
/**//from   w  ww. j a va  2 s  . co m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Expert: Longs are stored at lower precision by shifting off lower bits. The shift count is
     * stored as <code>SHIFT_START_LONG+shift</code> in the first character
     */
    public static final char SHIFT_START_LONG = (char) 0x20;
    /**
     * Expert: The maximum term length (used for <code>char[]</code> buffer size)
     * for encoding <code>long</code> values.
     * @see #longToPrefixCoded(long,int,char[])
     */
    public static final int BUF_SIZE_LONG = 63 / 7 + 2;

    /**
     * Expert: Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
     * This is method is used by {@link NumericTokenStream}.
     * @param val the numeric value
     * @param shift how many bits to strip from the right
     * @param buffer that will contain the encoded chars, must be at least of {@link #BUF_SIZE_LONG}
     * length
     * @return number of chars written to buffer
     */
    public static int longToPrefixCoded(final long val, final int shift, final char[] buffer) {
        if (shift > 63 || shift < 0)
            throw new IllegalArgumentException("Illegal shift value, must be 0..63");
        int nChars = (63 - shift) / 7 + 1, len = nChars + 1;
        buffer[0] = (char) (SHIFT_START_LONG + shift);
        long sortableBits = val ^ 0x8000000000000000L;
        sortableBits >>>= shift;
        while (nChars >= 1) {
            // Store 7 bits per character for good efficiency when UTF-8 encoding.
            // The whole number is right-justified so that lucene can prefix-encode
            // the terms more efficiently.
            buffer[nChars--] = (char) (sortableBits & 0x7f);
            sortableBits >>>= 7;
        }
        return len;
    }

    /**
     * Expert: Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
     * This is method is used by {@link LongRangeBuilder}.
     * @param val the numeric value
     * @param shift how many bits to strip from the right
     */
    public static String longToPrefixCoded(final long val, final int shift) {
        final char[] buffer = new char[BUF_SIZE_LONG];
        final int len = longToPrefixCoded(val, shift, buffer);
        return new String(buffer, 0, len);
    }

    /**
     * This is a convenience method, that returns prefix coded bits of a long without
     * reducing the precision. It can be used to store the full precision value as a
     * stored field in index.
     * <p>To decode, use {@link #prefixCodedToLong}.
     */
    public static String longToPrefixCoded(final long val) {
        return longToPrefixCoded(val, 0);
    }
}

Related

  1. longToNetworkByteOrderArray(long addr)
  2. longToNetworkBytes(long value)
  3. longToNumberWithDecimalPlaces(long longValue, int decimalPlaces)
  4. LongToOctString(final long value)
  5. longToOther(Object val, Class dest)
  6. longToRegisters(long v)
  7. longToRemoteAddrTo(long remoteAddr)
  8. longToSortableBytes(long value, byte[] result, int offset)
  9. longToStringAddress(long addr)