Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

In this page you can find the example usage for java.lang Math min.

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

public static SpannableString getSmallCapsString(String input) {
    // values needed to record start/end points of blocks of lowercase letters
    char[] chars = input.toCharArray();
    int currentBlock = 0;
    int[] blockStarts = new int[chars.length];
    int[] blockEnds = new int[chars.length];
    boolean blockOpen = false;

    // record where blocks of lowercase letters start/end
    for (int i = 0; i < chars.length; ++i) {
        char c = chars[i];
        if (c >= 'a' && c <= 'z') {
            if (!blockOpen) {
                blockOpen = true;//  ww w .ja va  2s  .c  o  m
                blockStarts[currentBlock] = i;
            }
            // replace with uppercase letters
            chars[i] = (char) (c - 'a' + '\u0041');
        } else {
            if (blockOpen) {
                blockOpen = false;
                blockEnds[currentBlock] = i;
                ++currentBlock;
            }
        }
    }

    // add the string end, in case the last character is a lowercase letter
    blockEnds[currentBlock] = chars.length;

    // shrink the blocks found above
    SpannableString output = new SpannableString(String.valueOf(chars));
    for (int i = 0; i < Math.min(blockStarts.length, blockEnds.length); ++i) {
        output.setSpan(new RelativeSizeSpan(0.8f), blockStarts[i], blockEnds[i],
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    }

    return output;
}

From source file:com.linkedin.paldb.impl.GenerateTestData.java

public static Integer[] generateRandomIntKeys(int count, long seed) {
    return generateRandomIntKeys(count, Math.min(count * 50, Integer.MAX_VALUE), seed);
}

From source file:Main.java

public static <E> List<E> asList(E... elements) {
    if (elements == null || elements.length == 0) {
        return Collections.emptyList();
    }//  www. ja  v  a 2  s .  c  o  m
    // Avoid integer overflow when a large array is passed in
    int capacity = (int) Math.min(5L + elements.length + (elements.length / 10), Integer.MAX_VALUE);
    ArrayList<E> list = new ArrayList<E>(capacity);
    Collections.addAll(list, elements);
    return list;
}

From source file:edu.washington.gs.skyline.model.quantification.PValues.java

public static double[] adjustPValues(double[] pValues) {
    List<Pair<Double, Integer>> entries = new ArrayList<>();
    DoubleStream.of(pValues).forEach(value -> entries.add(Pair.of(value, entries.size())));
    Collections.sort(entries);/*w  w  w  .j  a v  a  2  s .co m*/
    double currentMin = 1.0;
    double[] result = new double[entries.size()];
    for (int i = entries.size() - 1; i >= 0; i--) {
        double value = entries.get(i).getLeft() * entries.size() / (i + 1);
        currentMin = Math.min(value, currentMin);
        result[entries.get(i).getRight()] = currentMin;
    }
    return result;
}

From source file:Main.java

/**
 * Applies the given style to a range of the input CharSequence.
 * @param style The style to apply (see the style constants in {@link Typeface}).
 * @param input The CharSequence to style.
 * @param start Starting index of the range to style (will be clamped to be a minimum of 0).
 * @param end Ending index of the range to style (will be clamped to a maximum of the input
 *     length)./*  ww  w  . j  a  va 2s .c  o  m*/
 * @param flags Bitmask for configuring behavior of the span.  See {@link android.text.Spanned}.
 * @return The styled CharSequence.
 */
public static CharSequence applyStyleToSpan(int style, CharSequence input, int start, int end, int flags) {
    // Enforce bounds of the char sequence.
    start = Math.max(0, start);
    end = Math.min(input.length(), end);
    SpannableString text = new SpannableString(input);
    text.setSpan(new StyleSpan(style), start, end, flags);
    return text;
}

From source file:Main.java

/**
 * Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size,
 * while staying as least as large as the width and height parameters.
 *//*ww  w .  j  a v  a 2 s.c  om*/
@Nullable
public static Bitmap scaledBitmapFromURIWithMinimumSize(Context context, Uri imageURI, int width, int height)
        throws FileNotFoundException {
    BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI);
    options.inJustDecodeBounds = false;

    float wratio = 1.0f * options.outWidth / width;
    float hratio = 1.0f * options.outHeight / height;
    options.inSampleSize = (int) Math.min(wratio, hratio);

    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options);
    //        return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI));
}

From source file:Main.java

