List of usage examples for java.lang Math min
@HotSpotIntrinsicCandidate public static double min(double a, double b)
From source file:Main.java
public static Size getOptimalPreviewSize(Activity currentActivity, List<Size> sizes, double targetRatio) { // Use a very small tolerance because we want an exact match. final double ASPECT_TOLERANCE = 0.001; if (sizes == null) return null; Size optimalSize = null;/*ww w .j av a 2s. c o m*/ double minDiff = Double.MAX_VALUE; // Because of bugs of overlay and layout, we sometimes will try to // layout the viewfinder in the portrait orientation and thus get the // wrong size of mSurfaceView. When we change the preview size, the // new overlay will be created before the old one closed, which causes // an exception. For now, just get the screen size Display display = currentActivity.getWindowManager().getDefaultDisplay(); int targetHeight = Math.min(display.getHeight(), display.getWidth()); if (targetHeight <= 0) { // We don't know the size of SurfaceView, use screen height targetHeight = display.getHeight(); } // Try to find an size match aspect ratio and size for (Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find the one match the aspect ratio. This should not happen. // Ignore the requirement. if (optimalSize == null) { Log.w(TAG, "No preview size match the aspect ratio"); minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; }
From source file:Main.java
public static int firstDiff(byte[] a, byte[] b) { return firstDiff(a, b, 0, Math.min(a.length, b.length)); }
From source file:Main.java
private static String getControlByType(String prefs, String key, Object type, String value) { String htmlControl = ""; String htmlControlVal = ""; key = prefs + "_debugghostseperator_" + key; if (type instanceof Boolean) { htmlControlVal = (value.equalsIgnoreCase("true")) ? "checked=\"checked\"" : ""; htmlControl = "<input id=\"" + key + "\" type=\"checkbox\" value=\"" + key + "\" " + htmlControlVal + " />"; } else if (type instanceof Integer) { htmlControlVal = value;//from w w w .j a v a 2 s . c om htmlControl = "<input class=\"form-control\" type=\"number\" value=\"" + htmlControlVal + "\" id=\"" + key + "\">"; } else if (type instanceof String) { htmlControlVal = (value != null) ? value : "null"; htmlControl = "<input class=\"form-control\" type=\"text\" value=\"" + htmlControlVal + "\" id=\"" + key + "\">"; } else if (type instanceof Float) { htmlControlVal = value; htmlControl = "<input class=\"form-control\" type=\"number\" value=\"" + htmlControlVal + "\" id=\"" + key + "\">"; } else if (type instanceof Long) { htmlControlVal = value; htmlControl = "<input class=\"form-control\" type=\"number\" value=\"" + htmlControlVal + "\" id=\"" + key + "\">"; } else if (type instanceof HashSet) { StringBuilder sb = new StringBuilder(); HashSet<String> valueSet = (HashSet) type; int rowHeight = Math.min(valueSet.size(), 5); sb.append("<textarea class=\"form-control\" id=\"" + key + "\" rows=\"" + rowHeight + "\" style=\"margin-top: 0px; margin-bottom: 0px;\">"); for (String val : valueSet) { sb.append(val); sb.append("\r\n"); } sb.delete(sb.length() - 2, sb.length()); sb.append("</textarea>"); htmlControl = sb.toString(); } else { htmlControl = "[DebugGhost has no control configured for type '" + type.getClass().getSimpleName() + "']"; } htmlControl += "<input id=\"" + key + "_TYPE\" type=\"hidden\" value=\"" + type.getClass().getSimpleName() + "\" />"; return htmlControl; }
From source file:Main.java
/** * Create a bitmap which is wrapped to circle * (similar to what you can see in G+ profile pic.) * * @param bitmap Original Bitmap// w w w.j a v a 2s . c o m * @return Circled bitmap */ public static Bitmap createCircleBitmap(Bitmap bitmap) { final int width = bitmap.getWidth(); final int height = bitmap.getHeight(); BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Paint bitmapPaint = new Paint(); bitmapPaint.setAntiAlias(true); bitmapPaint.setShader(bitmapShader); Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint); return output; }
From source file:StreamUtils.java
/** * Copies length bytes from an InputStream to an OutputStream. * /*from w w w.jav a 2s . c o m*/ * @param in * InputStream from wihch the bytes are to copied. * @param out * OutputStream in which the bytes are copied. * @param length * The number of bytes to copy * @return Number of bytes which are copied. * @throws IOException * If the streams are unavailable. */ public static int streamCopy(InputStream in, OutputStream out, int length) throws IOException { byte[] buffer = new byte[BUFFERSIZE]; int read; int copied = 0; while ((read = in.read(buffer, 0, Math.min(BUFFERSIZE, length - copied))) > 0) { out.write(buffer, 0, read); copied += read; } return copied; }
From source file:de.mpg.imeji.logic.search.util.CollectionUtils.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static Collection intersection(final Collection a, final Collection b) { ArrayList list = new ArrayList(); Map mapa = getCardinalityMap(a); Map mapb = getCardinalityMap(b); Set elts = new LinkedHashSet(a); elts.addAll(b);//from w w w . ja va2 s . c o m Iterator it = elts.iterator(); while (it.hasNext()) { Object obj = it.next(); for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) { list.add(obj); } } return list; }
From source file:Main.java
/** * Author: Chas Emerick (source: http://mrfoo.de/archiv/1176-Levenshtein-Distance-in-Java.html) * * This method uses the LevenstheinDistance algorithm to compute the similarity of two strings. * * @return the minimum number of single-character edits required to change one of the given * strings into the other/*from w ww . ja v a2 s .com*/ */ public static int getLevenshteinDistance(String s, String t) { if (s == null || t == null) { throw new IllegalArgumentException("Strings must not be null"); } if (TextUtils.isEmpty(s)) { return t.length(); } else if (TextUtils.isEmpty(t)) { return s.length(); } int n = s.length(); // length of s 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:net.padlocksoftware.padlock.keymaker.Main.java
private static String[] formatOutput(String publicKey) { List<String> list = new ArrayList<String>(); // Break the public key String into 60 character strings int LENGTH = 60; int start = -LENGTH; int end = 0;// ww w. j a va 2s . com while (end < publicKey.length()) { end = Math.min(end + LENGTH, publicKey.length()); start += LENGTH; list.add(publicKey.substring(start, end)); } return list.toArray(new String[0]); }
From source file:Main.java
/** * Return the same image with a shadow, scaled by the specified amount.. * * @param bitmap The bitmap to decor with a shadow * @param width The target width of the decored bitmap * @param height The target height of the decored bitmap * * @return A new Bitmap based on the original bitmap *//*from w w w . j a v a 2 s. co m*/ public static Bitmap createShadow(Bitmap bitmap, int width, int height) { if (bitmap == null) return null; final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight); final int scaledWidth = (int) (bitmapWidth * scale); final int scaledHeight = (int) (bitmapHeight * scale); return createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, false, SHADOW_PAINT); }
From source file:com.not2excel.api.region.Cuboid.java
public Cuboid(int id, String name, Location l1, Location l2) throws CuboidException { if (!l1.getWorld().equals(l2.getWorld())) { throw new CuboidException("World's do not match. Failed to create Cuboid."); }//from w w w. j av a2s.com this.id = id; this.name = name; this.world = l1.getWorld(); minX = Math.min(l1.getBlockX(), l2.getBlockX()); minY = Math.min(l1.getBlockY(), l2.getBlockY()); minZ = Math.min(l1.getBlockZ(), l2.getBlockZ()); maxX = Math.max(l1.getBlockX(), l2.getBlockX()); maxY = Math.max(l1.getBlockY(), l2.getBlockY()); maxZ = Math.max(l1.getBlockZ(), l2.getBlockZ()); }