List of usage examples for java.lang Math min
@HotSpotIntrinsicCandidate public static double min(double a, double b)
From source file:Main.java
/** * Returns a {@link Collection} containing the intersection of the given * {@link Collection}s.//from w w w .j a va 2 s . c om * <p> * The cardinality of each element in the returned {@link Collection} will * be equal to the minimum of the cardinality of that element in the two * given {@link Collection}s. * * @param a * the first collection, must not be null * @param b * the second collection, must not be null * @return the intersection of the two collections * @see Collection#retainAll * @see #containsAny */ public static <E> Collection<E> intersection(final Collection<E> a, final Collection<E> b) { if (a == null || b == null) { return null; } List<E> list = getArrayList(); Map<E, Integer> mapa = getCardinalityMap(a); Map<E, Integer> mapb = getCardinalityMap(b); Set<E> elts = getHashSet(a); elts.addAll(b); for (E e : elts) { for (int i = 0, m = Math.min(getFreq(e, mapa), getFreq(e, mapb)); i < m; i++) { list.add(e); } } return list; }
From source file:Main.java
/** * Finds the length of the largest prefix for two character sequences. * * @param a character sequence// w w w . java2 s. c o m * @param b character sequence * @return the length of largest prefix of <code>a</code> and <code>b</code> * @throws IllegalArgumentException if either <code>a</code> or <code>b</code> * is <code>null</code> */ public static int largestPrefixLength(CharSequence a, CharSequence b) { int len = 0; for (int i = 0; i < Math.min(a.length(), b.length()); ++i) { if (a.charAt(i) != b.charAt(i)) break; ++len; } return len; }
From source file:Main.java
/** * Returns the contrast ratio between {@code foreground} and {@code background}. * {@code background} must be opaque.//from w w w.ja va2 s . c o m * <p> * Formula defined * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>. */ public static double calculateContrast(int foreground, int background) { if (Color.alpha(background) != 255) { throw new IllegalArgumentException("background can not be translucent"); } if (Color.alpha(foreground) < 255) { // If the foreground is translucent, composite the foreground over the background foreground = compositeColors(foreground, background); } final double luminance1 = calculateLuminance(foreground) + 0.05; final double luminance2 = calculateLuminance(background) + 0.05; // Now return the lighter luminance divided by the darker luminance return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2); }
From source file:Main.java
/** * @param cacheDirectoryName where the file system tile cache will be located * @param firstLevelSize size of the first level cache, no point cache being smaller * @param tileSize tile size/*from w ww .jav a 2 s . c o m*/ * @return recommended number of files in FileSystemTileCache */ public static int estimateSizeOfFileSystemCache(String cacheDirectoryName, int firstLevelSize, int tileSize) { // assumption on size of files in cache, on the large side as not to eat // up all free space, real average probably 50K compressed final int tileCacheFileSize = 4 * tileSize * tileSize; final int maxCacheFiles = 2000; // arbitrary, probably too high // result cannot be bigger than maxCacheFiles int result = (int) Math.min(maxCacheFiles, getAvailableCacheSlots(cacheDirectoryName, tileCacheFileSize)); if (firstLevelSize > result) { // no point having a file system cache that does not even hold the memory cache result = 0; } return result; }
From source file:Main.java
private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) source.recycle();//from w w w . ja va2s . co m return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; float scale = bitmapAspect > viewAspect ? targetHeight / bitmapHeightF : targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) scaler.setScale(scale, scale); else scaler = null; Bitmap b1; if (scaler != null) b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); else b1 = source; if (recycle && b1 != source) source.recycle(); int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); if (b2 != b1 && (recycle || b1 != source)) b1.recycle(); return b2; }
From source file:Main.java
/** Crops a line to the given x-coordinates. * @param line//from w w w.j a va2 s. c o m * @param startingXcoord * @param stoppingXcoord * @return the line cropped to the specified x-coordinates */ public static Point2D[] cropLineX(Point2D[] line, double startingXcoord, double stoppingXcoord) { // ensure that the line has at least one point if (line.length < 1) { throw new IllegalArgumentException("The line must contain at least one point."); } int maxIdx = line.length - 1; // determine the actual starting and stopping coordinates double startX = Math.max(startingXcoord, line[0].getX()); double stopX = Math.min(stoppingXcoord, line[maxIdx].getX()); // determine the index of each starting and stopping point; // these are the indices of the first vertices inside the cutoff // from the original set of vertices. int startXidx = 0; while (startXidx < maxIdx && line[startXidx].getX() <= startX) { startXidx++; } int stopXidx = maxIdx; while (stopXidx > 0 && line[stopXidx].getX() >= stopX) { stopXidx--; } // determine the new starting and stopping points Point2D firstPt = interpolate(line[startXidx - 1], line[startXidx], startX); Point2D lastPt = interpolate(line[stopXidx], line[stopXidx + 1], stopX); // generate the new line Point2D[] croppedLine = new Point2D[stopXidx - startXidx + 3]; // leaving room for the 2 end points // copy all interior points verbatim from the original line for (int origIdx = startXidx, newIdx = 1; origIdx <= stopXidx; origIdx++, newIdx++) { croppedLine[newIdx] = line[origIdx]; } // add in the new endpoints croppedLine[0] = firstPt; croppedLine[croppedLine.length - 1] = lastPt; return croppedLine; }
From source file:Main.java
public synchronized int read(byte[] bytes, int off, int len) throws IOException { len = Math.min(len, buf.remaining()); buf.get(bytes, off, len);/*from w w w. j a va 2 s . c o m*/ return len; }
From source file:Main.java
public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp, boolean fitInScreen) { if (fitInScreen) { source = scaleTo(scaler, source, targetWidth, targetHeight, true); }/*www .j av a 2 s . c om*/ int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if ((!scaleUp || fitInScreen) && (deltaX < 0 || deltaY < 0)) { Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); source.recycle(); return b2; } Bitmap b1 = scaleTo(scaler, source, targetWidth, targetHeight, false); int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); b1.recycle(); return b2; }
From source file:Main.java
/** * Auto fit the column of a table./*ww w. ja v a 2 s. com*/ * @param table the table for which to auto fit the columns. * @param columnIndex the index of the column to auto fit. * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH). */ public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) { TableModel model = table.getModel(); TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); int rowCount = table.getRowCount(); for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) { TableColumn column = table.getColumnModel().getColumn(i); int headerWidth = headerRenderer .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0) .getPreferredSize().width; int cellWidth = 0; for (int j = 0; j < rowCount; j++) { Component comp = table.getDefaultRenderer(model.getColumnClass(i)) .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i); int preferredWidth = comp.getPreferredSize().width; // Artificial space to look nicer. preferredWidth += 10; cellWidth = Math.max(cellWidth, preferredWidth); } // Artificial space for the sort icon. headerWidth += 20; column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth)); if (columnIndex >= 0) { break; } } }
From source file:Main.java
/** * Returns a {@link Collection} containing the intersection * of the given {@link Collection}s./*from w ww .j a v a 2s . com*/ * <p> * The cardinality of each element in the returned {@link Collection} * will be equal to the minimum of the cardinality of that element * in the two given {@link Collection}s. * * @param a The first collection * @param b The second collection * @see Collection#retainAll * @return The intersection of a and b, never null */ public static <E> Collection<E> intersection(final Collection<E> a, final Collection<E> b) { ArrayList<E> list = new ArrayList<E>(); Map<E, Integer> mapa = getCardinalityMap(a); Map<E, Integer> mapb = getCardinalityMap(b); Set<E> elts = new HashSet<E>(a); elts.addAll(b); for (E obj : elts) { for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) { list.add(obj); } } return list; }