Example usage for com.google.common.base Preconditions checkNotNull

List of usage examples for com.google.common.base Preconditions checkNotNull

Introduction

In this page you can find the example usage for com.google.common.base Preconditions checkNotNull.

Prototype

public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) 

Source Link

Document

Ensures that an object reference passed as a parameter to the calling method is not null.

Usage

From source file:de.cosmocode.palava.core.lifecycle.Lifecycle.java

/**
 * Checks for {@link Initializable}, {@link Disposable} and {@link Startable}.
 * /*from  w  ww  .  j av  a  2s .  c  o m*/
 * @since 2.3
 * @param service the service to check
 * @return true if the given service implements at least one of the three interfaces, false otherwise
 * @throws NullPointerException if service is null
 */
public static boolean hasInterface(Object service) {
    Preconditions.checkNotNull(service, "Service");
    return isInterface(service.getClass());
}

From source file:org.renyan.leveldb.impl.SequenceNumber.java

public static long packSequenceAndValueType(long sequence, ValueType valueType) {
    Preconditions.checkArgument(sequence <= MAX_SEQUENCE_NUMBER,
            "Sequence number is greater than MAX_SEQUENCE_NUMBER");
    Preconditions.checkNotNull(valueType, "valueType is null");

    return (sequence << 8) | valueType.getPersistentId();
}

From source file:com.facebook.stats.cardinality.StaticModelUtil.java

public static double[] weightsToProbabilities(double[] weights, int iterationLimit) {
    Preconditions.checkNotNull(weights, "weights is null");
    Preconditions.checkArgument(weights.length > 0, "weights is empty");

    return weightsToProbabilities(Arrays.copyOf(weights, weights.length), sum(weights), iterationLimit);
}

From source file:org.opendaylight.controller.netconf.it.SSLUtil.java

public static SSLContext initializeSecureContext(final String pass, final InputStream ksKeysFile,
        final InputStream ksTrustFile, final String algorithm)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        UnrecoverableKeyException, KeyManagementException {

    Preconditions.checkNotNull(ksTrustFile, "ksTrustFile cannot be null");
    Preconditions.checkNotNull(ksKeysFile, "ksKeysFile cannot be null");

    final char[] passphrase = pass.toCharArray();

    // First initialize the key and trust material.
    final KeyStore ksKeys = KeyStore.getInstance("JKS");
    ksKeys.load(ksKeysFile, passphrase);
    final KeyStore ksTrust = KeyStore.getInstance("JKS");
    ksTrust.load(ksTrustFile, passphrase);

    // KeyManager's decide which key material to use.
    final KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
    kmf.init(ksKeys, passphrase);/*from  w ww .j a va  2 s.c o m*/

    // TrustManager's decide whether to allow connections.
    final TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
    tmf.init(ksTrust);

    final SSLContext sslContext = SSLContext.getInstance("TLS");

    // Create/initialize the SSLContext with key material
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    return sslContext;
}

From source file:com.google.security.zynamics.binnavi.Gui.GraphWindows.Implementations.CGraphSearcher.java

/**
 * Toggles the state of case sensitive search in a graph.
 *
 * @param graph The graph where the search state is toggled.
 *///from ww w  . j a v  a 2  s.  co m
public static void toggleCaseSensitiveSearch(final ZyGraph graph) {
    Preconditions.checkNotNull(graph, "IE01756: Graph argument can not be null");

    graph.getSettings().getSearchSettings()
            .setSearchCaseSensitive(!graph.getSettings().getSearchSettings().getSearchCaseSensitive());
}

From source file:net.galaxy.weather.MeasurementParser.java

