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

Side by Side Diff: third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/util/ProtoWrapper.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.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Locale;
23
24
25 /**
26 * Base class for generated protobuf wrapper classes. Includes utilities for val idation of proto
27 * fields and implements hash code memoization.
28 */
29 public abstract class ProtoWrapper extends InternalBase {
30
31 /** Unchecked validation exception indicating a code issue. */
32 public static final class ValidationArgumentException extends IllegalArgumentE xception {
33 public ValidationArgumentException(String message) {
34 super(message);
35 }
36 }
37
38 /** Checked validation exception indicating an bogus protocol buffer instance. */
39 public static final class ValidationException extends Exception {
40 public ValidationException(String message) {
41 super(message);
42 }
43
44 public ValidationException(Throwable cause) {
45 super(cause);
46 }
47 }
48
49 /** Immutable, empty list. */
50 private static final List<?> EMPTY_LIST = Collections.unmodifiableList(new Arr ayList<Object>(0));
51 private static final int UNINITIALIZED_HASH_CODE = -1;
52 private static final int NOT_UNITIALIZED_HASH_CODE = UNINITIALIZED_HASH_CODE + 1;
53 private int hashCode;
54
55 @Override
56 public final int hashCode() {
57 if (hashCode == UNINITIALIZED_HASH_CODE) {
58 int computedHashCode = computeHashCode();
59
60 // If computeHashCode() happens to return UNITIALIZED_HASH_CODE, replace i t with a
61 // different (constant but arbitrary) value so that the hash code doesn't need to be
62 // recomputed.
63 hashCode = (computedHashCode == UNINITIALIZED_HASH_CODE) ? NOT_UNITIALIZED _HASH_CODE
64 : computedHashCode;
65 }
66 return hashCode;
67 }
68
69 /** Returns a hash code for this wrapper. */
70 protected abstract int computeHashCode();
71
72 /** Returns an immutable, empty list with elements of type {@code T}. */
73 @SuppressWarnings("unchecked")
74 protected static <T> List<T> emptyList() {
75 return (List<T>) EMPTY_LIST;
76 }
77
78 /** Checks that the given field is non null. */
79 protected static void required(String fieldName, Object fieldValue) {
80 if (fieldValue == null) {
81 throw new ValidationArgumentException(
82 String.format(Locale.ROOT, "Required field '%s' was not set", fieldNam e));
83 }
84 }
85
86 /**
87 * Checks that the given collection contains non-null elements. Treats {@code null} as empty.
88 * Returns an immutable copy of the given collection.
89 */
90 protected static <T> List<T> optional(String fieldName, Collection<T> fieldVal ues) {
91 if ((fieldValues == null) || (fieldValues.size() == 0)) {
92 return emptyList();
93 }
94 ArrayList<T> copy = new ArrayList<T>(fieldValues);
95 for (int i = 0; i < copy.size(); i++) {
96 if (copy.get(i) == null) {
97 throw new ValidationArgumentException(String.format(Locale.ROOT,
98 "Element %d of repeated field '%s' must not be null.", i, fieldName) );
99 }
100 }
101 return Collections.unmodifiableList(copy);
102 }
103
104 /**
105 * Checks that the given field is non-empty. Returns an immutable copy of the given
106 * collection.
107 */
108 protected static <T> List<T> required(String fieldName, Collection<T> fieldVal ues) {
109 List<T> copy = optional(fieldName, fieldValues);
110 if (fieldValues.isEmpty()) {
111 throw new ValidationArgumentException(String.format(Locale.ROOT,
112 "Repeated field '%s' must have at least one element", fieldName));
113 }
114 return copy;
115 }
116
117 /** Checks that the given field is non-negative. */
118 protected static void nonNegative(String fieldName, int value) {
119 if (value < 0) {
120 throw new ValidationArgumentException(
121 String.format(Locale.ROOT, "Field '%s' must be non-negative: %d", fiel dName, value));
122 }
123 }
124
125 /** Checks that the given field is non-negative. */
126 protected static void nonNegative(String fieldName, long value) {
127 if (value < 0) {
128 throw new ValidationArgumentException(
129 String.format(Locale.ROOT, "Field '%s' must be non-negative: %d", fiel dName, value));
130 }
131 }
132
133 /** Checks that the given field is positive. */
134 protected static void positive(String fieldName, int value) {
135 if (value <= 0) {
136 throw new ValidationArgumentException(
137 String.format(Locale.ROOT, "Field '%s' must be positive: %d", fieldNam e, value));
138 }
139 }
140
141 /** Checks that the given field is positive. */
142 protected static void positive(String fieldName, long value) {
143 if (value <= 0) {
144 throw new ValidationArgumentException(
145 String.format(Locale.ROOT, "Field '%s' must be positive: %d", fieldNam e, value));
146 }
147 }
148
149 /**
150 * Checks that the given field is not empty. Only call when the field has a va lue:
151 * {@link #required} can be called first, or the check can be conditionally pe rformed.
152 */
153 protected static void nonEmpty(String fieldName, String value) {
154 if (Preconditions.checkNotNull(value).length() == 0) {
155 throw new ValidationArgumentException(
156 String.format(Locale.ROOT, "Field '%s' must be non-empty", fieldName)) ;
157 }
158 }
159
160 /**
161 * Checks that the given field is not empty. Only call when the field has a va lue:
162 * {@link #required} can be called first, or the check can be conditionally pe rformed.
163 */
164 protected static void nonEmpty(String fieldName, Bytes value) {
165 if (Preconditions.checkNotNull(value).size() == 0) {
166 throw new ValidationArgumentException(
167 String.format(Locale.ROOT, "Field '%s' must be non-empty", fieldName)) ;
168 }
169 }
170
171 /** Checks that the given condition holds. */
172 protected void check(boolean condition, String message) {
173 if (!condition) {
174 throw new ValidationArgumentException(String.format(Locale.ROOT, "%s: %s", message, this));
175 }
176 }
177
178 /** Throws exception indicating a one-of violation due to multiple defined cho ices. */
179 protected static void oneOfViolation(String field1, String field2) {
180 throw new ValidationArgumentException(String.format(Locale.ROOT,
181 "Multiple one-of fields defined, including: %s, %s", field1, field2));
182 }
183
184 /** Throws exception indicating that no one-of choices are defined. */
185 protected static void oneOfViolation() {
186 throw new ValidationArgumentException("No one-of fields defined");
187 }
188
189 //
190 // Equals helpers.
191 //
192
193 /**
194 * Returns {@code true} if the provided objects are both null or are non-null and equal. Returns
195 * {@code false} otherwise.
196 */
197 protected static boolean equals(Object x, Object y) {
198 if (x == null) {
199 return y == null;
200 }
201 if (y == null) {
202 return false;
203 }
204 return x.equals(y);
205 }
206
207 //
208 // Hash code helpers for primitive types (taken from com.google.common.primiti ves package).
209 //
210
211 /** Returns hash code for the provided {@code long} value. */
212 protected static int hash(long value) {
213 // See Longs#hashCode
214 return (int) (value ^ (value >>> 32));
215 }
216
217 /** Returns hash code for the provided {@code int} value. */
218 protected static int hash(int value) {
219 return value;
220 }
221
222 /** Returns hash code for the provided {@code boolean} value. */
223 protected static int hash(boolean value) {
224 // See Booleans#hashCode
225 return value ? 1231 : 1237;
226 }
227
228 /** Returns hash code for the provided {@code float} value. */
229 protected static int hash(float value) {
230 return Float.valueOf(value).hashCode();
231 }
232
233 /** Returns hash code for the provided {@code double} value. */
234 protected static int hash(double value) {
235 return Double.valueOf(value).hashCode();
236 }
237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698