/** * 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; }
/** * 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; }
/** * 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; }
/** * Builds a TraceState by adding the entries to the parent in front of the key-value pairs list * and removing duplicate entries. * * @return a TraceState with the new entries. * @since 0.16 */ public Tracestate build() { if (entries == null) { return parent; } return Tracestate.create(entries); } }
@Test public void getEntries() { assertThat(firstTracestate.getEntries()).containsExactly(Entry.create(FIRST_KEY, FIRST_VALUE)); assertThat(secondTracestate.getEntries()) .containsExactly(Entry.create(SECOND_KEY, SECOND_VALUE)); assertThat(multiValueTracestate.getEntries()) .containsExactly( Entry.create(FIRST_KEY, FIRST_VALUE), Entry.create(SECOND_KEY, SECOND_VALUE)); }
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; }
@Test public void addAndUpdateEntry() { assertThat( firstTracestate .toBuilder() .set(FIRST_KEY, SECOND_VALUE) // update the existing entry .set(SECOND_KEY, FIRST_VALUE) // add a new entry .build() .getEntries()) .containsExactly( Entry.create(FIRST_KEY, SECOND_VALUE), Entry.create(SECOND_KEY, FIRST_VALUE)); }
@Test public void addSameKey() { assertThat( EMPTY .toBuilder() .set(FIRST_KEY, SECOND_VALUE) // update the existing entry .set(FIRST_KEY, FIRST_VALUE) // add a new entry .build() .getEntries()) .containsExactly(Entry.create(FIRST_KEY, FIRST_VALUE)); }