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:net.dontdrinkandroot.utils.collections.CollectionUtils.java

public static <T extends Number> AggregationResult aggregate(final Collection<T> collection) {

    if (collection.isEmpty()) {
        throw new IllegalArgumentException("Collection must not be empty");
    }//from w  w  w  .j a  v a 2 s . c  o m

    final AggregationResult result = new AggregationResult();
    result.setSize(collection.size());
    double sum = 0;
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;

    /* Determine min, max, sum */
    for (final T entry : collection) {
        if (entry != null) {
            final double value = entry.doubleValue();
            max = Math.max(max, value);
            min = Math.min(min, value);
            sum += value;
        }
    }
    result.setMin(min);
    result.setMax(max);
    result.setSum(sum);

    result.setAvg(sum / result.getSize());

    result.setMean(CollectionUtils.getMean(collection));

    return result;
}

From source file:ClippedDragImage.java

private Rectangle getAffectedArea(int oldx, int oldy, int newx, int newy, int width, int height) {
    int x = Math.min(oldx, newx);
    int y = Math.min(oldy, newy);
    int w = (Math.max(oldx, newx) + width) - x;
    int h = (Math.max(oldy, newy) + height) - y;
    return new Rectangle(x, y, w, h);
}

From source file:Main.java

/**
 * Returns the index of the last directory separator character.
 * <p>//from   w  ww  .j a  va  2 s .c  o m
 * This method will handle a file in either Unix or Windows format.
 * The position of the last forward or backslash is returned.
 * <p>
 * The output will be the same irrespective of the machine that the code is running on.
 *
 * @param filename  the filename to find the last path separator in, null returns -1
 * @return the index of the last separator character, or -1 if there
 * is no such character
 */
public static int indexOfLastSeparator(String filename) {
    if (filename == null) {
        return -1;
    }
    int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
    int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
    return Math.max(lastUnixPos, lastWindowsPos);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1, wait at 1
 * and then fade to 0 and dispose./*from ww w .  ja  v  a 2s  . c  o  m*/
 *
 * @param dialog the dialog to display
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 * @param displayTime the time in ms the dialog is fully visible
 */
public static void fadeInAndOut(final JDialog dialog, final int delay, final float incrementSize,
        final int displayTime) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;
        private boolean displayed = false;

        @Override
        public void actionPerformed(ActionEvent e) {

            if (!displayed) {
                opacity += incrementSize;
                dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
                if (opacity >= 1) {
                    timer.setDelay(displayTime);
                    displayed = true;
                }
            } else {
                timer.setDelay(delay);
                opacity -= incrementSize;
                dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
                if (opacity < 0) {
                    timer.stop();
                    dialog.dispose();
                }
            }
        }
    });

    dialog.setOpacity(0); // requires java 1.7
    timer.start();
    dialog.setVisible(true);
}

From source file:Main.java

/**
 * Moves the supplied <code>JSplitPane</code> divider to the specified <code>proportion</code>.
 * Valid values for <code>proportion</code> range from <code>0.0F<code>
 * to <code>1.0F</code>.  For example, a <code>proportion</code> of <code>0.3F</code> will move the
 * divider to 30% of the "size" (<i>width</i> for horizontal split, <i>height</i> for vertical split) of the
 * split container that contains the specified <code>Dockable</code>.  If a <code>proportion</code> of less
 * than <code>0.0F</code> is supplied, the value </code>0.0F</code> is used.  If a <code>proportion</code>
 * greater than <code>1.0F</code> is supplied, the value </code>1.0F</code> is used.
 * <br/>//from  w w  w  . j  a v a 2  s  .  c o  m
 * This method should be effective regardless of whether the split layout in question has been fully realized
 * and is currently visible on the screen.  This should alleviate common problems associated with setting
 * percentages of unrealized <code>Component</code> dimensions, which are initially <code>0x0</code> before
 * the <code>Component</code> has been rendered to the screen.
 * <br/>
 * If the specified <code>JSplitPane</code> is <code>null</code>, then this method returns with no action
 * taken.
 *
 * @param split the <code>JSplitPane</code> whose divider location is to be set.
 * @param proportion a double-precision floating point value that specifies a percentage,
 * from zero (top/left) to 1.0 (bottom/right)
 * @see #getSplitPaneSize(JSplitPane)
 * @see JSplitPane#setDividerLocation(double)
 */
public static void setSplitDivider(final JSplitPane split, float proportion) {
    if (split == null)
        return;

    proportion = Math.max(0f, proportion);
    final float percent = Math.min(1f, proportion);
    int size = getSplitPaneSize(split);

    if (split.isVisible() && size > 0 && EventQueue.isDispatchThread()) {
        split.setDividerLocation(proportion);
        split.validate();
        return;
    }

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            setSplitDivider(split, percent);
        }
    });
}

From source file:org.jfree.chart.demo.Animator.java

@SuppressWarnings("rawtypes")
public void actionPerformed(ActionEvent actionevent) {
    int i = (int) (Math.random() * (double) dataset.getRowCount());
    Comparable comparable = dataset.getRowKey(i);
    int j = (int) (Math.random() * (double) dataset.getColumnCount());
    Comparable comparable1 = dataset.getColumnKey(j);
    int k = Math.random() - 0.5D >= 0.0D ? 5 : -5;
    dataset.setValue(Math.max(0.0D, dataset.getValue(i, j).doubleValue() + (double) k), comparable,
            comparable1);//from  w w  w  . j a v a 2 s  . co  m
}

From source file:Main.java

public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(i);
        int width = 0;

        TableCellRenderer renderer = col.getHeaderRenderer();
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, i);
            Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false,
                    r, i);//w w  w.j a  v  a  2s . com
            width = Math.max(width, comp.getPreferredSize().width);
        }
        col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:Range.java

/**
 * @param r/*ww w. ja va  2  s .  c o  m*/
 * @param dest
 * @return <code>true</code> if the intersection exists
 */
public boolean intersection(Range r, Range dest) {
    if (intersects(r)) {
        dest.set(Math.max(min, r.min), Math.min(max, r.max));
        return true;
    }

    return false;
}

From source file:Main.java

/**
 * Get the child element having the supplied localname, position
 * and namespace.//from w ww . j  av  a  2 s .com
 * Can be used instead of XPath.
 * @param parent Parent element to be searched.
 * @param localname Localname of the element required.
 * @param position The position of the element relative to other sibling
 *                  elements having the same name (and namespace if specified) e.g. if
 *                  searching for the 2nd &lt;input&gt; element, this param needs to have a value of 2.
 * @param namespaceURI Namespace URI of the required element, or null
 * if a namespace comparison is not to be performed.
 * @return The element at the requested position, or null if no such child
 * element exists on the parent element.
 */
public static Element getElement(Element parent, String localname, int position, String namespaceURI) {
    List<Element> elements = getElements(parent, localname, namespaceURI);
    position = Math.max(position, 1);
    if (position > elements.size()) {
        return null;
    }
    return elements.get(position - 1);
}

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.");
    }//w  w  w  .  j a va2  s  . 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());
}