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 int clamp(int value, int min, int max) {
    return Math.min(Math.max(value, min), max);
}

From source file:Main.java

public static boolean rectOnRect(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
        double y4) {
    if (x1 >= Math.min(x3, x4) && x1 <= Math.max(x3, x4) && y1 >= Math.min(y3, y4) && y1 <= Math.max(y3, y4)
            && x2 >= Math.min(x3, x4) && x2 <= Math.max(x3, x4) && y2 >= Math.min(y3, y4)
            && y2 <= Math.max(y3, y4)) {
        return true;
    } else {/*from   ww w .  j a  v a2 s. com*/
        return false;
    }
}

From source file:Main.java

public static void fastBufferFileCopy(File source, File target) {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    long start = System.currentTimeMillis();
    FileInputStream fis = null;//  w w  w  .  ja v  a 2 s.c om
    FileOutputStream fos = null;
    long size = source.length();
    try {
        fis = new FileInputStream(source);
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream(target);
        bos = new BufferedOutputStream(fos);

        byte[] barr = new byte[Math.min((int) size, 1 << 20)];
        int read = 0;
        while ((read = bis.read(barr)) != -1) {
            bos.write(barr, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(fis);
        close(fos);
        close(bis);
        close(bos);
    }
    long end = System.currentTimeMillis();

    String srcPath = source.getAbsolutePath();
    Log.d("Copied " + srcPath + " to " + target,
            ", took " + (end - start) / 1000 + " ms, total size " + nf.format(size) + " Bytes, speed "
                    + ((end - start > 0) ? nf.format(size / (end - start)) : 0) + "KB/s");
}

From source file:Main.java

public static void cropImage(File original, File output, Rect imageRect, Rect cropRect)
        throws FileNotFoundException, IOException {
    Point dimen = getImageDimensions(original);
    float scaledW = imageRect.right - imageRect.left;
    float scaledH = imageRect.bottom - imageRect.top;
    float scale = Math.min((float) dimen.x / scaledW, (float) dimen.y / scaledH);

    Rect cropRegion = getIntrinsicCropRegion(dimen, cropRect, scale);
    BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(original.getPath(), false);
    Bitmap bitmap = decoder.decodeRegion(cropRegion, null);
    saveBitmap(output, bitmap);//from   w  w w . ja v  a 2  s  .co  m
}

From source file:Main.java

public static byte[] copy(byte[] b, int newSize) {
    byte[] r = new byte[newSize];
    System.arraycopy(b, 0, r, 0, Math.min(b.length, r.length));
    return r;/*from  w w  w .  j  a  v a  2s  .  com*/
}

From source file:Main.java

public static int getMatchingThresholdFromString(String value) throws ParseException {
    char percent = new DecimalFormatSymbols().getPercent();
    value = value.replace(percent, ' ');
    Number number = NumberFormat.getNumberInstance().parse(value);
    double parse = number.doubleValue();
    double p = Math.log10(Math.max(Double.MIN_VALUE, Math.min(1, parse / 100)));
    return Math.max(0, (int) Math.round(-12 * p));
}

From source file:Main.java

/**
 * Calculates the Levenshtein distance between two strings
 *
 * @link Reference & Created by http://rosettacode.org/wiki/Levenshtein_distance#Java
 *///from  w  ww .  ja va2 s. c  om
public static int editDistance(String s1, String s2) {
    s1 = s1.toLowerCase();
    s2 = s2.toLowerCase();

    int[] costs = new int[s2.length() + 1];
    for (int i = 0; i <= s1.length(); i++) {
        int lastValue = i;
        for (int j = 0; j <= s2.length(); j++) {
            if (i == 0) {
                costs[j] = j;
            } else {
                if (j > 0) {
                    int newValue = costs[j - 1];
                    if (s1.charAt(i - 1) != s2.charAt(j - 1)) {
                        newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
                    }
                    costs[j - 1] = lastValue;
                    lastValue = newValue;
                }
            }
        }
        if (i > 0) {
            costs[s2.length()] = lastValue;
        }
    }
    return costs[s2.length()];
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int[] getBitmapSize(File file, int targetWidth, int targetHeight) {
    if (null != file && file.exists() && file.isFile() && file.canRead()) {
        String path = file.getAbsolutePath();
        Options opts = new Options();
        if (targetWidth > 0 && targetHeight > 0) {
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, opts);
            opts.inSampleSize = computeSampleSize(opts, Math.min(targetWidth, targetHeight),
                    targetWidth * targetHeight);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                opts.inInputShareable = true;
                opts.inPurgeable = true;
            }//  www .j ava  2 s.co m
        }
        BitmapFactory.decodeFile(path, opts);
        return new int[] { opts.outWidth, opts.outHeight };
    }
    return null;
}

From source file:Main.java

private static byte[] getBytesFromFile(File file) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
    try {/*from   w w w  .  j  av  a  2 s.  c om*/
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int maxBufferSize = 1024 * 1024;
        int bufferSize = (int) Math.min(file.getTotalSpace(), maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = 0;

        if (fileInputStream != null) {
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        while (bytesRead > 0) {
            dataOutputStream.write(buffer, 0, bufferSize);
            int bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toByteArray();
}

From source file:Main.java

public static Map toMap(Map map, Object[] keys, Object[] values) {
    if (keys == null || values == null) {
        return map;
    }// w w  w.j  av  a  2  s  . c  o m
    for (int i = 0, max = Math.min(keys.length, values.length); i < max; i++) {
        map.put(keys[i], values[i]);
    }

    return map;
}