1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.apphosting
.api
.AppEngineInternal
;
8 import java
.util
.Iterator
;
9 import java
.util
.LinkedList
;
12 * A utility class that does various checks on search and indexing parameters.
16 public final class Util
{
18 private static final long MILLISECONDS_IN_DAY
= 1000L * 60 * 60 * 24;
24 * A helper method for testing two objects are equal.
26 * @return whether the two objects a and b are equal
28 public static boolean equalObjects(Object a
, Object b
) {
29 return a
== b
|| (a
!= null && a
.equals(b
));
33 * A helper method for overriding null values with a default value.
35 * @param value the value of some field
36 * @param defaultValue the default value for the field
37 * @return value if it is not null, otherwise the defaultValue
39 public static <T
> T
defaultIfNull(T value
, T defaultValue
) {
40 return value
== null ? defaultValue
: value
;
44 * Returns a string representation of the iterable objects. This is
45 * used in debugging. The limit parameter is used to control how many
46 * elements of the iterable are used to build the final string. Use
47 * 0 or negative values, to include all.
49 * @param objects an iterable of objects to be turned into a string
50 * @param limit the maximum number of objects from iterable to be used
53 public static <T
> String
iterableToString(Iterable
<T
> objects
, int limit
) {
54 StringBuilder builder
= new StringBuilder()
57 int head
= (limit
<= 0) ? Integer
.MAX_VALUE
: (limit
+ 1) / 2;
58 int tail
= (limit
<= 0) ?
0 : limit
- head
;
59 Iterator
<T
> iter
= objects
.iterator();
60 while (iter
.hasNext() && --head
>= 0) {
61 builder
.append(sep
).append(iter
.next().toString());
64 LinkedList
<T
> tailMembers
= new LinkedList
<T
>();
66 while (iter
.hasNext()) {
67 tailMembers
.add(iter
.next());
69 tailMembers
.removeFirst();
73 builder
.append(", ...");
75 for (T o
: tailMembers
) {
76 builder
.append(sep
).append(o
.toString());
79 return builder
.append("]").toString();
83 * Returns a string which can be used to append an iterable field to a debug
86 public static <T
> String
iterableFieldToString(String fieldName
, Iterable
<T
> objects
) {
87 if (!objects
.iterator().hasNext()) {
90 return String
.format(", %s=%s", fieldName
, Util
.iterableToString(objects
, 0));
94 * Returns a string that allows a field value pair to be added to a debug
97 public static String
fieldToString(String name
, Object value
) {
98 return fieldToString(name
, value
, false);
102 * Returns a string that allows a field value pair to be added to a debug
105 public static String
fieldToString(String name
, Object value
, boolean firstParameter
) {
109 if (firstParameter
) {
110 return String
.format("%s=%s", name
, value
);
112 return String
.format(", %s=%s", name
, value
);
116 * Converts a given date to a numeric value, ignoring any time component.
118 * @param date the date to convert to an int
119 * @return a number representing the given date
120 * @deprecated as of 1.7.2
123 public static int getIntForDate(Date date
) {
124 return (int) (date
.getTime() / MILLISECONDS_IN_DAY
);