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 float blendColorBurn(float cS, float cB) {
    if (cS == 0) {
        return 0;
    } else {/*from   ww w .ja v a  2  s. c om*/
        return 1f - Math.min(1, (1 - cB) / cS);
    }
}

From source file:Main.java

protected static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*from w  w  w. j  av  a 2s.  c  om*/
}

From source file:Main.java

/**
 * Compute edit distance between two strings = Levenshtein distance *
 *///w  w w .j  av a 2s.  c  o  m
public static int getLevenshteinDistance(final String s, final String t) {
    if (s == null || t == null) {
        throw new IllegalArgumentException("Strings must not be null");
    }

    final int n = s.length(); // length of s
    final int m = t.length(); // length of t

    if (n == 0) {
        return m;
    } else if (m == 0) {
        return n;
    }

    int p[] = new int[n + 1]; //'previous' cost array, horizontally
    int d[] = new int[n + 1]; // cost array, horizontally
    int _d[]; //placeholder to assist in swapping p and d

    // indexes into strings s and t
    int i; // iterates through s
    int j; // iterates through t
    char t_j; // jth character of t
    int cost; // cost
    for (i = 0; i <= n; i++) {
        p[i] = i;
    }
    for (j = 1; j <= m; j++) {
        t_j = t.charAt(j - 1);
        d[0] = j;
        for (i = 1; i <= n; i++) {
            cost = s.charAt(i - 1) == t_j ? 0 : 1;
            // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
            d[i] = Math.min(Math.min(d[i - 1] + 1, p[i] + 1), p[i - 1] + cost);
        }
        // copy current distance counts to 'previous row' distance counts
        _d = p;
        p = d;
        d = _d;
    }

    // our last action in the above loop was to switch d and p, so p now
    // actually has the most recent cost counts
    return p[n];
}

From source file:Main.java

/**
 * This method calculates a lighter color provided a factor of increase in lightness.
 *
 * @param color A color value//from  www . j a v a2  s. c o m
 * @param factor Factor of increase in lightness, range can be between 0 - 1.0
 * @return  The calculated darker color
 */
public static int calculateLighterColor(final int color, final float factor) {
    final int a = Color.alpha(color);
    final int r = Color.red(color);
    final int g = Color.green(color);
    final int b = Color.blue(color);

    final int lightnessLevel = Math.round(RGB_TOTAL_COLORS * factor);

    return Color.argb(a, Math.min(r + lightnessLevel, 255), Math.min(g + lightnessLevel, 255),
            Math.min(b + lightnessLevel, 255));
}

From source file:com.mirth.connect.donkey.util.StringUtil.java

/**
 * A memory efficient method for getting bytes from a string. It first calculates the required
 * byte array size to avoid any buffers growing out of control.
 * The main purpose of the method is to alleviate the use case when charset = "UTF-8". For some
 * reason, using new String(string, "UTF-8") requires
 * memory many times the size of the string itself. There is a performance decrease over the
 * Java method of about 30-100%, so this method would ideally only
 * be used when memory is an issue.//from   w  w  w.j  av  a  2 s  . c om
 */
public static byte[] getBytesUncheckedChunked(String string, String charset) {
    int offset = 0;
    int length = string.length();

    // Calculate the size of the string after byte encoding
    ByteCounterOutputStream outputStream = new ByteCounterOutputStream();
    while ((length - offset) > 0) {
        int segmentSize = Math.min(CHUNK_SIZE, length - offset);
        outputStream
                .write(StringUtils.getBytesUnchecked(string.substring(offset, offset + segmentSize), charset));
        offset += segmentSize;
    }

    // Create a byte array the size of the exact size required
    byte[] data = new byte[outputStream.size()];
    int position = 0;
    offset = 0;

    // Perform the conversion again and write the data to the byte array
    while ((length - offset) > 0) {
        int segmentSize = Math.min(CHUNK_SIZE, length - offset);
        byte[] segment = StringUtils.getBytesUnchecked(string.substring(offset, offset + segmentSize), charset);
        System.arraycopy(segment, 0, data, position, segment.length);
        offset += segmentSize;
        position += segment.length;
    }

    return data;
}

