Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / Util.java
blobac1984611125a1f2e2a8aac5720f2f177c00174a
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search;
5 import com.google.apphosting.api.AppEngineInternal;
7 import java.util.Date;
8 import java.util.Iterator;
9 import java.util.LinkedList;
11 /**
12 * A utility class that does various checks on search and indexing parameters.
15 @AppEngineInternal
16 public final class Util {
18 private static final long MILLISECONDS_IN_DAY = 1000L * 60 * 60 * 24;
20 private Util() {
23 /**
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));
32 /**
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;
43 /**
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
51 * to build a string
53 public static <T> String iterableToString(Iterable<T> objects, int limit) {
54 StringBuilder builder = new StringBuilder()
55 .append("[");
56 String sep = "";
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());
62 sep = ", ";
64 LinkedList<T> tailMembers = new LinkedList<T>();
65 int seen = 0;
66 while (iter.hasNext()) {
67 tailMembers.add(iter.next());
68 if (++seen > tail) {
69 tailMembers.removeFirst();
72 if (seen > tail) {
73 builder.append(", ...");
75 for (T o : tailMembers) {
76 builder.append(sep).append(o.toString());
77 sep = ", ";
79 return builder.append("]").toString();
82 /**
83 * Returns a string which can be used to append an iterable field to a debug
84 * string.
86 public static <T> String iterableFieldToString(String fieldName, Iterable<T> objects) {
87 if (!objects.iterator().hasNext()) {
88 return "";
90 return String.format(", %s=%s", fieldName, Util.iterableToString(objects, 0));
93 /**
94 * Returns a string that allows a field value pair to be added to a debug
95 * string.
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
103 * string.
105 public static String fieldToString(String name, Object value, boolean firstParameter) {
106 if (value == null) {
107 return "";
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
122 @Deprecated
123 public static int getIntForDate(Date date) {
124 return (int) (date.getTime() / MILLISECONDS_IN_DAY);