public class Util
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
Util.FilteringIterator<T>
Iterator that applies a predicate to each element.
|
static class |
Util.FoundOne
Exception used to interrupt a tree walk of any kind.
|
static class |
Util.OverFinder
Visitor which looks for an OVER clause inside a tree of
SqlNode objects. |
private static class |
Util.RandomAccessTransformingList<F,T>
Extension to
Util.TransformingList that implements
RandomAccess . |
private static class |
Util.TransformingList<F,T>
List that returns the same number of elements as a backing list,
applying a transformation function to each one.
|
Modifier and Type | Field and Description |
---|---|
private static java.nio.charset.Charset |
DEFAULT_CHARSET |
private static com.google.common.cache.LoadingCache<java.lang.Class,java.util.Map<java.lang.String,java.lang.Enum>> |
ENUM_CONSTANTS
Maps classes to the map of their enum values.
|
static java.lang.String |
FILE_SEPARATOR
System-dependent file separator, for example, "/" or "\."
|
static java.lang.String |
FILE_TIMESTAMP_FORMAT
Datetime format string for generating a timestamp string to be used as
part of a filename.
|
private static java.util.regex.Pattern |
JAVA_ID_PATTERN
Regular expression for a valid java identifier which contains no
underscores and can therefore be returned intact by
toJavaId(java.lang.String, int) . |
static java.lang.String |
LINE_SEPARATOR
System-dependent newline character.
|
Modifier | Constructor and Description |
---|---|
private |
Util() |
Modifier and Type | Method and Description |
---|---|
private static void |
appendPosixDaylightTransition(java.util.TimeZone tz,
java.lang.StringBuilder buf,
int mode,
int day,
int month,
int dayOfWeek,
int time,
int timeMode,
boolean verbose,
boolean isEnd)
Writes a daylight savings time transition to a POSIX timezone
description.
|
private static void |
appendPosixTime(java.lang.StringBuilder buf,
int millis)
Given a time expressed in milliseconds, append the time formatted as
"hh[:mm[:ss]]".
|
static <K,V> java.util.Map<K,V> |
asIndexMap(java.util.Collection<V> values,
com.google.common.base.Function<V,K> function)
Deprecated.
|
static <K,V> java.util.Map<K,V> |
asIndexMapJ(java.util.Collection<V> values,
java.util.function.Function<V,K> function)
Returns a map that is a view onto a collection of values, using the
provided function to convert a value to a key.
|
static <K,V> java.util.Map<K,V> |
blackholeMap()
Returns a map which ignores any write operation.
|
static java.util.Calendar |
calendar()
Creates a
Calendar in the UTC time zone and root locale. |
static java.util.Calendar |
calendar(long millis)
Creates a
Calendar in the UTC time zone and root locale
with a given time. |
static <E> java.lang.Iterable<E> |
cast(java.lang.Iterable<? super E> iterable,
java.lang.Class<E> clazz)
Converts an
Iterable whose members are automatically down-cast to
a given type. |
static <E> java.util.Iterator<E> |
cast(java.util.Iterator<?> iter,
java.lang.Class<E> clazz)
Converts a iterator whose members are automatically down-cast to a given
type.
|
static <E> java.util.List<E> |
cast(java.util.List<? super E> list,
java.lang.Class<E> clazz)
Converts a list whose members are automatically down-cast to a given
type.
|
static <T> java.lang.String |
commaList(java.util.List<T> list)
Converts a list of a string, with commas between elements.
|
static boolean |
contains(java.lang.String[] a,
int length,
java.lang.String s)
Returns whether an array of strings contains a given string among the
first
length entries. |
static void |
debugCode(java.io.PrintStream out,
java.lang.String code)
Prints the given code with line numbering.
|
static <T> T |
deprecated(T argument,
boolean fail)
Deprecated.
If a piece of code calls this method, it indicates that the
code needs to be cleaned up.
|
private static java.lang.String |
digits3(long x,
int z) |
static void |
discard(boolean b)
Does nothing with its argument.
|
static void |
discard(double d)
Does nothing with its argument.
|
static void |
discard(int i)
Does nothing with its argument.
|
static void |
discard(java.lang.Object o)
Does nothing with its argument.
|
static <E> java.util.List<E> |
distinctList(java.lang.Iterable<E> keys)
Converts an iterable into a list with unique elements.
|
static <E> java.util.List<E> |
distinctList(java.util.List<E> list)
Converts a list into a list with unique elements.
|
static <T extends java.lang.Enum<T>> |
enumConstants(java.lang.Class<T> clazz)
Creates a map of the values of an enumeration by name.
|
static <T extends java.lang.Enum<T>> |
enumVal(java.lang.Class<T> clazz,
java.lang.String name)
Returns the value of an enumeration with a particular name.
|
static <T extends java.lang.Enum<T>> |
enumVal(T default_,
java.lang.String name)
Returns the value of an enumeration with a particular or default value if
not found.
|
static <T> boolean |
equalShallow(java.util.List<? extends T> list0,
java.util.List<? extends T> list1)
Returns whether two lists are equal to each other using shallow
comparisons.
|
static <E> java.util.Collection<E> |
filter(java.util.Collection<?> collection,
java.lang.Class<E> includeFilter) |
static <E> java.lang.Iterable<E> |
filter(java.lang.Iterable<?> iterable,
java.lang.Class<E> includeFilter)
Makes a collection of untyped elements appear as a list of strictly typed
elements, by filtering out those which are not of the correct type.
|
static <E> java.lang.Iterable<E> |
filter(java.lang.Iterable<E> iterable,
java.util.function.Predicate<E> predicate)
Filters an iterable.
|
static <E> java.util.Iterator<E> |
filter(java.util.Iterator<E> iterator,
java.util.function.Predicate<E> predicate)
Filters an iterator.
|
static <E> java.util.List<E> |
filter(java.util.List<?> list,
java.lang.Class<E> includeFilter)
Returns a subset of a list containing only elements of a given type.
|
static int |
findMatch(java.util.List<java.lang.String> strings,
java.lang.String seek,
boolean caseSensitive)
Looks for a string within a list of strings, using a given
case-sensitivity policy, and returns the position at which the first match
is found, or -1 if there are no matches.
|
static boolean |
first(java.lang.Boolean v0,
boolean v1)
Unboxes a
Boolean value,
using a given default value if it is null. |
static byte |
first(java.lang.Byte v0,
byte v1)
Unboxes a
Byte value,
using a given default value if it is null. |
static char |
first(java.lang.Character v0,
char v1)
Unboxes a
Character value,
using a given default value if it is null. |
static double |
first(java.lang.Double v0,
double v1)
Unboxes a
Double value,
using a given default value if it is null. |
static float |
first(java.lang.Float v0,
float v1)
Unboxes a
Float value,
using a given default value if it is null. |
static int |
first(java.lang.Integer v0,
int v1)
Unboxes a
Integer value,
using a given default value if it is null. |
static long |
first(java.lang.Long v0,
long v1)
Unboxes a
Long value,
using a given default value if it is null. |
static short |
first(java.lang.Short v0,
short v1)
Unboxes a
Short value,
using a given default value if it is null. |
static <T> T |
first(T v0,
T v1)
Returns the first value if it is not null,
otherwise the second value.
|
static <E> int |
firstDuplicate(java.util.List<E> list)
Returns the ordinal of the first element in the list which is equal to a
previous element in the list.
|
static java.nio.charset.Charset |
getDefaultCharset()
Returns the
Charset object representing the value of
CalciteSystemProperty.DEFAULT_CHARSET |
static java.lang.String |
getFileTimestamp()
Deprecated.
|
static java.lang.String |
getMessages(java.lang.Throwable t)
Deprecated.
|
static java.lang.String |
getStackTrace(java.lang.Throwable t)
Deprecated.
Use
Throwables.getStackTraceAsString(Throwable) |
static int |
hash(int i,
int j)
Deprecated.
Use
Objects.hash(Object...) |
static int |
hash(int h,
java.lang.Object o)
Deprecated.
Use
Objects.hash(Object...) |
static int |
hashArray(int h,
java.lang.Object[] a)
Deprecated.
Use
Objects.hash(Object...) |
static int |
hashCode(double v)
Deprecated.
|
static java.lang.String |
human(double d)
Converts a number into human-readable form, with 3 digits and a "K", "M"
or "G" multiplier for thousands, millions or billions.
|
static <E> java.util.List<java.util.List<E>> |
immutableCopy(java.lang.Iterable<? extends java.lang.Iterable<E>> lists)
Returns a copy of a list of lists, making the component lists immutable if
they are not already.
|
static <E> boolean |
intersects(java.util.Collection<E> c0,
java.util.Collection<E> c1)
Returns whether two collections have any elements in common.
|
static <E> boolean |
isDistinct(java.util.List<E> list)
Returns whether the elements of
list are distinct. |
static boolean |
isNullOrEmpty(java.lang.String s) |
static boolean |
isSingleValue(SqlCall call)
Does nothing with its argument.
|
static boolean |
isValidJavaIdentifier(java.lang.String s)
Returns true when input string is a valid Java identifier.
|
static <E> E |
last(java.util.List<E> list)
Returns the last element of a list.
|
static <E> java.util.List<E> |
last(java.util.List<E> list,
int n)
Returns the last
n elements of a list. |
static java.lang.String |
lines(java.lang.Iterable<java.lang.String> strings)
Converts a list of strings to a string separated by newlines.
|
static java.lang.String |
listToString(java.util.List<java.lang.String> list)
Converts ["ab", "c"] to "ab"."c".
|
static <K,V> java.util.Map<K,V> |
mapOf(K key,
V value,
java.lang.Object... keyValues)
Returns a hashmap with given contents.
|
static boolean |
matches(boolean caseSensitive,
java.lang.String s0,
java.lang.String s1)
Returns whether a name matches another according to a given
case-sensitivity policy.
|
static <T> java.util.Set<T> |
minus(java.util.Set<T> set1,
java.util.Set<T> set2)
Returns a set of the elements which are in
set1 but not in
set2 , without modifying either. |
static java.lang.RuntimeException |
needToImplement(java.lang.Object o)
Returns a
RuntimeException indicating that a particular
feature has not been implemented, but should be. |
static java.lang.Error |
newInternal()
Deprecated.
Throw new
AssertionError |
static java.lang.Error |
newInternal(java.lang.String s)
Deprecated.
Throw new
AssertionError |
static java.lang.Error |
newInternal(java.lang.Throwable e)
Deprecated.
Throw new
RuntimeException if checked; throw raw
exception if unchecked or Error |
static java.lang.Error |
newInternal(java.lang.Throwable e,
java.lang.String s)
Deprecated.
Throw new
AssertionError if applicable;
or RuntimeException if e is checked;
or raw exception if e is unchecked or Error . |
static double |
nLogN(double d)
Computes
nlogn(n) using the natural logarithm (or
n if n < , so the result is never
negative. |
static <T> java.lang.Iterable<T> |
orEmpty(java.lang.Iterable<T> v0) |
static <E> java.util.List<Pair<E,E>> |
pairs(java.util.List<E> list)
Given a list with N elements
[e0, e1, ..., eN-1]
(where N is even), returns a list of the N / 2 elements
[ (e0, e1),
(e2, e3), ...
|
static java.util.Locale |
parseLocale(java.lang.String localeString)
Parses a locale string.
|
static void |
permAssert(boolean b,
java.lang.String description)
Deprecated.
Use
Preconditions.checkArgument(boolean) |
static void |
post(boolean b,
java.lang.String description)
Deprecated.
Use
Preconditions.checkArgument(boolean)
or Objects.requireNonNull(Object) |
static void |
pre(boolean b,
java.lang.String description)
Deprecated.
Use
Preconditions.checkArgument(boolean)
or Objects.requireNonNull(Object) |
static void |
print(java.io.PrintWriter pw,
java.lang.Object o)
Prints an object using reflection.
|
static void |
print(java.io.PrintWriter pw,
java.lang.Object o,
int indent) |
static void |
printJavaString(java.lang.Appendable appendable,
java.lang.String s,
boolean nullMeansNull)
Prints a string, enclosing in double quotes (") and escaping if
necessary.
|
static void |
println(java.io.PrintWriter pw,
java.lang.Object o) |
static java.io.PrintWriter |
printWriter(java.io.File file)
Creates a
PrintWriter to a given file using UTF-8
character set. |
static java.io.PrintWriter |
printWriter(java.io.OutputStream out)
Creates a
PrintWriter to a given output stream using UTF-8
character set. |
static <E> java.util.List<E> |
quotientList(java.util.List<E> list,
int n,
int k)
Creates a list that returns every
n th element of a list,
starting at element k . |
static java.util.List<java.lang.Integer> |
range(int end) |
static java.util.List<java.lang.Integer> |
range(int start,
int end) |
static java.lang.String |
readAllAsString(java.io.Reader reader)
Deprecated.
|
static java.io.BufferedReader |
reader(java.io.File file)
Creates a
BufferedReader to read a given file using UTF-8
character set. |
static java.io.BufferedReader |
reader(java.io.InputStream in)
Creates a
BufferedReader to a given input stream using UTF-8
character set. |
static java.lang.String |
replace(java.lang.String s,
java.lang.String find,
java.lang.String replace)
Replaces every occurrence of
find in s with
replace . |
static java.lang.String |
rpad(java.lang.String s,
int len)
Deprecated.
Use
Spaces.padRight(String, int) |
static java.lang.String |
rtrim(java.lang.String s)
Deprecated.
|
static <E> java.util.List<E> |
select(java.util.List<E> list,
java.util.List<java.lang.Integer> ordinals)
Returns a view of a list, picking the elements of a list with the given
set of ordinals.
|
static <T> java.lang.String |
sepList(java.util.List<T> list,
java.lang.String sep)
Converts a list of a string, with a given separator between elements.
|
static <E> java.util.List<E> |
skip(java.util.List<E> list)
Returns all but the first element of a list.
|
static <E> java.util.List<E> |
skip(java.util.List<E> list,
int fromIndex)
Returns all but the first
n elements of a list. |
static <E> java.util.List<E> |
skipLast(java.util.List<E> list)
Returns every element of a list but its last element.
|
static <E> java.util.List<E> |
skipLast(java.util.List<E> list,
int n)
Returns every element of a list but its last
n elements. |
static void |
squelchConnection(java.sql.Connection connection)
Deprecated.
|
static void |
squelchJar(java.util.jar.JarFile jar)
Deprecated.
|
static void |
squelchReader(java.io.Reader reader)
Deprecated.
|
static void |
squelchStmt(java.sql.Statement stmt)
Deprecated.
|
static void |
squelchStream(java.io.InputStream stream)
Deprecated.
|
static void |
squelchStream(java.io.OutputStream stream)
Deprecated.
|
static void |
squelchWriter(java.io.Writer writer)
Deprecated.
|
static <E> boolean |
startsWith(java.util.List<E> list0,
java.util.List<E> list1)
Returns whether one list is a prefix of another.
|
static java.util.List<java.lang.String> |
stringToList(java.lang.String s) |
static java.lang.String |
stripDoubleQuotes(java.lang.String value)
Converts double-quoted Java strings to their contents.
|
static void |
swallow(java.lang.Throwable e,
org.slf4j.Logger logger)
Records that an exception has been caught but will not be re-thrown.
|
static void |
throwIfUnchecked(java.lang.Throwable throwable)
As
Throwables .throwIfUnchecked(Throwable) ,
which was introduced in Guava 20,
but we don't require Guava version 20 yet. |
static <T> java.util.stream.Collector<T,com.google.common.collect.ImmutableList.Builder<T>,com.google.common.collect.ImmutableList<T>> |
toImmutableList()
Returns a
Collector that accumulates the input elements into a
Guava ImmutableList via a ImmutableList.Builder . |
static java.lang.String |
toJavaId(java.lang.String s,
int ordinal)
Converts an arbitrary string into a string suitable for use as a Java
identifier.
|
static java.lang.Iterable<java.lang.String> |
tokenize(java.lang.String s,
java.lang.String delim)
Converts a string into tokens.
|
static java.lang.String |
toLinux(java.lang.String s) |
static <T> java.util.List<T> |
toList(java.util.Iterator<T> iter)
Deprecated.
|
static java.util.Map<java.lang.String,java.lang.String> |
toMap(java.util.Properties properties)
Converts a
Properties object to a . |
static java.lang.String |
toPosix(java.util.TimeZone tz,
boolean verbose)
Converts a Java timezone to POSIX format, so that the boost C++ library
can instantiate timezone objects.
|
static java.lang.String |
toScientificNotation(java.math.BigDecimal bd)
Formats a
BigDecimal value to a string in scientific notation For
exampleA value of 0.00001234 would be formated as 1.234E-5
A value of 100000.00 would be formated as 1.00E5
A value of 100 (scale zero) would be formated as
1E2
|
static <T> java.lang.String |
toString(java.lang.Iterable<T> iterable,
java.lang.String start,
java.lang.String sep,
java.lang.String end)
Converts an iterable to a string.
|
static java.lang.RuntimeException |
toUnchecked(java.lang.Exception e)
Wraps an exception with
RuntimeException and return it. |
static java.net.URL |
toURL(java.io.File file)
Deprecated.
|
static <F,T> java.util.List<T> |
transform(java.util.List<F> list,
java.util.function.Function<F,T> function)
Transforms a list, applying a function to each element.
|
static <E extends java.lang.Enum<E>> |
unexpected(E value)
Returns an exception indicating that we didn't expect to find this
enumeration here.
|
public static final java.lang.String LINE_SEPARATOR
In general, you should not use this in expected results of tests.
Expected results should be the expected result on Linux (or Mac OS) using
'\n'. Apply toLinux(String)
to Windows actual results, if
necessary, to make them look like Linux actual.
public static final java.lang.String FILE_SEPARATOR
public static final java.lang.String FILE_TIMESTAMP_FORMAT
private static final java.util.regex.Pattern JAVA_ID_PATTERN
toJavaId(java.lang.String, int)
.private static final java.nio.charset.Charset DEFAULT_CHARSET
private static final com.google.common.cache.LoadingCache<java.lang.Class,java.util.Map<java.lang.String,java.lang.Enum>> ENUM_CONSTANTS
public static boolean isSingleValue(SqlCall call)
call
- the expression to evaluatepublic static void discard(java.lang.Object o)
public static void discard(int i)
public static void discard(boolean b)
public static void discard(double d)
public static void swallow(java.lang.Throwable e, org.slf4j.Logger logger)
e
- Exceptionlogger
- If not null, logs exception to this loggerpublic static <T> boolean equalShallow(java.util.List<? extends T> list0, java.util.List<? extends T> list1)
list0
- First listlist1
- Second list==
(may be null).@Deprecated public static int hash(int i, int j)
Objects.hash(Object...)
@Deprecated public static int hash(int h, java.lang.Object o)
Objects.hash(Object...)
@Deprecated public static int hashArray(int h, java.lang.Object[] a)
Objects.hash(Object...)
@Deprecated public static int hashCode(double v)
Utilities.hashCode(double)
double
value. Equivalent to
Double
.hashCode(double)
, but that method was only
introduced in JDK 1.8.v
- Valuepublic static <T> java.util.Set<T> minus(java.util.Set<T> set1, java.util.Set<T> set2)
set1
but not in
set2
, without modifying either.public static double nLogN(double d)
nlogn(n)
using the natural logarithm (or
n
if n < Math.E
, so the result is never
negative.public static void print(java.io.PrintWriter pw, java.lang.Object o)
null
;
arrays of objects and primitive values; for regular objects, we print all
public fields.public static void print(java.io.PrintWriter pw, java.lang.Object o, int indent)
public static void printJavaString(java.lang.Appendable appendable, java.lang.String s, boolean nullMeansNull)
printDoubleQuoted(w,"x\"y",false)
prints "x\"y"
.
The appendable where the value is printed must not incur I/O operations. This method is not meant to be used for writing the values to permanent storage.
java.lang.IllegalStateException
- if the print to the specified appendable fails due to I/Opublic static void println(java.io.PrintWriter pw, java.lang.Object o)
public static java.lang.String toScientificNotation(java.math.BigDecimal bd)
BigDecimal
value to a string in scientific notation For
example1.234E-5
1.00E5
1E2
If bd
has a precision higher than 20, this method will
truncate the output string to have a precision of 20 (no rounding will be
done, just a truncate).
public static java.lang.String replace(java.lang.String s, java.lang.String find, java.lang.String replace)
find
in s
with
replace
.@Deprecated public static java.net.URL toURL(java.io.File file) throws java.net.MalformedURLException
java.net.MalformedURLException
@Deprecated public static java.lang.String getFileTimestamp()
public static java.lang.String stripDoubleQuotes(java.lang.String value)
"foo\"bar"
becomes foo"bar
.public static java.lang.String toJavaId(java.lang.String s, int ordinal)
The mapping is one-to-one (that is, distinct strings will produce distinct java identifiers). The mapping is also reversible, but the inverse mapping is not implemented.
A valid Java identifier must start with a Unicode letter, underscore, or dollar sign ($). The other characters, if any, can be a Unicode letter, underscore, dollar sign, or digit.
This method uses an algorithm similar to URL encoding. Valid characters are unchanged; invalid characters are converted to an underscore followed by the hex code of the character; and underscores are doubled.
Examples:
toJavaId("foo")
returns "foo"
toJavaId("foo bar")
returns "foo_20_bar"
toJavaId("foo_bar")
returns "foo__bar"
toJavaId("0bar")
returns "_40_bar"
(digits
are illegal as a prefix)
toJavaId("foo0bar")
returns "foo0bar"
public static boolean isValidJavaIdentifier(java.lang.String s)
s
- input stringpublic static java.lang.String toLinux(java.lang.String s)
@Deprecated public static <T> java.util.List<T> toList(java.util.Iterator<T> iter)
Iterator
as a
List
.iter
- iterator to materializepublic static boolean isNullOrEmpty(java.lang.String s)
public static <T> java.lang.String commaList(java.util.List<T> list)
For example,
commaList(Arrays.asList({"a", "b"}))
returns "a, b".
list
- Listpublic static <T> java.lang.String sepList(java.util.List<T> list, java.lang.String sep)
public static java.nio.charset.Charset getDefaultCharset()
Charset
object representing the value of
CalciteSystemProperty.DEFAULT_CHARSET
java.nio.charset.IllegalCharsetNameException
- If the given charset
name is illegaljava.nio.charset.UnsupportedCharsetException
- If no support for
the named charset is
available in this
instance of the Java
virtual machine@Deprecated public static java.lang.Error newInternal()
AssertionError
@Deprecated public static java.lang.Error newInternal(java.lang.String s)
AssertionError
@Deprecated public static java.lang.Error newInternal(java.lang.Throwable e)
RuntimeException
if checked; throw raw
exception if unchecked or Error
public static java.lang.Error newInternal(java.lang.Throwable e, java.lang.String s)
AssertionError
if applicable;
or RuntimeException
if e is checked;
or raw exception if e is unchecked or Error
.public static void throwIfUnchecked(java.lang.Throwable throwable)
Throwables
.throwIfUnchecked(Throwable)
,
which was introduced in Guava 20,
but we don't require Guava version 20 yet.public static java.lang.RuntimeException toUnchecked(java.lang.Exception e)
RuntimeException
and return it.
If the exception is already an instance of RuntimeException,
returns it directly.@Deprecated public static java.lang.String getMessages(java.lang.Throwable t)
@Deprecated public static java.lang.String getStackTrace(java.lang.Throwable t)
Throwables.getStackTraceAsString(Throwable)
t
- Throwable@Deprecated public static void pre(boolean b, java.lang.String description)
Preconditions.checkArgument(boolean)
or Objects.requireNonNull(Object)
@Deprecated public static void post(boolean b, java.lang.String description)
Preconditions.checkArgument(boolean)
or Objects.requireNonNull(Object)
@Deprecated public static void permAssert(boolean b, java.lang.String description)
Preconditions.checkArgument(boolean)
public static java.lang.RuntimeException needToImplement(java.lang.Object o)
RuntimeException
indicating that a particular
feature has not been implemented, but should be.
If every 'hole' in our functionality uses this method, it will be
easier for us to identity the holes. Throwing a
UnsupportedOperationException
isn't as good, because
sometimes we actually want to partially implement an API.
Example usage:
class MyVisitor extends BaseVisitor { void accept(Foo foo) { // Exception will identify which subclass forgot to override // this method throw Util.needToImplement(this); } }
o
- The object which was the target of the call, or null. Passing
the object gives crucial information if a method needs to be
overridden and a subclass forgot to do so.UnsupportedOperationException
.public static <T> T deprecated(T argument, boolean fail)
Introduce a call to this method to indicate that a piece of code, or a javadoc comment, needs work before you check in. If you have an IDE which can easily trace references, this is an easy way to maintain a to-do list.
Checked-in code must never call this method: you must remove all calls/references to this method before you check in.
The argument
has generic type and determines the type of
the result. This allows you to use the method inside an expression, for
example
int x = Util.deprecated(0, false);
but the usual usage is to pass in a descriptive string.
deprecated
to fail if a piece of
supposedly dead code is reachedvoid foo(int x) { if (x < 0) { // If this code is executed, an error will be thrown. Util.deprecated( "no longer need to handle negative numbers", true); bar(x); } else { baz(x); } }
deprecated
to comment out dead
codeif (Util.deprecated(false, false)) { // This code will not be executed, but an error will not be thrown. baz(); }
argument
- Arbitrary argument to the method.fail
- Whether to throw an exception if this method is calledargument
.public static boolean contains(java.lang.String[] a, int length, java.lang.String s)
length
entries.a
- Array of stringslength
- Number of entries to searchs
- String to seek@Deprecated public static java.lang.String readAllAsString(java.io.Reader reader) throws java.io.IOException
Reader
and returns
them as a string.reader
- reader to read fromjava.io.IOException
@Deprecated public static void squelchJar(java.util.jar.JarFile jar)
jar
- jar to close@Deprecated public static void squelchStream(java.io.InputStream stream)
stream
- stream to close@Deprecated public static void squelchStream(java.io.OutputStream stream)
stream
- stream to close@Deprecated public static void squelchReader(java.io.Reader reader)
reader
- reader to close@Deprecated public static void squelchWriter(java.io.Writer writer)
writer
- writer to close@Deprecated public static void squelchStmt(java.sql.Statement stmt)
stmt
- stmt to close@Deprecated public static void squelchConnection(java.sql.Connection connection)
connection
- connection to close@Deprecated public static java.lang.String rtrim(java.lang.String s)
s
- string to be trimmed@Deprecated public static java.lang.String rpad(java.lang.String s, int len)
Spaces.padRight(String, int)
s
- string to be paddedlen
- desired lengthpublic static <T> java.lang.String toString(java.lang.Iterable<T> iterable, java.lang.String start, java.lang.String sep, java.lang.String end)
public static java.lang.String lines(java.lang.Iterable<java.lang.String> strings)
public static java.lang.Iterable<java.lang.String> tokenize(java.lang.String s, java.lang.String delim)
public static java.lang.String toPosix(java.util.TimeZone tz, boolean verbose)
POSIX IEEE 1003.1 defines a format for timezone specifications.
The boost C++ library can read these specifications and instantiate posix_time_zone objects from them. The purpose of this method, therefore, is to allow the C++ code such as the fennel calculator to use the same notion of timezone as Java code.
The format is as follows:
"std offset dst [offset],start[/time],end[/time]"
where:
[+|-]hh[:mm[:ss]] {h=0-23, m/s=0-59}
For example:
(Real format strings do not contain spaces; they are in the above template only for readability.)
Boost apparently diverges from the POSIX standard in how it treats the sign of timezone offsets. The POSIX standard states 'If preceded by a '-', the timezone shall be east of the Prime Meridian; otherwise, it shall be west', yet boost requires the opposite. For instance, PST has offset '-8' above. This method generates timezone strings consistent with boost's expectations.
tz
- Timezoneverbose
- Whether to include fields which can be omitted because
they have their default valuesprivate static void appendPosixDaylightTransition(java.util.TimeZone tz, java.lang.StringBuilder buf, int mode, int day, int month, int dayOfWeek, int time, int timeMode, boolean verbose, boolean isEnd)
tz
- Timezonebuf
- Buffer to append tomode
- Transition modeday
- Day of transitionmonth
- Month of transitiondayOfWeek
- Day of week of transitiontime
- Time of transition in millistimeMode
- Mode of time transitionverbose
- VerboseisEnd
- Whether this transition is leaving DSTprivate static void appendPosixTime(java.lang.StringBuilder buf, int millis)
buf
- Buffer to append tomillis
- Millisecondspublic static java.util.Locale parseLocale(java.lang.String localeString)
The inverse operation of Locale.toString()
.
localeString
- Locale string, e.g. "en" or "en_US"public static <E> java.util.List<E> cast(java.util.List<? super E> list, java.lang.Class<E> clazz)
If a member of the backing list is not an instanceof E
,
the accessing method (such as List.get(int)
) will throw a
ClassCastException
.
All modifications are automatically written to the backing list. Not synchronized.
list
- Backing list.clazz
- Class to cast to.public static <E> java.util.Iterator<E> cast(java.util.Iterator<?> iter, java.lang.Class<E> clazz)
If a member of the backing iterator is not an instanceof
E
, Iterator.next()
) will throw a
ClassCastException
.
All modifications are automatically written to the backing iterator. Not synchronized.
iter
- Backing iterator.clazz
- Class to cast to.public static <E> java.lang.Iterable<E> cast(java.lang.Iterable<? super E> iterable, java.lang.Class<E> clazz)
Iterable
whose members are automatically down-cast to
a given type.
All modifications are automatically written to the backing iterator. Not synchronized.
iterable
- Backing iterableclazz
- Class to cast topublic static <E> java.lang.Iterable<E> filter(java.lang.Iterable<?> iterable, java.lang.Class<E> includeFilter)
The returned object is an Iterable
,
which makes it ideal for use with the 'foreach' construct. For example,
List<Number> numbers = Arrays.asList(1, 2, 3.14,
4, null, 6E23);
for (int myInt : filter(numbers, Integer.class)) {
print(i);
}
will print 1, 2, 4.
iterable
- IterableincludeFilter
- Class whose instances to includepublic static <E> java.util.Collection<E> filter(java.util.Collection<?> collection, java.lang.Class<E> includeFilter)
public static <E> java.util.List<E> filter(java.util.List<?> list, java.lang.Class<E> includeFilter)
Modifications to the list are NOT written back to the source list.
list
- List of objectsincludeFilter
- Class to filter forpublic static java.util.Map<java.lang.String,java.lang.String> toMap(java.util.Properties properties)
Properties
object to a Map
<String,
String>
.
This is necessary because Properties
is a dinosaur class. It
ought to extend Map<String,String>
, but instead
extends
.
Hashtable
<Object,Object>
Typical usage, to iterate over a Properties
:
Properties properties;
for (Map.Entry<String, String> entry =
Util.toMap(properties).entrySet()) {
println("key=" + entry.getKey() + ", value=" + entry.getValue());
}
public static <K,V> java.util.Map<K,V> mapOf(K key, V value, java.lang.Object... keyValues)
Use this method in initializers. Type parameters are inferred from context, and the contents are initialized declaratively. For example,
Map<String, Integer> population =
Olap4jUtil.mapOf(
"UK", 65000000,
"USA", 300000000);
K
- Key typeV
- Value typekey
- First keyvalue
- First valuekeyValues
- Second and sequent key/value pairspublic static <E extends java.lang.Enum<E>> java.lang.Error unexpected(E value)
value
- Enumeration value which was not expectedpublic static <T extends java.lang.Enum<T>> java.util.Map<java.lang.String,T> enumConstants(java.lang.Class<T> clazz)
clazz
- Enumeration classpublic static <T extends java.lang.Enum<T>> T enumVal(java.lang.Class<T> clazz, java.lang.String name)
Similar to Enum.valueOf(Class, String)
, but returns null
rather than throwing IllegalArgumentException
.
T
- Enum class typeclazz
- Enum classname
- Name of enum constantpublic static <T extends java.lang.Enum<T>> T enumVal(T default_, java.lang.String name)
T
- Enum class typedefault_
- Default value (not null)name
- Name of enum constantpublic static <E> java.util.List<E> quotientList(java.util.List<E> list, int n, int k)
n
th element of a list,
starting at element k
.
It is OK if the list is empty or its size is not a multiple of
n
.
For instance, quotientList(list, 2, 0)
returns the even
elements of a list, and quotientList(list, 2, 1)
returns the odd
elements. Those lists are the same length only if list has even size.
public static <E> java.util.List<Pair<E,E>> pairs(java.util.List<E> list)
public static <T> T first(T v0, T v1)
The result may be null.
Equivalent to the Elvis operator (?:
) of languages such as
Groovy or PHP.
public static double first(java.lang.Double v0, double v1)
Double
value,
using a given default value if it is null.public static float first(java.lang.Float v0, float v1)
Float
value,
using a given default value if it is null.public static int first(java.lang.Integer v0, int v1)
Integer
value,
using a given default value if it is null.public static long first(java.lang.Long v0, long v1)
Long
value,
using a given default value if it is null.public static boolean first(java.lang.Boolean v0, boolean v1)
Boolean
value,
using a given default value if it is null.public static short first(java.lang.Short v0, short v1)
Short
value,
using a given default value if it is null.public static char first(java.lang.Character v0, char v1)
Character
value,
using a given default value if it is null.public static byte first(java.lang.Byte v0, byte v1)
Byte
value,
using a given default value if it is null.public static <T> java.lang.Iterable<T> orEmpty(java.lang.Iterable<T> v0)
public static <E> E last(java.util.List<E> list)
java.lang.IndexOutOfBoundsException
- if the list is emptypublic static <E> java.util.List<E> skipLast(java.util.List<E> list)
public static <E> java.util.List<E> skipLast(java.util.List<E> list, int n)
n
elements.public static <E> java.util.List<E> last(java.util.List<E> list, int n)
n
elements of a list.public static <E> java.util.List<E> skip(java.util.List<E> list)
public static <E> java.util.List<E> skip(java.util.List<E> list, int fromIndex)
n
elements of a list.public static java.util.List<java.lang.Integer> range(int end)
public static java.util.List<java.lang.Integer> range(int start, int end)
public static <E> boolean isDistinct(java.util.List<E> list)
list
are distinct.public static <E> int firstDuplicate(java.util.List<E> list)
For example,
firstDuplicate(Arrays.asList("a", "b", "c", "b", "a"))
returns 3, the ordinal of the 2nd "b".
list
- Listpublic static <E> java.util.List<E> distinctList(java.util.List<E> list)
The order is preserved; the second and subsequent occurrences are removed.
If the list is already unique it is returned unchanged.
public static <E> java.util.List<E> distinctList(java.lang.Iterable<E> keys)
The order is preserved; the second and subsequent occurrences are removed.
If iterable
is a unique list it is returned unchanged.
public static <E> boolean intersects(java.util.Collection<E> c0, java.util.Collection<E> c1)
public static int findMatch(java.util.List<java.lang.String> strings, java.lang.String seek, boolean caseSensitive)
public static boolean matches(boolean caseSensitive, java.lang.String s0, java.lang.String s1)
public static <E> boolean startsWith(java.util.List<E> list0, java.util.List<E> list1)
public static java.lang.String listToString(java.util.List<java.lang.String> list)
public static java.util.List<java.lang.String> stringToList(java.lang.String s)
public static java.lang.String human(double d)
Examples: -2, 0, 1, 999, 1.00K, 1.99K, 3.45M, 4.56B.
private static java.lang.String digits3(long x, int z)
public static <K,V> java.util.Map<K,V> asIndexMapJ(java.util.Collection<V> values, java.util.function.Function<V,K> function)
Unlike
Maps.uniqueIndex(Iterable, com.google.common.base.Function)
,
returns a view whose contents change as the collection of values changes.
K
- Key typeV
- Value typevalues
- Collection of valuesfunction
- Function to map value to key@Deprecated public static <K,V> java.util.Map<K,V> asIndexMap(java.util.Collection<V> values, com.google.common.base.Function<V,K> function)
public static void debugCode(java.io.PrintStream out, java.lang.String code)
public static <E> java.util.List<java.util.List<E>> immutableCopy(java.lang.Iterable<? extends java.lang.Iterable<E>> lists)
public static java.io.PrintWriter printWriter(java.io.OutputStream out)
PrintWriter
to a given output stream using UTF-8
character set.
Does not use the default character set.
public static java.io.PrintWriter printWriter(java.io.File file) throws java.io.FileNotFoundException
PrintWriter
to a given file using UTF-8
character set.
Does not use the default character set.
java.io.FileNotFoundException
public static java.io.BufferedReader reader(java.io.InputStream in)
BufferedReader
to a given input stream using UTF-8
character set.
Does not use the default character set.
public static java.io.BufferedReader reader(java.io.File file) throws java.io.FileNotFoundException
BufferedReader
to read a given file using UTF-8
character set.
Does not use the default character set.
java.io.FileNotFoundException
public static java.util.Calendar calendar()
Calendar
in the UTC time zone and root locale.
Does not use the time zone or locale.public static java.util.Calendar calendar(long millis)
Calendar
in the UTC time zone and root locale
with a given time.public static <T> java.util.stream.Collector<T,com.google.common.collect.ImmutableList.Builder<T>,com.google.common.collect.ImmutableList<T>> toImmutableList()
Collector
that accumulates the input elements into a
Guava ImmutableList
via a ImmutableList.Builder
.
It will be obsolete when we move to Guava 21.0
,
which has ImmutableList.toImmutableList()
.
T
- Type of the input elementsCollector
that collects all the input elements into an
ImmutableList
, in encounter orderpublic static <F,T> java.util.List<T> transform(java.util.List<F> list, java.util.function.Function<F,T> function)
public static <E> java.lang.Iterable<E> filter(java.lang.Iterable<E> iterable, java.util.function.Predicate<E> predicate)
public static <E> java.util.Iterator<E> filter(java.util.Iterator<E> iterator, java.util.function.Predicate<E> predicate)
public static <E> java.util.List<E> select(java.util.List<E> list, java.util.List<java.lang.Integer> ordinals)
public static <K,V> java.util.Map<K,V> blackholeMap()
Copyright © 2012–2019 The Apache Software Foundation. All rights reserved.