com.github.cowwoc.requirements.Requirements Maven / Gradle / Ivy
Show all versions of token-bucket Show documentation
/*
* Copyright 2013 Gili Tzabari.
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
package com.github.cowwoc.requirements;
import com.github.cowwoc.requirements.annotation.CheckReturnValue;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import com.github.cowwoc.requirements.java.JavaRequirements;
import java.lang.String;
import com.github.cowwoc.requirements.java.ObjectVerifier;
import com.github.cowwoc.requirements.java.ObjectValidator;
import com.github.cowwoc.requirements.java.CollectionVerifier;
import com.github.cowwoc.requirements.java.CollectionValidator;
import com.github.cowwoc.requirements.java.ListVerifier;
import com.github.cowwoc.requirements.java.ListValidator;
import com.github.cowwoc.requirements.java.ArrayVerifier;
import com.github.cowwoc.requirements.java.ArrayValidator;
import com.github.cowwoc.requirements.java.ComparableVerifier;
import com.github.cowwoc.requirements.java.ComparableValidator;
import com.github.cowwoc.requirements.java.PrimitiveNumberVerifier;
import com.github.cowwoc.requirements.java.PrimitiveNumberValidator;
import com.github.cowwoc.requirements.java.PrimitiveIntegerVerifier;
import com.github.cowwoc.requirements.java.PrimitiveIntegerValidator;
import com.github.cowwoc.requirements.java.IntegerVerifier;
import com.github.cowwoc.requirements.java.IntegerValidator;
import com.github.cowwoc.requirements.java.PrimitiveFloatingPointVerifier;
import com.github.cowwoc.requirements.java.PrimitiveFloatingPointValidator;
import com.github.cowwoc.requirements.java.PrimitiveBooleanVerifier;
import com.github.cowwoc.requirements.java.PrimitiveBooleanValidator;
import com.github.cowwoc.requirements.java.PrimitiveCharacterVerifier;
import com.github.cowwoc.requirements.java.PrimitiveCharacterValidator;
import com.github.cowwoc.requirements.java.NumberVerifier;
import com.github.cowwoc.requirements.java.NumberValidator;
import com.github.cowwoc.requirements.java.BooleanVerifier;
import com.github.cowwoc.requirements.java.BooleanValidator;
import com.github.cowwoc.requirements.java.FloatingPointVerifier;
import com.github.cowwoc.requirements.java.FloatingPointValidator;
import com.github.cowwoc.requirements.java.BigDecimalVerifier;
import com.github.cowwoc.requirements.java.BigDecimalValidator;
import com.github.cowwoc.requirements.java.MapVerifier;
import com.github.cowwoc.requirements.java.MapValidator;
import com.github.cowwoc.requirements.java.PathVerifier;
import com.github.cowwoc.requirements.java.PathValidator;
import com.github.cowwoc.requirements.java.StringVerifier;
import com.github.cowwoc.requirements.java.StringValidator;
import com.github.cowwoc.requirements.java.UriVerifier;
import com.github.cowwoc.requirements.java.UriValidator;
import com.github.cowwoc.requirements.java.UrlVerifier;
import com.github.cowwoc.requirements.java.UrlValidator;
import com.github.cowwoc.requirements.java.ClassVerifier;
import com.github.cowwoc.requirements.java.ClassValidator;
import com.github.cowwoc.requirements.java.OptionalVerifier;
import com.github.cowwoc.requirements.java.OptionalValidator;
import com.github.cowwoc.requirements.java.InetAddressVerifier;
import com.github.cowwoc.requirements.java.InetAddressValidator;
import com.github.cowwoc.requirements.java.DefaultJavaRequirements;
import com.github.cowwoc.requirements.java.Configuration;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Verifies requirements using a thread-specific {@link Configuration configuration}.
*
* Thread-safety: This class is not thread-safe.
* @see DefaultRequirements
* @see JavaRequirements
*/
public final class Requirements implements Configuration, JavaRequirements
{
private final JavaRequirements javaRequirements;
/**
* Creates a new instance of Requirements.
*/
public Requirements()
{
this.javaRequirements = new DefaultJavaRequirements();
}
/**
* @param javaRequirements the JavaRequirements instance to delegate to
* @throws AssertionError if any of the arguments are null
*/
private Requirements(JavaRequirements javaRequirements)
{
assert (javaRequirements != null) : "javaRequirements may not be null";
this.javaRequirements = javaRequirements;
}
/**
* Returns a copy of this configuration.
*
* @return a copy of this configuration
*/
public Requirements copy()
{
return new Requirements(javaRequirements.copy());
}
@Override
public boolean assertionsAreEnabled()
{
return javaRequirements.assertionsAreEnabled();
}
@Override
public Requirements withAssertionsEnabled()
{
javaRequirements.withAssertionsEnabled();
return this;
}
@Override
public Requirements withAssertionsDisabled()
{
javaRequirements.withAssertionsDisabled();
return this;
}
@Override
public boolean isDiffEnabled()
{
return javaRequirements.isDiffEnabled();
}
@Override
public Requirements withDiff()
{
javaRequirements.withDiff();
return this;
}
@Override
public Requirements withoutDiff()
{
javaRequirements.withoutDiff();
return this;
}
@Override
public boolean isCleanStackTrace()
{
return javaRequirements.isCleanStackTrace();
}
@Override
public Requirements withCleanStackTrace()
{
javaRequirements.withCleanStackTrace();
return this;
}
@Override
public Requirements withoutCleanStackTrace()
{
javaRequirements.withoutCleanStackTrace();
return this;
}
@Override
public Map getContext()
{
return javaRequirements.getContext();
}
@Override
public Requirements withContext(String name, Object value)
{
javaRequirements.withContext(name, value);
return this;
}
@Override
public Requirements withoutContext(String name)
{
javaRequirements.withoutContext(name);
return this;
}
@Override
public Requirements withoutAnyContext()
{
javaRequirements.withoutAnyContext();
return this;
}
@Override
public String getContextMessage(String message)
{
return javaRequirements.getContextMessage(message);
}
@Override
public String toString(Object value)
{
return javaRequirements.toString(value);
}
@Override
public Requirements withStringConverter(Class type, Function converter)
{
javaRequirements.withStringConverter(type, converter);
return this;
}
@Override
public Requirements withoutStringConverter(Class type)
{
javaRequirements.withoutStringConverter(type);
return this;
}
@Override
public Requirements withConfiguration(Configuration newConfig)
{
javaRequirements.withConfiguration(newConfig);
return this;
}
/**
* Verifies requirements only if {@link Configuration#assertionsAreEnabled() assertions are enabled}.
*
* @param the return value of the operation
* @param requirements the requirements to verify
* @return the return value of the operation, or {@code null} if assertions are disabled
* @throws NullPointerException if {@code requirements} is null
* @see #assertThat(Consumer)
*/
public V assertThatAndReturn(Function requirements)
{
// Use a simple if-statement to reduce computation/allocation when assertions are disabled
if (requirements == null)
throw new IllegalArgumentException("requirements may not be null");
if (assertionsAreEnabled())
return requirements.apply(this);
return null;
}
/**
* Verifies requirements only if {@link Configuration#assertionsAreEnabled() assertions are enabled}.
*
* @param requirements the requirements to verify
* @throws NullPointerException if {@code requirements} is null
* @see #assertThatAndReturn(Function)
*/
public void assertThat(Consumer requirements)
{
// Use a simple if-statement to reduce computation/allocation when assertions are disabled
if (requirements == null)
throw new IllegalArgumentException("requirements may not be null");
if (assertionsAreEnabled())
requirements.accept(this);
}
@Override
@CheckReturnValue
public ObjectVerifier requireThat(T actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ObjectValidator validateThat(T actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public , E> CollectionVerifier requireThat(C actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public , E> CollectionValidator validateThat(C actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public , E> ListVerifier requireThat(L actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public , E> ListValidator validateThat(L actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(byte[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(byte[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(short[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(short[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(int[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(int[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(long[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(long[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(float[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(float[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(double[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(double[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(boolean[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(boolean[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(char[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(char[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayVerifier requireThat(E[] actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ArrayValidator validateThat(E[] actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public > ComparableVerifier requireThat(T actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public > ComparableValidator validateThat(T actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveNumberVerifier requireThat(byte actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveNumberValidator validateThat(byte actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveNumberVerifier requireThat(short actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveNumberValidator validateThat(short actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveIntegerVerifier requireThat(int actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveIntegerValidator validateThat(int actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public IntegerVerifier requireThat(Integer actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public IntegerValidator validateThat(Integer actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveIntegerVerifier requireThat(long actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveIntegerValidator validateThat(long actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public IntegerVerifier requireThat(Long actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public IntegerValidator validateThat(Long actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveFloatingPointVerifier requireThat(float actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveFloatingPointValidator validateThat(float actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveFloatingPointVerifier requireThat(double actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveFloatingPointValidator validateThat(double actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveBooleanVerifier requireThat(boolean actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveBooleanValidator validateThat(boolean actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveCharacterVerifier requireThat(char actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PrimitiveCharacterValidator validateThat(char actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public > NumberVerifier requireThat(T actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public > NumberValidator validateThat(T actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public BooleanVerifier requireThat(Boolean actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public BooleanValidator validateThat(Boolean actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public FloatingPointVerifier requireThat(Float actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public FloatingPointValidator validateThat(Float actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public FloatingPointVerifier requireThat(Double actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public FloatingPointValidator validateThat(Double actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public BigDecimalVerifier requireThat(BigDecimal actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public BigDecimalValidator validateThat(BigDecimal actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public MapVerifier requireThat(Map actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public MapValidator validateThat(Map actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public PathVerifier requireThat(Path actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public PathValidator validateThat(Path actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public StringVerifier requireThat(String actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public StringValidator validateThat(String actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public UriVerifier requireThat(URI actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public UriValidator validateThat(URI actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public UrlVerifier requireThat(URL actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public UrlValidator validateThat(URL actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public ClassVerifier requireThat(Class actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public ClassValidator validateThat(Class actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public OptionalVerifier requireThat(Optional> actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public OptionalValidator validateThat(Optional> actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
@Override
@CheckReturnValue
public InetAddressVerifier requireThat(InetAddress actual, String name)
{
return javaRequirements.requireThat(actual, name);
}
@Override
@CheckReturnValue
public InetAddressValidator validateThat(InetAddress actual, String name)
{
return javaRequirements.validateThat(actual, name);
}
}