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

/**
 *
 * @param <T>/* w w w . j a va  2s. c  om*/
 * @param list
 * @param fromIndex
 * @param toIndex
 * @return
 */
public static <T> List<T> subListReverse(List<T> list, int fromIndex, int toIndex) {
    List<T> subList = new Vector<T>(fromIndex - toIndex);
    for (int i = Math.min(fromIndex, list.size()) - 1; (i >= toIndex) && (i >= 0); i--) {
        T obj = list.get(i);
        subList.add(obj);
    }
    return subList;
}

From source file:Main.java

public static List<String> getParts(String string, int partitionSize) {
    List<String> parts = new ArrayList<String>();
    int len = string.length();
    for (int i = 0; i < len; i += partitionSize) {
        parts.add(string.substring(i, Math.min(len, i + partitionSize)));
    }// w ww . j  a va  2s  .c  om
    return parts;
}

From source file:Main.java

public static <T> T[] ensureMinLength(T[] array, int length) {

    if (array.length >= length) {
        return array;
    }// ww  w . j ava 2s.  co m

    T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), length);
    if (array.length > 0) {
        System.arraycopy(array, 0, newArray, 0, Math.min(array.length, length));
    }
    return newArray;
}

From source file:Main.java

public static double getBearingDifference(double a1, double a2) {
    return Math.min((a1 - a2) < 0 ? a1 - a2 + 360 : a1 - a2, (a2 - a1) < 0 ? a2 - a1 + 360 : a2 - a1);
}

From source file:Main.java

/**
 * @param s1//from w w w  .jav a 2s  . co m
 * @param s2
 * @return the common string in both {@code s1} and {@code s2}
 */
public static String getCommonString(String s1, String s2) {
    int l1 = s1.length();
    int l2 = s2.length();
    if (l1 == l2) {
        return s1;
    }

    int length = Math.min(l1, l2);

    String returnString = null;
    for (int i = 0; i < length; i++) {
        if (s1.charAt(i) == s2.charAt(i)) {
            continue;
        }
        returnString = s1.substring(0, i);
        break;
    }
    if (returnString == null) {
        returnString = l1 < l2 ? s1 : s2;
    }

    if (returnString.endsWith("/")) {
        int l = returnString.length();
        if (l > 1) {
            return returnString.substring(0, l - 1);
        }
    }
    return returnString;
}

From source file:Main.java

private static byte[] copyArray(byte[] original, int start, int length) {
    byte[] copy = new byte[length];
    System.arraycopy(original, start, copy, 0, Math.min(original.length - start, length));
    return copy;/*  w w  w  . j ava 2 s .c om*/
}

From source file:Main.java

public static byte[][] chunkArray(byte[] array, int chunkSize) {
    int numOfChunks = (int) Math.ceil((double) array.length / chunkSize);
    byte[][] output = new byte[numOfChunks][];

    for (int i = 0; i < numOfChunks; ++i) {
        int start = i * chunkSize;
        int length = Math.min(array.length - start, chunkSize);

        byte[] temp = new byte[length];
        System.arraycopy(array, start, temp, 0, length);
        output[i] = temp;//from  w  w w  . j ava 2  s  .  c  om
    }

    return output;
}

From source file:Main.java

public static <T> List<T> safeSubList(List<T> list, int fromIndex, int toIndex) {
    int size = list.size();
    if (fromIndex >= size || toIndex <= 0 || fromIndex >= toIndex) {
        return Collections.emptyList();
    }/*from  w  ww. ja  v a2  s .  c o m*/

    fromIndex = Math.max(0, fromIndex);
    toIndex = Math.min(size, toIndex);

    return list.subList(fromIndex, toIndex);
}

From source file:Main.java

/**
 * Resizes an array of ints to a specified size.
 *
 * @param list Array to be resized.//  w ww.j  a v a 2 s  .  c  om
 * @param newSize Array size after resizing
 * @return Array of ints with the specified size
 */
static public int[] resizeArrayInt(int[] val, int newSize) {
    int[] newval = new int[newSize];
    System.arraycopy(val, 0, newval, 0, Math.min(val.length, newSize));
    return newval;
}

From source file:Main.java

/**
 * return either the first space or the first nbsp
 *//*w  w w  . j  a v a 2  s  . c  o m*/
public static int findSpace(String haystack, int begin) {
    int space = haystack.indexOf(' ', begin);
    int nbsp = haystack.indexOf('\u00A0', begin);
    if (space == -1 && nbsp == -1) {
        return -1;
    } else if (space >= 0 && nbsp >= 0) {
        return Math.min(space, nbsp);
    } else {
        // eg one is -1, and the other is >= 0
        return Math.max(space, nbsp);
    }
}