Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * 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: Integers are stored at lower precision by shifting off lower bits. The shift count is
     * stored as <code>SHIFT_START_INT+shift</code> in the first character
     */
    public static final char SHIFT_START_INT = (char) 0x60;
    /**
     * Expert: The maximum term length (used for <code>char[]</code> buffer size)
     * for encoding <code>int</code> values.
     * @see #intToPrefixCoded(int,int,char[])
     */
    public static final int BUF_SIZE_INT = 31 / 7 + 2;

    public static String floatToPrefixCoded(float val) {
        return intToPrefixCoded(floatToSortableInt(val));
    }

    /**
     * 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_INT}
     * length
     * @return number of chars written to buffer
     */
    public static int intToPrefixCoded(final int val, final int shift, final char[] buffer) {
        if (shift > 31 || shift < 0)
            throw new IllegalArgumentException("Illegal shift value, must be 0..31");
        int nChars = (31 - shift) / 7 + 1, len = nChars + 1;
        buffer[0] = (char) (SHIFT_START_INT + shift);
        int sortableBits = val ^ 0x80000000;
        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;
    }

    public static String intToPrefixCoded(final int val, final int shift) {
        final char[] buffer = new char[BUF_SIZE_INT];
        final int len = intToPrefixCoded(val, shift, buffer);
        return new String(buffer, 0, len);
    }

    public static String intToPrefixCoded(final int val) {
        return intToPrefixCoded(val, 0);
    }

    /**
     * Converts a <code>float</code> value to a sortable signed <code>int</code>.
     * The value is converted by getting their IEEE 754 floating-point &quot;float format&quot;
     * bit layout and then some bits are swapped, to be able to compare the result as int.
     * By this the precision is not reduced, but the value can easily used as an int.
     * The sort order (including {@link Float#NaN}) is defined by
     * {@link Float#compareTo}; {@code NaN} is greater than positive infinity.
     * @see #sortableIntToFloat
     */
    public static int floatToSortableInt(float val) {
        int f = Float.floatToIntBits(val);
        if (f < 0)
            f ^= 0x7fffffff;
        return f;
    }
}