Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended jinja template to generate template-type semantic attributes. #24

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

public final class AttributeKeyTemplate<T> {
AlexanderWert marked this conversation as resolved.
Show resolved Hide resolved
AlexanderWert marked this conversation as resolved.
Show resolved Hide resolved

private final ConcurrentMap<String, AttributeKey<T>> keysCache = new ConcurrentHashMap<>();
private final String prefix;
private final Function<String, AttributeKey<T>> keyBuilder;
private ConcurrentMap<String, AttributeKey<T>> keysCache;
AlexanderWert marked this conversation as resolved.
Show resolved Hide resolved

AttributeKeyTemplate(String prefix, Function<String, AttributeKey<T>> keyBuilder) {
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
this.prefix = prefix;
Expand Down Expand Up @@ -56,11 +56,26 @@ static AttributeKeyTemplate<List<Double>> doubleArrayKeyTemplate(String prefix)
}

private AttributeKey<T> createAttributeKey(String keyName) {
String key = prefix + "." + keyName.toLowerCase(Locale.ROOT).replace('-', '_');
String key = prefix + "." + keyName.toLowerCase(Locale.ROOT);
AlexanderWert marked this conversation as resolved.
Show resolved Hide resolved
return keyBuilder.apply(key);
}

/**
* Returns an {@link AttributeKey} object for the given attribute key whereby the key is the
* variable part of the full attribute name in a template-typed attribute, for example
* <b>http.request.header.&lt;key&gt;</b>.
*
* <p>{@link AttributeKey} objets are being created and cached on the first invocation of this
* method for a certain key. Subsequent invocations of this method with the same key return the
* cached object.
*
* @param key The variable part of the template-typed attribute name.
* @return An {@link AttributeKey} object for the given key.
*/
public AttributeKey<T> getAttributeKey(String key) {
if (keysCache == null) {
keysCache = new ConcurrentHashMap<>(1);
}
return keysCache.computeIfAbsent(key, this::createAttributeKey);
}
}