public static MeasurementDto parse(int src, String str) throws IllegalArgumentException {
    Preconditions.checkNotNull(str, "String is null");
    Preconditions.checkArgument(str.length() > 6, "Invalid string: [%s]", str);

    logger.debug("parsing: [{}]", str);
    try {//from ww  w .  ja va  2s. co  m
        String payload = str.substring(0, str.lastIndexOf('^'));
        logger.trace("  payload: `{}`", payload);
        String receivedCrcStr = str.substring(str.lastIndexOf('^') + 1, str.length());
        logger.trace("  CRC16: `{}`", receivedCrcStr);

        int calculatedCrc;
        try {
            calculatedCrc = Digest.crc16(payload.getBytes("ASCII")) & 0xFFFF;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        int receivedCrc = Integer.valueOf(receivedCrcStr, 16);
        if (calculatedCrc != receivedCrc) {
            throw new IllegalArgumentException("Checksum mismatch");
        }

        String[] parts = payload.split("\\|");
        if (parts.length != 5) {
            throw new IllegalArgumentException("Invalid format -- missing parts");
        }

        long timestamp = Long.parseLong(parts[0]) * 1000 + DT_ADD;

        String[] thParts = parts[1].split(":");
        double temp = 0;
        double humid = 0;
        if (thParts.length == 2) {
            temp = Integer.valueOf(thParts[0], 10) / 10.0;
            humid = Integer.valueOf(thParts[1], 10) / 10.0;
            // temperature sensor error
        }

        MathContext mc = new MathContext(2);
        return new MeasurementDto(src, timestamp, temp, humid, Integer.valueOf(parts[4], 10) / 100.0,
                MeasurementDto.BatteryStatus.values()[Integer.valueOf(parts[2], 10)],
                Integer.valueOf(parts[3], 10) / 100.0);
    } catch (Exception ex) {
        throw new IllegalArgumentException(
                String.format("Error parsing string [%s]: %s", str, ex.getMessage()));
    }
}

From source file:com.github.jeluard.guayaba.util.ServiceLoaders.java

/**
 * @param <T>// w  ww.  j a v a  2  s . c  o  m
 * @param type
 * @param classLoader
 * @return first implementation of `type` as reported by {@link ServiceLoader#load(java.lang.Class, java.lang.ClassLoader)}, if any
 */
public static <T> T load(final Class<T> type, final ClassLoader classLoader) {
    Preconditions.checkNotNull(type, "null type");
    Preconditions.checkNotNull(classLoader, "null classLoader");

    final Iterator<T> iterator = ServiceLoader.load(type, classLoader).iterator();

    Preconditions.checkArgument(iterator.hasNext(), "empty result");

    return iterator.next();
}

From source file:com.ebay.logstorm.server.platform.ExecutionPlatformFactory.java

public static ExecutionPlatform getPlatformInstance(Class<? extends ExecutionPlatform> platformClass) {
    Preconditions.checkNotNull(platformClass, "Platform class is null");
    if (platformInstanceCache.containsKey(platformClass)) {
        return platformInstanceCache.get(platformClass);
    } else {/*from  w  w  w  . j  a v a2 s. c  o  m*/
        try {
            ExecutionPlatform instance = platformClass.newInstance();
            platformInstanceCache.put(platformClass, instance);
            return instance;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.facebook.presto.operator.AggregationFunctionDefinition.java

public static AggregationFunctionDefinition aggregation(AggregationFunction function, Input... inputs) {
    Preconditions.checkNotNull(function, "function is null");
    Preconditions.checkNotNull(inputs, "inputs is null");

    return aggregation(function, Arrays.asList(inputs));
}

From source file:com.knewton.mapreduce.util.MREnvironment.java

/**
 * Gets an enum type from a string value.
 *
 * @param strVal/*  w  ww.j  a v a2s. c  o m*/
 *            The string value representation of the environment
 * @return The Hadoop environment.
 */
public static MREnvironment fromValue(String strVal) {
    Preconditions.checkNotNull(strVal, "Parameter cannot be null");
    for (MREnvironment mre : MREnvironment.values()) {
        if (mre.getValue().equalsIgnoreCase(strVal)) {
            return mre;
        }
    }
    throw new IllegalArgumentException(String.format("Cannot determine type from %s", strVal));
}