Example usage for org.apache.commons.lang Validate isTrue

List of usage examples for org.apache.commons.lang Validate isTrue

Introduction

In this page you can find the example usage for org.apache.commons.lang Validate isTrue.

Prototype

public static void isTrue(boolean expression, String message, double value) 

Source Link

Document

Validate an argument, throwing IllegalArgumentException if the test result is false.

This is used when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.

 Validate.isTrue( d > 0.0, "The value must be greater than zero: ", d); 

For performance reasons, the double value is passed as a separate parameter and appended to the message string only in the case of an error.

Usage

From source file:com.opengamma.analytics.math.statistics.descriptive.robust.TrimmedMeanCalculator.java

public TrimmedMeanCalculator(final double gamma) {
    Validate.isTrue(gamma >= 0 && gamma <= 1, "Gamma must be between 0 and 1, have {}", gamma);
    _gamma = gamma > 0.5 ? 1 - gamma : gamma;
}

From source file:com.opengamma.analytics.math.statistics.descriptive.robust.WinsorizedMeanCalculator.java

public WinsorizedMeanCalculator(final double gamma) {
    Validate.isTrue(gamma > 0 && gamma < 1, "Gamma must be between 0 and 1, have {}", gamma);
    _gamma = gamma > 0.5 ? 1 - gamma : gamma;
}

From source file:com.beoui.geocell.model.Point.java

public Point(double lat, double lon) {
    Validate.isTrue(!(lat > 90.0 || lat < -90.0), "Latitude must be in [-90, 90]  but was ", lat);
    Validate.isTrue(!(lon > 180.0 || lon < -180.0), "Longitude must be in [-180, 180] but was ", lon);
    this.lat = lat;
    this.lon = lon;
}

From source file:com.quartercode.disconnected.util.LocationGenerator.java

/**
 * Generates the given amount of locations on an earth map, ignoring the given ignore locations.
 * //from  w w  w . j a  va 2s  .c o m
 * @param amount The amount of locations to generate.
 * @return The generated locations.
 * @throws RuntimeException The map image can't be read.
 */
public static List<Location> generateLocations(int amount, List<Location> ignore) {

    Validate.isTrue(amount > 0, "Generation amount must be > 0: ", amount);

    if (map == null) {
        try {
            map = ImageIO.read(LocationGenerator.class.getResource("/data/map.png"));
        } catch (IOException e) {
            throw new RuntimeException("Can't read map data image", e);
        }
    }

    if (ignore == null) {
        ignore = new ArrayList<Location>();
    }

    int width = map.getWidth();
    int height = map.getHeight();

    List<Location> result = new ArrayList<Location>();
    RandomPool random = new RandomPool(100);
    int blackRGB = Color.BLACK.getRGB();
    while (result.size() < amount) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        if (map.getRGB(x, y) == blackRGB) {
            Location location = new Location((float) x / (float) width, (float) y / (float) height);
            if (!ignore.contains(location) && !result.contains(location)) {
                result.add(location);
            }
        }
    }

    return result;
}

From source file:apm.common.security.Digests.java

/**
 * ??Byte[]salt.//from  w w w .  j av a 2  s .c  om
 * 
 * @param numBytes byte?
 */
public static byte[] generateSalt(int numBytes) {
    Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);

    byte[] bytes = new byte[numBytes];
    random.nextBytes(bytes);
    return bytes;
}

From source file:ch.admin.suis.msghandler.util.ZipUtils.java

/**
 * Decompress the given file to the specified directory.
 *
 * @param zipFile the ZIP file to decompress
 * @param toDir   the directory where the files from the archive must be placed; the
 *                file will be replaced if it already exists
 * @return a list of files that were extracted into the destination directory
 * @throws IllegalArgumentException if the provided file does not exist or the specified destination
 *                                  is not a directory
 * @throws IOException              if an IO error has occured (probably, a corrupted ZIP file?)
 *///from  w w  w.  ja va 2s.  com
public static List<File> decompress(File zipFile, File toDir) throws IOException {
    Validate.isTrue(zipFile.exists(), "ZIP file does not exist", zipFile.getAbsolutePath());
    Validate.isTrue(toDir.isDirectory(), toDir.getAbsolutePath() + " is not a directory");

    final ArrayList<File> files = new ArrayList<>();

    try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)))) {

        // read the entries
        ZipEntry entry;
        while (null != (entry = zis.getNextEntry())) {
            if (entry.isDirectory()) {
                LOG.error(MessageFormat.format(
                        "cannot extract the entry {0} from the {1}. because it is a directory", entry.getName(),
                        zipFile.getAbsolutePath()));
                continue;
            }

            // extract the file to the provided destination
            // we have to watch out for a unique name of the file to be extracted:
            // it can happen, that several at the same time incoming messages have a file with the same name
            File extracted = new File(FileUtils.getFilename(toDir, entry.getName()));
            if (!extracted.getParentFile().mkdirs()) {
                LOG.debug("cannot make all the necessary directories for the file "
                        + extracted.getAbsolutePath() + " or " + "the path is already created ");
            }

            try (BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(extracted),
                    BUFFER_SIZE)) {
                byte[] data = new byte[BUFFER_SIZE];
                int count;
                while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }

                files.add(extracted);
            }
        }

    }

    return files;
}

From source file:com.quartercode.disconnected.sim.Location.java

/**
 * Sets the relative x coordinate.//from w  ww .ja  va  2  s . c  o  m
 * 
 * @param x The new relative x coordinate.
 */
public void setX(float x) {

    Validate.isTrue(x >= 0 && x <= 1, "X must be in range 0 <= x <= 1: ", x);
    this.x = Math.round(x * 100) / 100F;
}

From source file:com.quartercode.disconnected.sim.comp.net.Address.java

/**
 * Creates a new address and sets the target ip and the target port.
 * The port must be in range 0 <= port <= 65535.
 * /*from w  w w.j  ava 2  s .  c om*/
 * @param ip The target ip which represents the network interface which holds the service.
 * @param port The target port which specifies the service.
 */
public Address(IP ip, int port) {

    Validate.isTrue(port >= 0 || port <= 65535, "The port must be in range 0 <= port <= 65535 (e.g. 8080): ",
            port);

    this.ip = ip;
    this.port = port;
}

From source file:com.quartercode.disconnected.sim.Location.java

/**
 * Sets the relative y coordinate./*from   ww w.  ja v a 2  s .c o m*/
 * 
 * @param y The new relative y coordinate.
 */
public void setY(float y) {

    Validate.isTrue(y >= 0 && y <= 1, "Y must be in range 0 <= x <= 1: ", y);
    this.y = Math.round(y * 100) / 100F;
}