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, @Nullable String errorMessageTemplate,
        @Nullable Object... errorMessageArgs) 

Source Link

Document

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

Usage

From source file:com.google.walkaround.slob.server.ChangeDataSerializer.java

public static JSONObject dataToClientJson(ChangeData<String> data, long resultingRevision) {
    Preconditions.checkArgument(resultingRevision <= MAX_DOUBLE_INTEGER, "Resulting revision %s is too large",
            resultingRevision);/*from w  w w.  jav a2s.c om*/

    // Assume payload is JSON, and parse it to avoid nested json.
    // TODO(danilatos): Consider using ChangeData<JSONObject> instead.
    // The reason I haven't done it yet is because it's not immutable,
    // and also for reasons described in ChangeData.
    JSONObject payloadJson;
    try {
        payloadJson = new JSONObject(data.getPayload());
    } catch (JSONException e) {
        throw new IllegalArgumentException("Invalid payload for " + data, e);
    }

    JSONObject json = new JSONObject();
    try {
        Preconditions.checkArgument(resultingRevision >= 0, "invalid rev %s", resultingRevision);
        json.put("revision", resultingRevision);
        long sanityCheck = json.getLong("revision");
        if (sanityCheck != resultingRevision) {
            throw new AssertionError("resultingRevision " + resultingRevision
                    + " not losslessly represented in JSON, got back " + sanityCheck);
        }
        json.put("sid", data.getClientId().getId());
        json.put("op", payloadJson);
        return json;
    } catch (JSONException e) {
        throw new Error(e);
    }
}

From source file:org.janusgraph.core.util.JanusGraphId.java

/**
 * Converts a user provided long id into a JanusGraph vertex id. The id must be positive and can be at most 61 bits long.
 * This method is useful when providing ids during vertex creation via {@link com.tinkerpop.gremlin.structure.Graph#addVertex(Object)}.
 *
 * @param id long id//from w  w  w . j  a  v a  2 s.  co m
 * @return a corresponding JanusGraph vertex id
 */
public static final long toVertexId(long id) {
    Preconditions.checkArgument(id > 0, "Vertex id must be positive: %s", id);
    Preconditions.checkArgument(IDManager.VertexIDType.NormalVertex.removePadding(Long.MAX_VALUE) >= id,
            "Vertex id is too large: %s", id);
    return IDManager.VertexIDType.NormalVertex.addPadding(id);
}

From source file:com.janeluo.jfinalplus.kit.ModelKit.java

@SuppressWarnings("rawtypes")
public static Model set(Model model, Object... attrsAndValues) {
    int length = attrsAndValues.length;
    Preconditions.checkArgument(length % 2 == 0, "attrsAndValues length must be even number", length);
    for (int i = 0; i < length; i = i + 2) {
        Object attr = attrsAndValues[i];
        Preconditions.checkArgument(attr instanceof String, "the odd number of attrsAndValues  must be String");
        model.set((String) attr, attrsAndValues[i + 1]);
    }//  w  ww. j  av  a2  s.c om
    return model;
}

From source file:com.cisco.oss.foundation.directory.utils.DirectoryUtils.java

/**
 * Delete the files in directory./*from w  w  w.  j  a  v  a 2s .co m*/
 *
 * @param directory
 * @throws IOException
 */
public static void deleteDirectoryContents(File directory) throws IOException {
    Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory);
    File[] files = directory.listFiles();
    if (files == null) {
        throw new IOException("Error listing files for " + directory);
    }
    for (File file : files) {
        deleteRecursively(file);
    }
}

From source file:org.locationtech.geogig.osm.internal.log.OSMLogEntry.java

public static OSMLogEntry valueOf(String s) {
    String[] tokens = s.split("\t");
    Preconditions.checkArgument(tokens.length == 3, "wrong OSM log entry definition: %s", s);
    ObjectId id = ObjectId.valueOf(tokens[0]);
    try {/* w ww  . ja  v  a 2  s.c  o m*/
        long changeset = Long.parseLong(tokens[1]);
        long timestamp = Long.parseLong(tokens[2]);
        return new OSMLogEntry(id, changeset, timestamp);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("wrong OSM log entry definition: " + s);
    }
}

From source file:org.opendaylight.yangtools.sal.java.api.generator.TypeUtils.java

/**
 * Given a {@link Type} object lookup the base Java type which sits at the top
 * of its type hierarchy.//www. j av  a  2  s  . c  o  m
 *
 * @param type Input Type object
 * @return Resolved {@link ConcreteType} instance.
 */
static ConcreteType getBaseYangType(@Nonnull final Type type) {
    // Already the correct type
    if (type instanceof ConcreteType) {
        return (ConcreteType) type;
    }

    Preconditions.checkArgument(type instanceof GeneratedTransferObject, "Unsupported type %s", type);

    // Need to walk up the GTO chain to the root
    GeneratedTransferObject rootGto = (GeneratedTransferObject) type;
    while (rootGto.getSuperType() != null) {
        rootGto = rootGto.getSuperType();
    }

    // Look for the 'value' property and return its type
    for (GeneratedProperty s : rootGto.getProperties()) {
        if (VALUE_PROP.equals(s.getName())) {
            return (ConcreteType) s.getReturnType();
        }
    }

    // Should never happen
    throw new IllegalArgumentException(String.format("Type %s root %s properties %s do not include \"%s\"",
            type, rootGto, rootGto.getProperties(), VALUE_PROP));
}

From source file:com.google.copybara.git.GitAuthorParser.java

/**
 * Parses a Git author {@code string} into an {@link Author}.
 *///from  ww  w . j ava  2s  .  c o m
static Author parse(String author) {
    Preconditions.checkNotNull(author);
    Matcher matcher = AUTHOR_PATTERN.matcher(author);
    Preconditions.checkArgument(matcher.matches(), "Invalid author '%s'. Must be in the form of 'Name <email>'",
            author);
    return new Author(matcher.group(1).trim(), matcher.group(2).trim());
}

From source file:co.cask.common.DirUtils.java

/**
 * Wipes out all the a directory starting from a given directory.
 * @param directory to be cleaned//w w w.j a  v  a2  s  .  com
 * @throws java.io.IOException
 */
public static void deleteDirectoryContents(File directory) throws IOException {
    Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory);
    Deque<File> stack = Queues.newArrayDeque();
    stack.add(directory);

    while (!stack.isEmpty()) {
        File file = stack.peekLast();
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            file.delete();
            stack.pollLast();
        } else {
            Collections.addAll(stack, files);
        }
    }
}

From source file:com.google.code.jgntp.internal.util.Hex.java

public static byte[] fromHexadecimal(String s) {
    if (s == null) {
        return null;
    }//from   www .j a va 2s.c  o m
    Preconditions.checkArgument(s.length() % 2 == 0, "Invalid hex string [%s]", s);
    byte[] result = new byte[s.length() / 2];
    for (int i = 0; i < s.length(); i = i + 2) {
        int first = Character.digit(s.charAt(i), 16);
        int second = Character.digit(s.charAt(i + 1), 16);
        result[i / 2] = (byte) (0x0 + ((first & 0xff) << 4) + (second & 0xff));
    }
    return result;
}

From source file:org.geogit.api.plumbing.merge.Conflict.java

public static Conflict valueOf(String s) {
    String[] tokens = s.split("\t");
    Preconditions.checkArgument(tokens.length == 4, "wrong conflict definitions: %s", s);
    String path = tokens[0];/*from   w w  w.j a  v  a  2 s .c  o m*/
    ObjectId ancestor = ObjectId.valueOf(tokens[1]);
    ObjectId ours = ObjectId.valueOf(tokens[2]);
    ObjectId theirs = ObjectId.valueOf(tokens[3]);
    return new Conflict(path, ancestor, ours, theirs);
}