From source file:Main.java

/**
 * Creates the element mapping for the specified nodes and all their child
 * nodes.//  w ww  . j av  a  2 s  .  c o m
 *
 * @param n1 node 1
 * @param n2 node 2
 * @param mapping the mapping to be filled
 */
private static void createElementMappingForNodes(Node n1, Node n2, Map<Node, Node> mapping) {
    mapping.put(n1, n2);
    NodeList childNodes1 = n1.getChildNodes();
    NodeList childNodes2 = n2.getChildNodes();
    int count = Math.min(childNodes1.getLength(), childNodes2.getLength());
    for (int i = 0; i < count; i++) {
        createElementMappingForNodes(childNodes1.item(i), childNodes2.item(i), mapping);
    }
}

From source file:ColorUtil.java

public static final Color mult(Color c, double amount) {
    return c == null ? null
            : new Color(Math.min(255, (int) (c.getRed() * amount)),
                    Math.min(255, (int) (c.getGreen() * amount)), Math.min(255, (int) (c.getBlue() * amount)),
                    c.getAlpha());//w  ww  . j a v a2  s  . c om
}

From source file:Main.java

/**
 * Darken a color by percent.//from www .j a  va  2s . com
 * 
 * @param color
 * @param percent 0.0 - 1.0
 * @return A new, darker color.
 */
public static int darkenByPercent(int color, float percent) {
    // TODO We may try an HSV approach...
    // float[] hsv = new float[3];
    // Color.colorToHSV(color, hsv);
    // hsv[2] *= percent;
    // return Color.HSVToColor(hsv);
    float r = Color.red(color) * percent;
    float g = Color.green(color) * percent;
    float b = Color.blue(color) * percent;
    int ir = Math.min(255, (int) r);
    int ig = Math.min(255, (int) g);
    int ib = Math.min(255, (int) b);
    int ia = Color.alpha(color);
    return (Color.argb(ia, ir, ig, ib));
}

From source file:Main.java

/**
 * Create circle image.//ww  w  .j  a va  2 s  .  c om
 *
 * @param bitmap      Bitmap to be cropped
 * @param resColor    Resource color
 * @param strokeWidth Thickness of stroke
 * @return Returns the circle image with border
 */
public static Bitmap getCircleImage(Bitmap bitmap, int resColor, int strokeWidth) {
    // create Bitmap to draw
    Bitmap mBitmap = Bitmap.createBitmap(bitmap.getWidth() + 8, bitmap.getHeight() + 8,
            Bitmap.Config.ARGB_8888);

    // create Rect to hold image
    final Rect mRec = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    // create Canvas
    Canvas mCanvas = new Canvas(mBitmap);
    mCanvas.drawARGB(0, 0, 0, 0);

    // create Paint
    final Paint mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setAntiAlias(true);

    // get the half size of the image
    int mHalfWidth = bitmap.getWidth() / 2;
    int mHalfHeight = bitmap.getHeight() / 2;

    // draw circle
    mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint);

    // unknown
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // draw the image
    mCanvas.drawBitmap(bitmap, mRec, mRec, mPaint);

    // set border mode
    mPaint.setXfermode(null);

    // set stroke
    mPaint.setStyle(Paint.Style.STROKE);

    // set stroke color
    mPaint.setColor(resColor);

    // set stroke width
    mPaint.setStrokeWidth(strokeWidth);

    // draw stroke
    mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint);

    // return the circle image
    return mBitmap;
}

From source file:Main.java

public static String formatAmountConversionRate(double convRate) {
    if (convRate == 0)
        return null;
    BigDecimal cr = new BigDecimal(convRate);
    int x = 7 - cr.precision() + cr.scale();
    String bds = cr.movePointRight(cr.scale()).toString();
    if (x > 9)
        bds = zeropad(bds, bds.length() + x - 9);
    String ret = zeropadRight(bds, 7);
    return Math.min(9, x) + takeFirstN(ret, 7);
}