List of usage examples for java.lang Math min
@HotSpotIntrinsicCandidate public static double min(double a, double b)
From source file:mase.mason.world.GeomUtils.java
public static Pair<Double2D, Double2D> computeBB(Double2D[] points) { double xMin = Double.POSITIVE_INFINITY; double yMin = Double.POSITIVE_INFINITY; double xMax = Double.NEGATIVE_INFINITY; double yMax = Double.NEGATIVE_INFINITY; for (Double2D p : points) { xMin = Math.min(xMin, p.x); yMin = Math.min(yMin, p.y); xMax = Math.max(xMax, p.x); yMax = Math.max(yMax, p.y); }/*w w w.j a v a2s .co m*/ return Pair.of(new Double2D(xMin, yMin), new Double2D(xMax, yMax)); }
From source file:Main.java
public static BufferedImage int2image(int[][] scene) { int maxValue = -1; int minValue = Integer.MAX_VALUE; for (int y = 0; y < scene.length; y++) { for (int x = 0; x < scene[y].length; x++) { maxValue = Math.max(maxValue, scene[y][x]); minValue = Math.min(minValue, scene[y][x]); }//from w w w .j a v a2 s . com } if (maxValue == minValue) maxValue = minValue + 1; final double scale = 255.0 / (maxValue - minValue); BufferedImage image = new BufferedImage(scene[0].length, scene.length, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < scene.length; y++) { for (int x = 0; x < scene[y].length; x++) { final int c = (int) (scale * (scene[y][x] - minValue)); image.setRGB(x, y, c << 16 | c << 8 | c); } } return image; }
From source file:Main.java
public static byte[] copyOfRange(byte[] original, int from, int to) { int newLength = to - from; if (newLength < 0) { throw new IllegalArgumentException(from + " > " + to); }/*from w ww.ja v a2 s . co m*/ byte[] copy = new byte[newLength]; System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; }
From source file:Main.java
private static boolean isLowSpace(long totalSpace, long usableSpace) { // For caching attachments we want to enable caching if there is // more than 100MB available, or if 25% of total space is free on devices // where the cache partition is < 400MB. return usableSpace < Math.min(totalSpace * MIN_CACHE_THRESHOLD, MIN_CACHE_AVAILABLE_SPACE_BYTES); }
From source file:Main.java
/** * Returns the amount of storage currently available in the cache. * * @param dir The cache directory path name. * @return The amount of storage available in bytes. *///from ww w .j a va2 s . co m public static long calculateDiskCacheSize(File dir) { long size = MIN_DISK_CACHE_SIZE; try { StatFs statFs = new StatFs(dir.getAbsolutePath()); long available = statFs.getBlockCountLong() * statFs.getBlockSizeLong(); // Target 2% of the total space. size = available * MAX_DISK_CACHE_AS_PERCENT / 100; } catch (IllegalArgumentException ignored) { } // Bound inside min/max size for disk cache. return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE); }
From source file:Main.java
public static String createBestMapping(String wordForm, String lemma) { String mapping = wordForm + ">" + lemma; // Find longest start match int minLen = Math.min(wordForm.length(), lemma.length()); if (wordForm.charAt(0) == lemma.charAt(0)) { String wordFormStart = wordForm.substring(0, minLen - 1); String lemmaStart = lemma.substring(0, minLen - 1); String wordFormEnd;/*from ww w. j a v a 2 s. co m*/ String lemmaEnd; while (!wordFormStart.equals(lemmaStart)) { wordFormStart = wordFormStart.substring(0, wordFormStart.length() - 1); lemmaStart = lemmaStart.substring(0, lemmaStart.length() - 1); } wordFormEnd = wordForm.substring(lemmaStart.length()); lemmaEnd = lemma.substring(lemmaStart.length()); mapping = wordFormEnd + ">" + lemmaEnd; } return mapping; }
From source file:Main.java
public static Camera.Size getOptimalPreviewSize(Activity currentActivity, List<Camera.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; Camera.Size optimalSize = null;//from ww w. j a v a2 s . co 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 preview surface. 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. Point point = getDefaultDisplaySize(currentActivity, new Point()); int targetHeight = Math.min(point.x, point.y); // Try to find an size match aspect ratio and size for (Camera.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) { minDiff = Double.MAX_VALUE; for (Camera.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
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels < 0) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength < 0) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; }//ww w. j a v a 2s . c om if (maxNumOfPixels < 0 && minSideLength < 0) { return 1; } else if (minSideLength < 0) { return lowerBound; } else { return upperBound; } }
From source file:Main.java
/** * Gets the encoding of xml byte array./*from w w w . ja v a2 s. c o m*/ * * @param xmlData * the xml data * @return the encoding of xml byte array * @throws Exception * the exception */ public static String getEncodingOfXmlByteArray(byte[] xmlData) throws Exception { String encodingAttributeName = "ENCODING"; try { String first50CharactersInUtf8UpperCase = new String(xmlData, "UTF-8").substring(0, 50).toUpperCase(); if (first50CharactersInUtf8UpperCase.contains(encodingAttributeName)) { int encodingstart = first50CharactersInUtf8UpperCase.indexOf(encodingAttributeName) + encodingAttributeName.length(); int contentStartEinfach = first50CharactersInUtf8UpperCase.indexOf("'", encodingstart); int contentStartDoppelt = first50CharactersInUtf8UpperCase.indexOf("\"", encodingstart); int contentStart = Math.min(contentStartEinfach, contentStartDoppelt); if (contentStartEinfach < 0) { contentStart = contentStartDoppelt; } if (contentStart < 0) { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } contentStart = contentStart + 1; int contentEndSingle = first50CharactersInUtf8UpperCase.indexOf("'", contentStart); int contentEndDouble = first50CharactersInUtf8UpperCase.indexOf("\"", contentStart); int contentEnd = Math.min(contentEndSingle, contentEndDouble); if (contentEndSingle < 0) { contentEnd = contentEndDouble; } if (contentEnd < 0) { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } String encodingString = first50CharactersInUtf8UpperCase.substring(contentStart, contentEnd); return encodingString; } else { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } } catch (UnsupportedEncodingException e) { throw new Exception("XmlByteArray-Encoding nicht ermittelbar"); } }
From source file:RectUtils.java
/** * Unions the pair of source <code>Rectangle2D</code> objects and puts the * result into the returned <code>Rectangle2D</code> object. This method * extends the Rectangle2D version by checking for null parameters, the * returned value will also be <code>null</code> if the two input * rectangles are <code>null</code> * /* ww w .j a v a 2 s .com*/ * @param src1 * the first of a pair of <code>Rectangle2D</code> objects to * be combined with each other * @param src2 * the second of a pair of <code>Rectangle2D</code> objects to * be combined with each other * */ public static Rectangle2D union(Rectangle2D src1, Rectangle2D src2) { Rectangle2D result = null; if (src1 == null && src2 == null) { result = null; } else if (src1 != null && src2 != null) { double x1 = Math.min(src1.getMinX(), src2.getMinX()); double y1 = Math.min(src1.getMinY(), src2.getMinY()); double x2 = Math.max(src1.getMaxX(), src2.getMaxX()); double y2 = Math.max(src1.getMaxY(), src2.getMaxY()); result = new Rectangle2D.Double(); result.setFrameFromDiagonal(x1, y1, x2, y2); } else if (src1 != null) { double x1 = src1.getMinX(); double y1 = src1.getMinY(); double x2 = src1.getMaxX(); double y2 = src1.getMaxY(); result = new Rectangle2D.Double(); result.setFrameFromDiagonal(x1, y1, x2, y2); } else { // only src2 is non-null double x1 = src2.getMinX(); double y1 = src2.getMinY(); double x2 = src2.getMaxX(); double y2 = src2.getMaxY(); result = new Rectangle2D.Double(); result.setFrameFromDiagonal(x1, y1, x2, y2); } return result; }