|
| 1 | +/* |
| 2 | + * Copyright © 2021 Apple Inc. and the ServiceTalk project authors |
| 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 io.servicetalk.context.api; |
| 17 | + |
| 18 | +import io.servicetalk.context.api.ContextMap.Key; |
| 19 | + |
| 20 | +import javax.annotation.Nullable; |
| 21 | + |
| 22 | +import static java.lang.Integer.toHexString; |
| 23 | +import static java.lang.System.identityHashCode; |
| 24 | +import static java.util.Objects.requireNonNull; |
| 25 | + |
| 26 | +/** |
| 27 | + * Shared utilities for {@link ContextMap}. |
| 28 | + */ |
| 29 | +final class ContextMapUtils { |
| 30 | + private ContextMapUtils() { |
| 31 | + // no instances |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@link Object#toString()} implementation for {@link ContextMap}. |
| 36 | + * |
| 37 | + * @param map {@link ContextMap} to convert |
| 38 | + * @return {@link String} representation of the context map |
| 39 | + */ |
| 40 | + static String toString(final ContextMap map) { |
| 41 | + final String simpleName = map.getClass().getSimpleName(); |
| 42 | + final int size = map.size(); |
| 43 | + if (size == 0) { |
| 44 | + return simpleName + '@' + toHexString(identityHashCode(map)) + ":{}"; |
| 45 | + } |
| 46 | + // 12 is 1 character for '@' + 8 hash code integer in hex form + 1 character for ':' + 2 characters for "{}". |
| 47 | + // Assume size of 90 for each key/value pair: 42 overhead characters for formatting + 16 characters for key |
| 48 | + // name + 16 characters for key type + 16 characters for value. |
| 49 | + StringBuilder sb = new StringBuilder(simpleName.length() + 12 + size * 90); |
| 50 | + sb.append(simpleName) |
| 51 | + .append('@') |
| 52 | + // There are many copies of these maps around, the content maybe equal but a differentiating factor is |
| 53 | + // the object reference. this may help folks understand why state is not visible across AsyncContext |
| 54 | + // boundaries. |
| 55 | + .append(toHexString(identityHashCode(map))) |
| 56 | + .append(":{"); |
| 57 | + |
| 58 | + map.forEach((key, value) -> { |
| 59 | + sb.append(key).append('=').append(value == map ? "(this Map)" : value).append(", "); |
| 60 | + return true; |
| 61 | + }); |
| 62 | + sb.setLength(sb.length() - 2); |
| 63 | + return sb.append('}').toString(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@link java.util.Objects#equals(Object, Object)} alternative for {@link ContextMap}. |
| 68 | + * |
| 69 | + * @param first the first {@link ContextMap} |
| 70 | + * @param second the second {@link ContextMap} to compare equality with the first one |
| 71 | + * @return {@code true} if both {@link ContextMap}(s) are equal (contains the same elements), {@code false} |
| 72 | + * otherwise. |
| 73 | + */ |
| 74 | + static boolean equals(final ContextMap first, final ContextMap second) { |
| 75 | + if (first.size() != second.size()) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + final Key<?> stopped = first.forEach((key, value) -> second.contains((Key<? super Object>) key, value)); |
| 80 | + return stopped == null; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Make sure that the {@code value} type matches with the {@link Key#type()}. |
| 85 | + * |
| 86 | + * @param key the {@link Key} to verify |
| 87 | + * @param value the value to verify |
| 88 | + * @throws NullPointerException if {@code key == null} |
| 89 | + * @throws IllegalArgumentException if type of the {@code value} does not match with {@link Key#type()} |
| 90 | + */ |
| 91 | + static void ensureType(final Key<?> key, @Nullable final Object value) { |
| 92 | + requireNonNull(key); |
| 93 | + if (value != null && !key.type().isInstance(value)) { |
| 94 | + throw new IllegalArgumentException("Type of the value " + value + '(' + value.getClass() + ')' + |
| 95 | + " does mot match with " + key); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments