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

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

Introduction

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

Prototype

public static void checkArgument(boolean expression) 

Source Link

Document

Ensures the truth of an expression involving one or more parameters to the calling method.

Usage

From source file:org.sonar.lua.checks.utils.Function.java

public static String getLocalName(AstNode functionDef) {
    Preconditions.checkArgument(functionDef.is(LuaGrammar.LOCALFUNCSTAT));
    return functionDef.getFirstChild(LuaGrammar.NAME).getTokenValue();

}

From source file:edu.byu.nlp.stats.SymmetricDirichletMLEFPOptimizable.java

public static SymmetricDirichletMLEFPOptimizable newOptimizable(double[][] data) {
    Preconditions.checkNotNull(data);/*from   ww  w .  ja  v  a  2  s  .c om*/
    Preconditions.checkArgument(data.length > 0);

    double[] meanLogTheta = Matrices.sumOverFirst(data);
    DoubleArrays.divideToSelf(meanLogTheta, data.length);
    return new SymmetricDirichletMLEFPOptimizable(meanLogTheta, data.length, data[0].length);
}

From source file:com.youtube.serializer.YoutubeEventClassifier.java

public static Class detectClass(String json) {
    Preconditions.checkNotNull(json);/*from   w  ww. j a v  a 2s.  c  om*/
    Preconditions.checkArgument(StringUtils.isNotEmpty(json));

    ObjectNode objectNode;
    try {
        objectNode = (ObjectNode) mapper.readTree(json);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    if (objectNode.findValue("kind") != null && objectNode.get("kind").toString().equals(VIDEO_IDENTIFIER)) {
        return Video.class;
    } else if (objectNode.findValue("kind") != null
            && objectNode.get("kind").toString().contains(CHANNEL_IDENTIFIER)) {
        return com.google.api.services.youtube.model.Channel.class;
    } else {
        return ObjectNode.class;
    }
}

From source file:pocman.game.Tiles.java

public static Tile fromCharacter(final Character character) {
    Preconditions.checkArgument(character != null);
    final Tile tile = TILE_BY_CHARACTER_MAP.get(character);
    Preconditions.checkState(tile != null, "Unknown tile from character: " + character);
    return tile;/*from w  w  w.  j a v  a  2  s . c  om*/
}

From source file:com.davidbracewell.io.CharsetDetector.java

/**
 * Detects the character set for the buffer.
 *
 * @param buffer The buffer/*from www. j  a v a  2  s . co  m*/
 * @param offset where to start
 * @param length the length to read
 * @return The detected charset or null
 */
public static Charset detect(byte[] buffer, int offset, int length) {
    Preconditions.checkNotNull(buffer);
    Preconditions.checkArgument(length > 0);
    Preconditions.checkArgument(offset >= 0);

    final com.ibm.icu.text.CharsetDetector detector = new com.ibm.icu.text.CharsetDetector();
    try {
        detector.setText(new ByteArrayInputStream(buffer, offset, length));
        return Charset.forName(detector.detect().getName());
    } catch (Exception e) {
        return null;
    }
}

From source file:org.robotframework.ide.eclipse.main.plugin.tableeditor.AddingToken.java

public AddingToken(final Object parent, final TokenState... states) {
    Preconditions.checkArgument(states.length > 0);
    this.parent = parent;
    this.states = states;
    this.current = 0;
}

From source file:co.cask.cdap.template.etl.common.ETLUtils.java

/**
 * Parses a duration String to its long value.
 * Frequency string consists of a number followed by an unit, with 's' for seconds, 'm' for minutes, 'h' for hours
 * and 'd' for days. For example, an input of '5m' means 5 minutes which will be parsed to 300000 milliseconds.
 *
 * @param durationStr the duration string (ex: 5m, 5h etc).
 * @return long which is milliseconds equivalent of the duration string
 */// w w w . ja v a 2  s.c  om
public static long parseDuration(String durationStr) {
    //TODO: replace with TimeMathParser (available only internal to cdap)
    Preconditions.checkArgument(!Strings.isNullOrEmpty(durationStr));
    durationStr = durationStr.toLowerCase();

    String value = durationStr.substring(0, durationStr.length() - 1);
    int parsedValue = 0;
    try {
        parsedValue = Integer.parseInt(value);
    } catch (NumberFormatException nfe) {
        Throwables.propagate(nfe);
    }
    Preconditions.checkArgument(parsedValue >= 0);

    char lastChar = durationStr.charAt(durationStr.length() - 1);
    switch (lastChar) {
    case 's':
        return TimeUnit.SECONDS.toMillis(parsedValue);
    case 'm':
        return TimeUnit.MINUTES.toMillis(parsedValue);
    case 'h':
        return TimeUnit.HOURS.toMillis(parsedValue);
    case 'd':
        return TimeUnit.DAYS.toMillis(parsedValue);
    }
    throw new IllegalArgumentException(String.format("Time unit not supported: %s", lastChar));
}

From source file:org.ros.internal.node.topic.TopicDefinition.java

/**
 * @param header//from w  w  w  .  ja  v a2  s. c  o m
 *          a {@link Map} of header fields
 * @return a new {@link TopicDefinition} from the given header
 */
public static TopicDefinition newFromHeader(Map<String, String> header) {
    Preconditions.checkArgument(header.containsKey(ConnectionHeaderFields.TOPIC));
    GraphName name = new GraphName(header.get(ConnectionHeaderFields.TOPIC));
    return new TopicDefinition(new TopicIdentifier(name), MessageDefinition.newFromHeader(header));
}

From source file:org.ros.internal.node.topic.TopicDeclaration.java

/**
 * @param header/*from w w w . ja  v a 2s  .c om*/
 *          a {@link Map} of header fields
 * @return a new {@link TopicDeclaration} from the given header
 */
public static TopicDeclaration newFromHeader(Map<String, String> header) {
    Preconditions.checkArgument(header.containsKey(ConnectionHeaderFields.TOPIC));
    GraphName name = GraphName.of(header.get(ConnectionHeaderFields.TOPIC));
    String type = header.get(ConnectionHeaderFields.TYPE);
    String definition = header.get(ConnectionHeaderFields.MESSAGE_DEFINITION);
    String md5Checksum = header.get(ConnectionHeaderFields.MD5_CHECKSUM);
    TopicDescription topicDescription = new TopicDescription(type, definition, md5Checksum);
    return new TopicDeclaration(new TopicIdentifier(name), topicDescription);
}

From source file:org.xacml4j.v30.types.StringExp.java

/**
 * Creates {@link StringExp} from given string instance
 *
 * @param v a string value//w w w.  j  av a  2  s . c om
 * @return {@link StringExp}
 * @exception IllegalArgumentException if given
 * string value is null or empty
 */
public static StringExp of(String v) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(v));
    return new StringExp(v);
}