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: 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;

    public static String doubleToPrefixCoded(double val) {
        return longToPrefixCoded(doubleToSortableLong(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_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;
    }

    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);
    }

    public static String longToPrefixCoded(final long val) {
        return longToPrefixCoded(val, 0);
    }

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