org.apache.cassandra.cql3.statements.RequestValidations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-all Show documentation
Show all versions of cassandra-all Show documentation
The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.cql3.statements;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.utils.ByteBufferUtil;
/**
* Utility methods use to perform request validation.
*
* This class use overloaded methods to allow to specify different numbers of message arguments. While
* this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection
* overhead that is incurred by varargs calls.
*
* Warning about performance
*
* The goal of this class is to improve readability of code, but in some circumstances this may come at a
* significant performance cost. Remember that argument values for message construction must all be computed eagerly,
* and autoboxing may happen as well, even when the check succeeds. If the message arguments are expensive to create
* you should use the customary form:
*
* if (value < 0.0)
* throw RequestValidations.invalidRequest("negative value: %s", toReadableText(value));
*
*
*/
public final class RequestValidations
{
/**
* Checks that the specified expression is {@code true}. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param message the error message
* @throws InvalidRequestException if the specified expression is {@code false}.
*/
public static void checkTrue(boolean expression, String message) throws InvalidRequestException
{
if (!expression)
throw invalidRequest(message);
}
/**
* Checks that the specified expression is true
. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param messageTemplate the template used to build the error message
* @param messageArg the message argument
* @throws InvalidRequestException if the specified expression is {@code false}.
*/
public static void checkTrue(boolean expression,
String messageTemplate,
Object messageArg) throws InvalidRequestException
{
if (!expression)
throw invalidRequest(messageTemplate, messageArg);
}
/**
* Checks that the specified expression is true
. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param messageTemplate the template used to build the error message
* @param arg1 the first message argument
* @param arg2 the second message argument
* @throws InvalidRequestException if the specified expression is {@code false}.
*/
public static void checkTrue(boolean expression,
String messageTemplate,
Object arg1,
Object arg2) throws InvalidRequestException
{
if (!expression)
throw invalidRequest(messageTemplate, arg1, arg2);
}
/**
* Checks that the specified expression is true
. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param messageTemplate the template used to build the error message
* @param arg1 the first message argument
* @param arg2 the second message argument
* @param arg3 the third message argument
* @throws InvalidRequestException if the specified expression is {@code false}.
*/
public static void checkTrue(boolean expression,
String messageTemplate,
Object arg1,
Object arg2,
Object arg3) throws InvalidRequestException
{
if (!expression)
throw invalidRequest(messageTemplate, arg1, arg2, arg3);
}
/**
* Checks that the specified collections is NOT empty
.
* If it is an {@code InvalidRequestException} will be thrown.
*
* @param collection the collection to test
* @param messageTemplate the template used to build the error message
* @param messageArg the message argument
* @return the collection
* @throws InvalidRequestException if the specified collection is empty
.
*/
public static , E> T checkNotEmpty(T collection,
String messageTemplate,
Object messageArg)
throws InvalidRequestException
{
checkTrue(!collection.isEmpty(), messageTemplate, messageArg);
return collection;
}
/**
* Checks that the specified collections is NOT empty
.
* If it is an {@code InvalidRequestException} will be thrown.
*
* @param collection the collection to test
* @param messageTemplate the template used to build the error message
* @param arg1 the first message argument
* @param arg2 the second message argument
* @return the collection
* @throws InvalidRequestException if the specified collection is empty
.
*/
public static , E> T checkNotEmpty(T collection,
String messageTemplate,
Object arg1,
Object arg2)
throws InvalidRequestException
{
checkTrue(!collection.isEmpty(), messageTemplate, arg1, arg2);
return collection;
}
/**
* Checks that the specified list does not contains duplicates.
*
* @param list the list to test
* @param message the error message
* @throws InvalidRequestException if the specified list contains duplicates.
*/
public static void checkContainsNoDuplicates(List> list, String message) throws InvalidRequestException
{
if (new HashSet<>(list).size() != list.size())
throw invalidRequest(message);
}
/**
* Checks that the specified list contains only the specified elements.
*
* @param list the list to test
* @param expectedElements the expected elements
* @param message the error message
* @throws InvalidRequestException if the specified list contains duplicates.
*/
public static void checkContainsOnly(List list,
List expectedElements,
String message) throws InvalidRequestException
{
List copy = new ArrayList<>(list);
copy.removeAll(expectedElements);
if (!copy.isEmpty())
throw invalidRequest(message);
}
/**
* Checks that the specified expression is {@code false}. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param messageTemplate the template used to build the error message
* @param messageArg the message argument
* @throws InvalidRequestException if the specified expression is true
.
*/
public static void checkFalse(boolean expression,
String messageTemplate,
Object messageArg) throws InvalidRequestException
{
checkTrue(!expression, messageTemplate, messageArg);
}
/**
* Checks that the specified expression is {@code false}. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param messageTemplate the template used to build the error message
* @param arg1 the first message argument
* @param arg2 the second message argument
* @throws InvalidRequestException if the specified expression is true
.
*/
public static void checkFalse(boolean expression,
String messageTemplate,
Object arg1,
Object arg2) throws InvalidRequestException
{
checkTrue(!expression, messageTemplate, arg1, arg2);
}
/**
* Checks that the specified expression is {@code false}. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param messageTemplate the template used to build the error message
* @param arg1 the first message argument
* @param arg2 the second message argument
* @param arg3 the third message argument
* @throws InvalidRequestException if the specified expression is true
.
*/
public static void checkFalse(boolean expression,
String messageTemplate,
Object arg1,
Object arg2,
Object arg3) throws InvalidRequestException
{
checkTrue(!expression, messageTemplate, arg1, arg2, arg3);
}
/**
* Checks that the specified expression is {@code false}. If not an {@code InvalidRequestException} will
* be thrown.
*
* @param expression the expression to test
* @param message the error message
* @throws InvalidRequestException if the specified expression is true
.
*/
public static void checkFalse(boolean expression, String message) throws InvalidRequestException
{
checkTrue(!expression, message);
}
/**
* Checks that the specified object is NOT {@code null}.
* If it is an {@code InvalidRequestException} will be thrown.
*
* @param object the object to test
* @param message the error message
* @return the object
* @throws InvalidRequestException if the specified object is {@code null}.
*/
public static T checkNotNull(T object, String message) throws InvalidRequestException
{
checkTrue(object != null, message);
return object;
}
/**
* Checks that the specified object is NOT {@code null}.
* If it is an {@code InvalidRequestException} will be thrown.
*
* @param object the object to test
* @param messageTemplate the template used to build the error message
* @param messageArg the message argument
* @return the object
* @throws InvalidRequestException if the specified object is {@code null}.
*/
public static T checkNotNull(T object, String messageTemplate, Object messageArg) throws InvalidRequestException
{
checkTrue(object != null, messageTemplate, messageArg);
return object;
}
/**
* Checks that the specified object is NOT {@code null}.
* If it is an {@code InvalidRequestException} will be thrown.
*
* @param object the object to test
* @param messageTemplate the template used to build the error message
* @param arg1 the first message argument
* @param arg2 the second message argument
* @return the object
* @throws InvalidRequestException if the specified object is {@code null}.
*/
public static T checkNotNull(T object,
String messageTemplate,
Object arg1,
Object arg2) throws InvalidRequestException
{
checkTrue(object != null, messageTemplate, arg1, arg2);
return object;
}
/**
* Checks that the specified bind marker value is set to a meaningful value.
* If it is not a {@code InvalidRequestException} will be thrown.
*
* @param b the ByteBuffer
to test
* @param messageTemplate the template used to build the error message
* @param messageArg the message argument
* @throws InvalidRequestException if the specified bind marker value is not set to a meaningful value.
*/
public static void checkBindValueSet(ByteBuffer b, String messageTemplate, Object messageArg) throws InvalidRequestException
{
checkTrue(b != ByteBufferUtil.UNSET_BYTE_BUFFER, messageTemplate, messageArg);
}
/**
* Checks that the specified object is {@code null}.
* If it is not an {@code InvalidRequestException} will be thrown.
*
* @param object the object to test
* @param messageTemplate the template used to build the error message
* @param messageArg the message argument
* @throws InvalidRequestException if the specified object is not {@code null}.
*/
public static void checkNull(Object object, String messageTemplate, Object messageArg) throws InvalidRequestException
{
checkTrue(object == null, messageTemplate, messageArg);
}
/**
* Checks that the specified object is {@code null}.
* If it is not an {@code InvalidRequestException} will be thrown.
*
* @param object the object to test
* @param message the error message
* @throws InvalidRequestException if the specified object is not {@code null}.
*/
public static void checkNull(Object object, String message) throws InvalidRequestException
{
checkTrue(object == null, message);
}
/**
* Returns an {@code InvalidRequestException} with the specified message.
*
* @param message the error message
* @return an {@code InvalidRequestException} with the specified message.
*/
public static InvalidRequestException invalidRequest(String message)
{
return new InvalidRequestException(message);
}
/**
* Returns an {@code InvalidRequestException} with the specified message.
*
* @param messageTemplate the template used to build the error message
* @param messageArgs the message arguments
* @return an {@code InvalidRequestException} with the specified message.
*/
public static InvalidRequestException invalidRequest(String messageTemplate, Object... messageArgs)
{
return new InvalidRequestException(String.format(messageTemplate, messageArgs));
}
/**
* This class must not be instantiated as it only contains static methods.
*/
private RequestValidations()
{
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy