DefaultQueryParamsBuilder extends StringMultimapBuilder<
        /* IN_NAME */ String, /* NAME */ String,
        /* CONTAINER */ QueryParamsBase, /* SELF */ QueryParamsBuilder> implements QueryParamsBuilder { @Override public QueryParams build() { final QueryParamsBase delegate = delegate(); if (delegate != null) { if (delegate.isEmpty()) { return DefaultQueryParams.EMPTY; } else { return new DefaultQueryParams(promoteDelegate()); } } final QueryParamsBase parent = parent(); if (parent != null) { if (parent instanceof QueryParams) { return (QueryParams) parent; } return updateParent(new DefaultQueryParams(parent)); } return DefaultQueryParams.EMPTY; } DefaultQueryParamsBuilder();   DefaultQueryParamsBuilder(QueryParamsBase parent); @Override QueryParams build();  }
@Test void add() { final QueryParams headers = QueryParams.builder() .add("a", "b") .add("c", ImmutableList.of("d", "e")) .add("f", "g", "h") .add(ImmutableMap.of("i", "j").entrySet()) .build(); assertThat(headers).containsExactly( Maps.immutableEntry("a", "b"), Maps.immutableEntry("c", "d"), Maps.immutableEntry("c", "e"), Maps.immutableEntry("f", "g"), Maps.immutableEntry("f", "h"), Maps.immutableEntry("i", "j")); }
@Test void set() { final QueryParams headers = QueryParams.builder() .add("a", "b") .add("c", ImmutableList.of("d", "e")) .add("f", "g", "h") .add(ImmutableMap.of("i", "j").entrySet()) .set("a", "B") .set("c", ImmutableList.of("D", "E")) .set("f", "G", "H") .set(ImmutableMap.of("i", "J").entrySet()) .build(); assertThat(headers).containsExactly( Maps.immutableEntry("a", "B"), Maps.immutableEntry("c", "D"), Maps.immutableEntry("c", "E"), Maps.immutableEntry("f", "G"), Maps.immutableEntry("f", "H"), Maps.immutableEntry("i", "J")); }
@Test void mutation() { final QueryParams headers = QueryParams.of("a", "b"); final QueryParams headers2 = headers.toBuilder().set("a", "c").build(); assertThat(headers).isNotSameAs(headers2); assertThat(headers).isNotEqualTo(headers2); assertThat(headers).containsExactly(Maps.immutableEntry("a", "b")); assertThat(headers2).containsExactly(Maps.immutableEntry("a", "c")); }
@Test void mutationAfterBuild() { final QueryParams headers = QueryParams.of("a", "b"); final DefaultQueryParamsBuilder builder = (DefaultQueryParamsBuilder) headers.toBuilder(); assertThat(builder.parent()).isSameAs(headers); assertThat(builder.delegate()).isNull(); builder.add("c", "d"); assertThat(builder.parent()).isSameAs(headers); assertThat(builder.delegate()).isNotNull().isNotSameAs(builder.parent()); QueryParamsBase oldDelegate = builder.delegate(); final QueryParams headers2 = builder.build(); assertThat(headers2).isNotSameAs(headers); assertThat(((QueryParamsBase) headers2).entries).isNotSameAs(((QueryParamsBase) headers).entries); assertThat(builder.parent()).isSameAs(oldDelegate); assertThat(builder.delegate()).isNull(); builder.add("e", "f"); assertThat(builder.parent()).isSameAs(oldDelegate); assertThat(builder.delegate()).isNotNull().isNotSameAs(builder.parent()); oldDelegate = builder.delegate(); final QueryParams headers3 = builder.build(); assertThat(headers3).isNotSameAs(headers); assertThat(headers3).isNotSameAs(headers2); assertThat(((QueryParamsBase) headers3).entries).isNotSameAs(((QueryParamsBase) headers).entries); assertThat(((QueryParamsBase) headers3).entries).isNotSameAs(((QueryParamsBase) headers2).entries); assertThat(builder.parent()).isSameAs(oldDelegate); assertThat(builder.delegate()).isNull(); builder.clear(); assertThat(headers).isNotSameAs(headers3); assertThat(headers).containsExactly(Maps.immutableEntry("a", "b")); assertThat(headers2).containsExactly(Maps.immutableEntry("a", "b"), Maps.immutableEntry("c", "d")); assertThat(headers3).containsExactly(Maps.immutableEntry("a", "b"), Maps.immutableEntry("c", "d"), Maps.immutableEntry("e", "f")); }
@Test void noMutationNoCopy() { final QueryParams headers = QueryParams.of("a", "b"); assertThat(headers.toBuilder().build()).isSameAs(headers); final DefaultQueryParamsBuilder builder = (DefaultQueryParamsBuilder) headers.toBuilder(); builder.getAndRemove("c"); builder.getAndRemove("c", "d"); builder.getIntAndRemove("c"); builder.getIntAndRemove("c", 0); builder.getLongAndRemove("c"); builder.getLongAndRemove("c", 0); builder.getFloatAndRemove("c"); builder.getFloatAndRemove("c", 0); builder.getDoubleAndRemove("c"); builder.getDoubleAndRemove("c", 0); builder.getAllAndRemove("c"); builder.getTimeMillisAndRemove("c"); builder.getTimeMillisAndRemove("c", 0); builder.remove("c"); builder.removeAndThen("c"); assertThat(builder.build()).isSameAs(headers); assertThat(builder.delegate()).isNull(); }
@Test void empty() { final QueryParams headers = QueryParams.of("a", "b"); assertThat(headers.toBuilder() .clear() .build()).isSameAs(DefaultQueryParams.EMPTY); }
@Test void buildTwice() { final QueryParamsBuilder builder = QueryParams.builder().add("foo", "bar"); assertThat(builder.build()).isEqualTo(QueryParams.of("foo", "bar")); assertThat(builder.build()).isEqualTo(QueryParams.of("foo", "bar")); }