List of usage examples for com.google.common.base Preconditions checkArgument
public static void checkArgument(boolean expression, @Nullable Object errorMessage)
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.AsNumberCaseParser.java
public static AsNumberCase parseSubobject(final ByteBuf buffer) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (buffer.readableBytes() != CONTENT_LENGTH) { throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + CONTENT_LENGTH + "."); }//from w ww . j a v a 2 s . c o m return new AsNumberCaseBuilder() .setAsNumber( new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()) .build(); }
From source file:com.google.security.zynamics.zylib.yfileswrap.gui.zygraph.ZyGraphVerifier.java
/** * Verifies the map that maps between ynode objects and node objects. If the map has an incorrect * format, the function throws an {@link IllegalArgumentException}. * /*from w w w. j a v a 2 s. c om*/ * @param graph */ public static <NodeType> void verifyMap(final Graph2D graph, final HashMap<Node, NodeType> nodeMap) { // Let's verify the node map. // // - The number of mappings must equal or less than the number of graphs in the node (each node // must be mapped, but some nodes can be hidden in folder nodes) // - No element of the key set/value set of the mapping must appear more than once // - No key or value of the mapping must be null Preconditions.checkArgument(graph.nodeCount() <= nodeMap.size(), "Error: Invalid node map (Graph contains " + graph.nodeCount() + " nodes while nodeMap contains " + nodeMap.size() + " nodes"); final HashSet<Node> visitedNodes = new HashSet<Node>(); final HashSet<NodeType> visitedNodes2 = new HashSet<NodeType>(); for (final Map.Entry<Node, NodeType> elem : nodeMap.entrySet()) { final Node ynode = elem.getKey(); final NodeType node = elem.getValue(); Preconditions.checkArgument((ynode != null) && (node != null), "Error: Invalid node map"); // We can not check this because of nodes hidden in folder nodes // if (!graph.contains(ynode)) Preconditions.checkArgument(!visitedNodes.contains(ynode), "Error: Invalid node map"); Preconditions.checkArgument(!visitedNodes2.contains(node), "Error: Invalid node map"); visitedNodes.add(ynode); visitedNodes2.add(node); } }
From source file:org.apache.james.util.streams.Offset.java
public static Offset from(int offset) { Preconditions.checkArgument(offset >= 0, "offset should be positive"); return new Offset(offset); }
From source file:io.spikex.core.util.XXHash32.java
public static String hashAsHex(final String utf8Str, final int salt) { Preconditions.checkArgument((utf8Str != null && utf8Str.length() > 0), "utf8Str is null or empty"); byte[] data = utf8Str.getBytes(StandardCharsets.UTF_8); int hash = FACTORY.hash32().hash(data, 0, data.length, salt); byte[] hashBytes = ByteBuffer.allocate(4).putInt(hash).order(BIG_ENDIAN).array(); return DatatypeConverter.printHexBinary(hashBytes).toLowerCase(); }
From source file:com.turn.splicer.tsdbutils.JSON.java
public static <T> T parseToObject(final String json, final Class<T> pojo) { Preconditions.checkNotNull(json);/*from w ww.ja v a 2 s . c om*/ Preconditions.checkArgument(!json.isEmpty(), "Incoming data was null or empty"); Preconditions.checkNotNull(pojo); try { return jsonMapper.readValue(json, pojo); } catch (IOException e) { throw new JSONException(e); } }
From source file:com.comcast.viper.flume2storm.zookeeper.ZkUtilies.java
/** * Builds a valid (guaranteed) ZNode path made of the components passed in * parameter. This method handles the path separator between component, so it * can be called with or without them.//from w ww . java 2 s . co m * * @param components * A bunch of ZNode path elements. Some may be null. * @return The concatenated path of all the elements * @throws IllegalArgumentException * if the path is invalid (empty for example) */ public static String buildZkPath(final String... components) { Preconditions.checkArgument(components != null, "No path element specified"); boolean isFirst = true; final StringBuilder result = new StringBuilder(); for (int i = 0; i < components.length; i++) { if (StringUtils.isEmpty(components[i])) { continue; } assert components[i] != null; // Checking path separator if (isFirst) { // First element must start with / if (!components[i].startsWith(SEPARATOR)) { result.append(SEPARATOR); } result.append(components[i]); } else { if (!SEPARATOR_CHAR.equals(result.charAt(result.length() - 1)) && !components[i].startsWith(SEPARATOR)) { result.append(SEPARATOR); result.append(components[i]); } else if (SEPARATOR_CHAR.equals(result.charAt(result.length() - 1)) && components[i].startsWith(SEPARATOR)) { result.append(components[i].substring(1)); } else { result.append(components[i]); } } isFirst = false; } final String path = result.toString(); PathUtils.validatePath(path); return path; }
From source file:com.linkedin.pinot.core.data.function.FunctionRegistry.java
public static void registerStaticFunction(Method method) { Preconditions.checkArgument(Modifier.isStatic(method.getModifiers()), "Method needs to be static:" + method); List<FunctionInfo> list = new ArrayList<>(); FunctionInfo functionInfo = new FunctionInfo(method, method.getDeclaringClass()); list.add(functionInfo);//from www . j a v a2 s. c o m _functionInfoMap.put(method.getName().toLowerCase(), list); }
From source file:org.zanata.client.commands.QualifiedSrcDocName.java
public static QualifiedSrcDocName from(String qualifiedName) { String extension = FilenameUtils.getExtension(qualifiedName); Preconditions.checkArgument(!Strings.isNullOrEmpty(extension), "expect a qualified document name (with extension)"); return new QualifiedSrcDocName(qualifiedName); }
From source file:io.fabric8.jube.process.support.FileUtils.java
public static void extractArchive(File archiveFile, File targetDirectory, String extractCommand, Duration timeLimit, Executor executor) throws CommandFailedException { Preconditions.checkNotNull(archiveFile, "archiveFile is null"); Preconditions.checkNotNull(targetDirectory, "targetDirectory is null"); Preconditions.checkArgument(targetDirectory.isDirectory(), "targetDirectory is not a directory: " + targetDirectory.getAbsolutePath()); final String[] commands = splitCommands(extractCommand); final String[] args = Arrays.copyOf(commands, commands.length + 1); args[args.length - 1] = archiveFile.getAbsolutePath(); LOG.info("Extracting archive with commands: " + Arrays.asList(args)); new Command(args).setDirectory(targetDirectory).setTimeLimit(timeLimit).execute(executor); }
From source file:org.haiku.haikudepotserver.dataobjects.Repository.java
public static Repository get(ObjectContext context, ObjectId objectId) { Preconditions.checkArgument(null != context, "the context must be supplied"); Preconditions.checkArgument(null != objectId, "the objectId must be supplied"); Preconditions.checkArgument(objectId.getEntityName().equals(Repository.class.getSimpleName()), "the objectId must be targetting Repository"); return SelectById.query(Repository.class, objectId).selectOne(context); }