1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
6 import java
.util
.Iterator
;
7 import java
.util
.LinkedList
;
10 * A utility class that does various checks on search and indexing parameters.
13 public final class Util
{
15 private static final long MILLISECONDS_IN_DAY
= 1000L * 60 * 60 * 24;
21 * @return whether or not the string is null or empty
23 public static boolean isNullOrEmpty(String string
) {
24 return string
== null || string
.isEmpty();
28 * A helper method for testing two objects are equal.
30 * @return whether the two objects a and b are equal
32 public static boolean equalObjects(Object a
, Object b
) {
33 return a
== b
|| (a
!= null && a
.equals(b
));
37 * A helper method for overriding null values with a default value.
39 * @param value the value of some field
40 * @param defaultValue the default value for the field
41 * @return value if it is not null, otherwise the defaultValue
43 public static <T
> T
defaultIfNull(T value
, T defaultValue
) {
44 return value
== null ? defaultValue
: value
;
48 * Returns a string representation of the iterable objects. This is
49 * used in debugging. The limit parameter is used to control how many
50 * elements of the iterable are used to build the final string. Use
51 * 0 or negative values, to include all.
53 * @param objects an iterable of objects to be turned into a string
54 * @param limit the maximum number of objects from iterable to be used
57 public static <T
> String
iterableToString(Iterable
<T
> objects
, int limit
) {
58 StringBuilder builder
= new StringBuilder()
61 int head
= (limit
<= 0) ? Integer
.MAX_VALUE
: (limit
+ 1) / 2;
62 int tail
= (limit
<= 0) ?
0 : limit
- head
;
63 Iterator
<T
> iter
= objects
.iterator();
64 while (iter
.hasNext() && --head
>= 0) {
65 builder
.append(sep
).append(iter
.next().toString());
68 LinkedList
<T
> tailMembers
= new LinkedList
<T
>();
70 while (iter
.hasNext()) {
71 tailMembers
.add(iter
.next());
73 tailMembers
.removeFirst();
77 builder
.append(", ...");
79 for (T o
: tailMembers
) {
80 builder
.append(sep
).append(o
.toString());
83 return builder
.append("]").toString();
87 * Returns a string which can be used to append an iterable field to a debug
90 public static <T
> String
iterableFieldToString(String fieldName
, Iterable
<T
> objects
) {
91 if (!objects
.iterator().hasNext()) {
94 return String
.format(", %s=%s", fieldName
, Util
.iterableToString(objects
, 0));
98 * Returns a string that allows a field value pair to be added to a debug
101 public static String
fieldToString(String name
, Object value
) {
102 return fieldToString(name
, value
, false);
106 * Returns a string that allows a field value pair to be added to a debug
109 public static String
fieldToString(String name
, Object value
, boolean firstParameter
) {
113 if (firstParameter
) {
114 return String
.format("%s=%s", name
, value
);
116 return String
.format(", %s=%s", name
, value
);
120 * Converts a given date to a numeric value, ignoring any time component.
122 * @param date the date to convert to an int
123 * @return a number representing the given date
124 * @deprecated as of 1.7.2
127 public static int getIntForDate(Date date
) {
128 return (int) (date
.getTime() / MILLISECONDS_IN_DAY
);