Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

In this page you can find the example usage for java.lang Math max.

Prototype

@HotSpotIntrinsicCandidate
public static double max(double a, double b) 

Source Link

Document

Returns the greater of two double values.

Usage

From source file:Main.java

public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp,
        boolean recycle) {
    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
        /*//from w  w w.j av a  2s . c  om
         * In this case the bitmap is smaller, at least in one dimension,
         * than the target.  Transform it by placing as much of the image
         * as possible into the target and leaving the top/bottom or
         * left/right (or both) black.
         */
        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();
        }
        return b2;
    }
    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    if (bitmapAspect > viewAspect) {
        float scale = targetHeight / bitmapHeightF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    } else {
        float scale = targetWidth / bitmapWidthF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        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) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:Main.java

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    int minSize = Math.max(reqWidth, reqHeight);

    if (height > minSize || width > minSize) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        while ((halfHeight / inSampleSize) > minSize && (halfWidth / inSampleSize) > minSize) {
            inSampleSize *= 2;//from   ww w  .  j a v  a  2  s.c o m
        }
    }

    return inSampleSize;
}

From source file:Main.java

/**
 * Packs all table rows to their preferred height.
 *
 * @param table table to process//  www. ja  va 2  s  .  c  o m
 */
public static void packRowHeights(final JTable table) {
    for (int row = 0; row < table.getRowCount(); row++) {
        int maxHeight = 0;
        for (int column = 0; column < table.getColumnCount(); column++) {
            final TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
            final Object valueAt = table.getValueAt(row, column);
            final Component renderer = cellRenderer.getTableCellRendererComponent(table, valueAt, false, false,
                    row, column);
            final int heightPreferable = renderer != null ? renderer.getPreferredSize().height : 0;
            maxHeight = Math.max(heightPreferable, maxHeight);
        }
        table.setRowHeight(row, maxHeight);
    }
}

From source file:ch.rasc.edsutil.RepositoryUtil.java

public static Pageable createPageable(ExtDirectStoreReadRequest request) {

    List<Order> orders = new ArrayList<>();
    for (SortInfo sortInfo : request.getSorters()) {

        if (sortInfo.getDirection() == SortDirection.ASCENDING) {
            orders.add(new Order(Direction.ASC, sortInfo.getProperty()));
        } else {//from  w  ww. j av  a  2  s  . c o  m
            orders.add(new Order(Direction.DESC, sortInfo.getProperty()));
        }
    }

    // Ext JS pages starts with 1, Spring Data starts with 0
    int page = Math.max(request.getPage() - 1, 0);

    if (orders.isEmpty()) {
        return new PageRequest(page, request.getLimit());
    }

    Sort sort = new Sort(orders);
    return new PageRequest(page, request.getLimit(), sort);

}

From source file:Center.java

License:asdf

public void addNotify() {
    super.addNotify();
    int maxWidth = 0;
    FontMetrics fm = getFontMetrics(getFont());
    for (int i = 0; i < text.length; i++) {
        maxWidth = Math.max(maxWidth, fm.stringWidth(text[i]));
    }/*from   w  ww.ja  v a2 s .  c o  m*/
    Insets inset = getInsets();
    dim = new Dimension(maxWidth + inset.left + inset.right,
            text.length * fm.getHeight() + inset.top + inset.bottom);
    setSize(dim);
}

From source file:arena.utils.FileUtils.java

public static String extractFileWithoutPath(String input) {
    if (input == null) {
        return null;
    } else {/*from   w  w w. j  a  v a 2  s .  c om*/
        int lastSlashPos = Math.max(input.lastIndexOf('/'), input.lastIndexOf('\\'));
        return (lastSlashPos == -1 ? input : input.substring(lastSlashPos + 1));
    }
}

From source file:Main.java

/**
 * Calculate an inSampleSize for use in a
 * {@link BitmapFactory.Options} object when decoding
 * bitmaps using the decode* methods from
 * {@link BitmapFactory}. This implementation calculates
 * the closest inSampleSize that will result in the final decoded bitmap
 * having a width and height equal to or larger than the requested width and
 * height. This implementation does not ensure a power of 2 is returned for
 * inSampleSize which can be faster when decoding but results in a larger
 * bitmap which isn't as useful for caching purposes.
 *
 * @param options   An options object with out* params already populated (run
 *                  through a decode* method with inJustDecodeBounds==true
 * @param reqWidth  The requested width of the resulting bitmap
 * @param reqHeight The requested height of the resulting bitmap
 * @return The value to be used for inSampleSize
 *///from ww  w. j  a  v  a 2 s  .  c  o m
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;

    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        int widthSampleSize = 0;
        int heightSampleSize = 0;
        if (reqWidth < width) {
            widthSampleSize = Math.round((float) width / (float) reqWidth);
        }
        if (reqHeight < height) {
            heightSampleSize = Math.round((float) height / (float) reqHeight);
        }
        inSampleSize = Math.max(widthSampleSize, heightSampleSize);
    }
    return inSampleSize;
}

From source file:Main.java

/**
 * Returns a {@link Collection} containing the union of the given
 * {@link Collection}s./*from  w ww  .  j av  a2 s  .c  om*/
 * <p>
 * The cardinality of each element in the returned {@link Collection} will
 * be equal to the maximum of the cardinality of that element in the two
 * given {@link Collection}s.
 * 
 * @see Collection#addAll
 */
public static Collection union(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new HashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Picks a start position for {@link Cursor#fillWindow} such that the
 * window will contain the requested row and a useful range of rows
 * around it.//w  ww.  ja v  a 2  s  . c om
 *
 * When the data set is too large to fit in a cursor window, seeking the
 * cursor can become a very expensive operation since we have to run the
 * query again when we move outside the bounds of the current window.
 *
 * We try to choose a start position for the cursor window such that
 * 1/3 of the window's capacity is used to hold rows before the requested
 * position and 2/3 of the window's capacity is used to hold rows after the
 * requested position.
 *
 * @param cursorPosition The row index of the row we want to get.
 * @param cursorWindowCapacity The estimated number of rows that can fit in
 * a cursor window, or 0 if unknown.
 * @return The recommended start position, always less than or equal to
 * the requested row.
 * @hide
 */
public static int cursorPickFillWindowStartPosition(int cursorPosition, int cursorWindowCapacity) {
    return Math.max(cursorPosition - cursorWindowCapacity / 3, 0);
}

From source file:ByteArrayList.java

private void ensureCapacity(int cap) {
    if (m_bytes.length < cap) {
        int capacity = Math.max((3 * m_bytes.length) / 2 + 1, cap);
        byte[] nbytes = new byte[capacity];
        System.arraycopy(m_bytes, 0, nbytes, 0, m_size);
        m_bytes = nbytes;// w  ww.j  ava  2  s.  c o  m
    }
}