/** * Removes the {@code Entry} that has the given {@code key} if it is present. * * @param key the key for the {@code Entry} to be removed. * @return this. * @since 0.16 */ @SuppressWarnings("nullness") public Builder remove(String key) { Utils.checkNotNull(key, "key"); if (entries == null) { // Copy entries from the parent. entries = new ArrayList<Entry>(parent.getEntries()); } for (int i = 0; i < entries.size(); i++) { if (entries.get(i).getKey().equals(key)) { entries.remove(i); // Exit now because the entries list cannot contain duplicates. break; } } return this; }
/** * Adds or updates the {@code Entry} that has the given {@code key} if it is present. The new * {@code Entry} will always be added in the front of the list of entries. * * @param key the key for the {@code Entry} to be added. * @param value the value for the {@code Entry} to be added. * @return this. * @since 0.16 */ @SuppressWarnings("nullness") public Builder set(String key, String value) { // Initially create the Entry to validate input. Entry entry = Entry.create(key, value); if (entries == null) { // Copy entries from the parent. entries = new ArrayList<Entry>(parent.getEntries()); } for (int i = 0; i < entries.size(); i++) { if (entries.get(i).getKey().equals(entry.getKey())) { entries.remove(i); // Exit now because the entries list cannot contain duplicates. break; } } // Inserts the element at the front of this list. entries.add(0, entry); return this; }
.append(entry.getKey()) .append(TRACESTATE_KEY_VALUE_DELIMITER) .append(entry.getValue());
.append(entry.getKey()) .append(TRACESTATE_KEY_VALUE_DELIMITER) .append(entry.getValue());
/** * Returns the value to which the specified key is mapped, or null if this map contains no mapping * for the key. * * @param key with which the specified value is to be associated * @return the value to which the specified key is mapped, or null if this map contains no mapping * for the key. * @since 0.16 */ @javax.annotation.Nullable public String get(String key) { for (Entry entry : getEntries()) { if (entry.getKey().equals(key)) { return entry.getValue(); } } return null; }
private static List<Entry> toEntriesProto(List<io.opencensus.trace.Tracestate.Entry> entries) { List<Entry> entriesProto = new ArrayList<Entry>(); for (io.opencensus.trace.Tracestate.Entry entry : entries) { entriesProto.add( Entry.newBuilder().setKey(entry.getKey()).setValue(entry.getValue()).build()); } return entriesProto; }