public static Vector<byte[]> split(int command, byte[] dataToTransport, int chunksize) {
    Vector<byte[]> result = new Vector<byte[]>();
    if (chunksize < 8) {
        throw new RuntimeException("Invalid chunk size");
    }// ww w.j  a v a  2  s  . co  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int remaining_length = dataToTransport.length;
    int offset = 0;
    int seq = 0;
    boolean firstPacket = true;

    while (remaining_length > 0) {
        int l = 0;
        if (!firstPacket) {
            baos.write(seq);
            l = Math.min(chunksize - 1, remaining_length);
        } else {
            baos.write(command);
            // first packet has the total transport length
            baos.write(remaining_length >> 8);
            baos.write(remaining_length);
            l = Math.min(chunksize - 3, remaining_length);
        }
        baos.write(dataToTransport, offset, l);
        remaining_length -= l;
        offset += l;
        result.add(baos.toByteArray());
        baos.reset();
        if (!firstPacket) {
            seq++;
        }
        firstPacket = false;
    }

    return result;
}

From source file:Main.java

public static long skip(InputStream input, long toSkip) throws IOException {
    if (toSkip < 0L) {
        throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip);
    } else {//w w  w . j  av  a2s . com
        if (SKIP_BYTE_BUFFER == null) {
            SKIP_BYTE_BUFFER = new byte[2048];
        }

        long remain;
        long n;
        for (remain = toSkip; remain > 0L; remain -= n) {
            n = (long) input.read(SKIP_BYTE_BUFFER, 0, (int) Math.min(remain, 2048L));
            if (n < 0L) {
                break;
            }
        }

        return toSkip - remain;
    }
}

From source file:Main.java

public static void setBestExposure(Camera.Parameters parameters, boolean lightOn) {
    int minExposure = parameters.getMinExposureCompensation();
    int maxExposure = parameters.getMaxExposureCompensation();
    float step = parameters.getExposureCompensationStep();
    if ((minExposure != 0 || maxExposure != 0) && step > 0.0f) {
        // Set low when light is on
        float targetCompensation = lightOn ? MIN_EXPOSURE_COMPENSATION : MAX_EXPOSURE_COMPENSATION;
        int compensationSteps = Math.round(targetCompensation / step);
        //            float actualCompensation = step * compensationSteps;
        // Clamp value:
        compensationSteps = Math.max(Math.min(compensationSteps, maxExposure), minExposure);
        if (parameters.getExposureCompensation() != compensationSteps) {
            //                Log.i(TAG, "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation);
            //            } else {
            //                Log.i(TAG, "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation);
            parameters.setExposureCompensation(compensationSteps);
        }/*w  w w. j  a v  a  2s .com*/
        //        } else {
        //            Log.i(TAG, "Camera does not support exposure compensation");
    }
}

From source file:Main.java

public static byte[] resize(byte[] picArray, int size, boolean faceDetect) {
    if (picArray == null) {
        return null;
    }/*from w  w w.j ava2  s . co m*/
    Bitmap pic = BitmapFactory.decodeByteArray(picArray, 0, picArray.length);
    if (pic == null) {
        return null;
    }

    size = Math.min(size, Math.min(pic.getHeight(), pic.getWidth()));
    if (size % 2 != 0)
        size--;
    Log.i("sizes", "old width:" + pic.getWidth() + " old height:" + pic.getHeight() + " new size:" + size);
    Bitmap scaledPic = scale(pic, size);

    int width = scaledPic.getWidth();
    int height = scaledPic.getHeight();

    //if pic is already square, we are done now
    if (width == height) {
        return bitmapToBytes(scaledPic);
    }

    PointF mid = null;
    int cropcenter;

    if (faceDetect)
        mid = findFaceMid(scaledPic);

    Bitmap out;
    if (width > height) {
        if (mid != null)
            cropcenter = Math.max(size / 2, Math.min((int) Math.floor(mid.y), width - size / 2));
        else
            cropcenter = width / 2;
        Log.i("CROPPING", "width:" + width + " center:" + cropcenter + " size:" + size + " left edge:"
                + (cropcenter - size / 2) + " right edge:" + (cropcenter + size / 2));
        out = Bitmap.createBitmap(scaledPic, cropcenter - size / 2, 0, size, size);
    } else {
        if (mid != null)
            cropcenter = Math.max(size / 2, Math.min((int) Math.floor(mid.x), height - size / 2));
        else
            cropcenter = height / 2;
        out = Bitmap.createBitmap(scaledPic, 0, 0, size, size);
    }

    return bitmapToBytes(out);
}