Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 The Flogger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.flogger.backend;
import static com.google.common.flogger.util.Checks.checkArgument;
import static com.google.common.flogger.util.Checks.checkNotNull;
import com.google.common.flogger.MetadataKey;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Callback API for logger backend implementations to handle metadata keys/values. The API methods
* will be called once for each distinct key, in encounter order. Different methods are called
* depending on whether the key is repeatable or not.
*
*
It is expected that the most convenient way to construct a metadata handler is via the {@link
* MetadataHandler.Builder Builder} class, which lets keys be individually mapped to callbacks,
* however the class can also just be extended to implement alternate/custom behavior.
*
* @param the arbitrary context type.
*/
public abstract class MetadataHandler {
/**
* Handles a single metadata key/value mapping. This method is called directly for singleton non
* repeatable) keys, but may also be called for repeated keys by the default implementation of
* {@link #handleRepeated}. It is up to the implementation to override that method if this
* behaviour is unwanted.
*
* @param key the metadata key (not necessarily a "singleton" key).
* @param value associated metadata value.
* @param context an arbitrary context object supplied to the process method.
* @param the key/value type.
*/
protected abstract void handle(MetadataKey key, T value, C context);
/**
* Handles values for a repeatable metadata key. The method is called for all repeatable keys
* (even those with only one value). The default implementation makes repeated callbacks to the
* {@link #handle} method, in order, for each value.
*
* @param key the repeatable metadata key.
* @param values a lightweight iterator over all values associated with the key. Note that this
* instance is read-only and must not be held beyond the scope of this callback.
* @param context an arbitrary context object supplied to the process method.
* @param the key/value type.
*/
protected void handleRepeated(MetadataKey key, Iterator values, C context) {
while (values.hasNext()) {
handle(key, values.next(), context);
}
}
/**
* Returns a builder for a handler with the specified default callback. The default handler will
* receive all key/value pairs from the metadata individually, which can result in repeated keys
* being seen more than once.
*
*
A default handler is required because no handler can know the complete set of keys which
* might be available and it is very undesirable to drop unknown keys. If default repeated values
* should be handled together, {@link Builder#setDefaultRepeatedHandler(RepeatedValueHandler)}
* should be called as well.
*
*
Unknown keys/values can only be handled in a generic fashion unless a given key is matched
* to a known constant. However the entire point of this map-based handler is to avoid any need to
* do explicit matching, so the default handler should not need to know the value type.
*
* @param defaultHandler the default handler for unknown keys/values.
* @param the context type.
*/
public static Builder builder(ValueHandler