Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/util/LazyString.java

Issue 1162033004: Pull cacheinvalidations code directory into chromium repo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.google.ipc.invalidation.util;
17
18 import java.util.Collection;
19
20
21 /**
22 * Utilities to enable creation of lazy strings, where the instantiation of the string is delayed
23 * so that, e.g., log messages that aren't printed have reduced overhead.
24 */
25 public class LazyString {
26
27 /** Receiver formatting objects using {@link Object#toString()}. */
28 public static final LazyStringReceiver<Object> OBJECT_RECEIVER =
29 new LazyStringReceiver<Object>() {
30 @Override
31 public void appendToBuilder(TextBuilder builder, Object element) {
32 builder.append(element);
33 }
34 };
35
36 /**
37 * Receiver appending an {@code element} to the given {@code builder}. Impleme ntations may assume
38 * the builder and element are not {@code null}.
39 */
40 public interface LazyStringReceiver<T> {
41 void appendToBuilder(TextBuilder builder, T element);
42 }
43
44 /**
45 * Given an {@code element} to be logged lazily, returns null if the object is null. Otherwise,
46 * return an object that would convert it to a string using {@code builderFunc tion}. I.e., this
47 * method will call {@code builderFunction} with a new {@link TextBuilder} and provided
48 * {@code element} and return the string created with it.
49 */
50 public static <T> Object toLazyCompactString(final T element,
51 final LazyStringReceiver<T> builderFunction) {
52 if (element == null) {
53 return null;
54 }
55 return new Object() {
56 @Override public String toString() {
57 TextBuilder builder = new TextBuilder();
58 builderFunction.appendToBuilder(builder, element);
59 return builder.toString();
60 }
61 };
62 }
63
64 /**
65 * Returns an object that converts the given {@code elements} array into a deb ug string when
66 * {@link Object#toString} is called using
67 * {@link #appendElementsToBuilder(TextBuilder, Object[], LazyStringReceiver)} .
68 */
69 public static <T> Object toLazyCompactString(final T[] elements,
70 final LazyStringReceiver<? super T> elementReceiver) {
71 if ((elements == null) || (elements.length == 0)) {
72 return null;
73 }
74 return new Object() {
75 @Override public String toString() {
76 return appendElementsToBuilder(new TextBuilder(), elements, elementRecei ver).toString();
77 }
78 };
79 }
80
81 /**
82 * Returns an object that converts the given {@code elements} collection into a debug string when
83 * {@link Object#toString} is called using
84 * {@link #appendElementsToBuilder(TextBuilder, Object[], LazyStringReceiver)} .
85 */
86 public static <T> Object toLazyCompactString(final Collection<T> elements,
87 final LazyStringReceiver<? super T> elementReceiver) {
88 if ((elements == null) || elements.isEmpty()) {
89 return null;
90 }
91 return new Object() {
92 @Override public String toString() {
93 return appendElementsToBuilder(new TextBuilder(), elements, elementRecei ver).toString();
94 }
95 };
96 }
97
98 /**
99 * Appends {@code elements} formatted using {@code elementReceiver} to {@code builder}. Elements
100 * are comma-separated.
101 */
102 public static <T> TextBuilder appendElementsToBuilder(TextBuilder builder, T[] elements,
103 LazyStringReceiver<? super T> elementReceiver) {
104 if (elements == null) {
105 return builder;
106 }
107 for (int i = 0; i < elements.length; i++) {
108 if (i != 0) {
109 builder.append(", ");
110 }
111 T element = elements[i];
112 if (element != null) {
113 elementReceiver.appendToBuilder(builder, element);
114 }
115 }
116 return builder;
117 }
118
119 /**
120 * Appends {@code elements} formatted using {@code elementReceiver} to {@code builder}. Elements
121 * are comma-separated.
122 */
123 public static <T> TextBuilder appendElementsToBuilder(TextBuilder builder,
124 Iterable<T> elements, LazyStringReceiver<? super T> elementReceiver) {
125 if (elements == null) {
126 return builder;
127 }
128 boolean first = true;
129 for (T element : elements) {
130 if (first) {
131 first = false;
132 } else {
133 builder.append(", ");
134 }
135 if (element != null) {
136 elementReceiver.appendToBuilder(builder, element);
137 }
138 }
139 return builder;
140 }
141
142 private LazyString() { // To prevent instantiation.
143 }
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698