Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / Util.java
blob2f1ff5dd0da9c9864eaf9860febd514540a9d1b0
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search;
5 import java.util.Date;
6 import java.util.Iterator;
7 import java.util.LinkedList;
9 /**
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;
17 private Util() {
20 /**
21 * @return whether or not the string is null or empty
23 public static boolean isNullOrEmpty(String string) {
24 return string == null || string.isEmpty();
27 /**
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));
36 /**
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;
47 /**
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
55 * to build a string
57 public static <T> String iterableToString(Iterable<T> objects, int limit) {
58 StringBuilder builder = new StringBuilder()
59 .append("[");
60 String sep = "";
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());
66 sep = ", ";
68 LinkedList<T> tailMembers = new LinkedList<T>();
69 int seen = 0;
70 while (iter.hasNext()) {
71 tailMembers.add(iter.next());
72 if (++seen > tail) {
73 tailMembers.removeFirst();
76 if (seen > tail) {
77 builder.append(", ...");
79 for (T o : tailMembers) {
80 builder.append(sep).append(o.toString());
81 sep = ", ";
83 return builder.append("]").toString();
86 /**
87 * Returns a string which can be used to append an iterable field to a debug
88 * string.
90 public static <T> String iterableFieldToString(String fieldName, Iterable<T> objects) {
91 if (!objects.iterator().hasNext()) {
92 return "";
94 return String.format(", %s=%s", fieldName, Util.iterableToString(objects, 0));
97 /**
98 * Returns a string that allows a field value pair to be added to a debug
99 * string.
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
107 * string.
109 public static String fieldToString(String name, Object value, boolean firstParameter) {
110 if (value == null) {
111 return "";
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
126 @Deprecated
127 public static int getIntForDate(Date date) {
128 return (int) (date.getTime() / MILLISECONDS_IN_DAY);