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

Introduce static MultiValueMap factory methods #32832

Closed
poutsma opened this issue May 16, 2024 · 2 comments
Closed

Introduce static MultiValueMap factory methods #32832

poutsma opened this issue May 16, 2024 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@poutsma
Copy link
Contributor

poutsma commented May 16, 2024

Java 9 introduced static factory methods on the collection interaces, to create immutable collections. We should consider doing the same for MultiValueMap.

There are several open questions to answer:

  • Should multi-value maps returned by these factory methods be immutable, just like the java collections?
  • It seems difficult to create a convenient, unambiguous API that takes keys and values. Consider MultiValueMap.of("foo", "bar", "baz", "qux"). Does the resulting map have two entries (foo->bar and baz->qux)? Or does it have one entry, with three values (foo->[bar, baz, qux])? Without reading the javadoc, you would not know, which makes the method problematic.
  • We could introduce MultiValueMap<K, V> MultiValueMap::of(Map<K, V>), so that you could do MultiValueMap.of(Map.of("foo", "bar").
  • If we also want to support multiple values, we could have MultiValueMap<K, V> MultiValueMap::of(Map<K, List<V>>), which allows MultiValueMap.of(Map.of("foo", List.of("bar", "baz")).
  • If we want to support both of variants (single and multi value), they would have to be named differently because of type erasure. For instance, MultiValueMap<K, V> MultiValueMap::ofSingle(Map<K, V>) and MultiValueMap<K, V> MultiValueMap::ofMulti(Map<K, List<V>>)
@poutsma poutsma added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels May 16, 2024
@poutsma poutsma added this to the 6.2.x milestone May 16, 2024
@poutsma poutsma self-assigned this May 16, 2024
@xenoterracide
Copy link

xenoterracide commented May 16, 2024

It seems difficult to create a convenient, unambiguous API that takes keys and values. Consider MultiValueMap.of("foo", "bar", "baz", "qux"). Does the resulting map have two entries (foo->bar and baz->qux)? Or does it have one entry, with three values (foo->[bar, baz, qux])? Without reading the javadoc, you would not know, which makes the method problematic.

I think I would add this, but not call it of rather something like ofKeyValues, although if you go with java's way... that would result in "foo->bar and baz->qux, but you could get the latter by doing "foo", "bar", "foo", "baz", "foo", "qux" although I think that java has a limit to this telescoping constructor, so diminishing returns.

We could introduce MultiValueMap<K, V> MultiValueMap::of(Map<K, V>), so that you could do MultiValueMap.of(Map.of("foo", "bar").

also a good idea

I think I would probably have

  • MultiValueMap<K, V> MultiValueMap::of(Map<K, List<V>>)
  • MultiValueMap<K, V> MultiValueMap::ofMap(Map<K, V>)

the ofMap or ofSingle feels like it probably covers the other case well enough. If later the other needs to be added, it still could. I'm not certain about simply ofSingle as a name... intuitively I'm not certain would would think of this as each key only having a single value.

ofSingle feels like of singleton, or maybe foo->[bar, baz, qux]) where ofMulti is foo->bar and baz->qux or maybe it's the other way around... lol. of single value? of single key?

@poutsma
Copy link
Contributor Author

poutsma commented May 24, 2024

In the end, we settled for fromMultiValue and fromSingleValue, so that you can write fromSingleValue(Map.of("foo", "bar")), which reads rather nicely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants