do not use ellipsis in parameter domain arguments

This commit is contained in:
Jörg Prante 2023-03-31 15:47:56 +02:00
parent dbe58d2d84
commit ab86306ee7
2 changed files with 61 additions and 76 deletions

View file

@ -92,8 +92,8 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public String getAsString(String key, Domain... domains) { public String getAsString(String key, Domain domain) {
Object object = get(key, domains); Object object = get(key, domain);
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<Object> collection = (Collection<Object>) object; Collection<Object> collection = (Collection<Object>) object;
Iterator<Object> iterator = collection.iterator(); Iterator<Object> iterator = collection.iterator();
@ -108,8 +108,8 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Integer getAsInteger(String key, Domain... domains) { public Integer getAsInteger(String key, Domain domain) {
Object object = get(key, domains); Object object = get(key, domain);
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<Object> collection = (Collection<Object>) object; Collection<Object> collection = (Collection<Object>) object;
Iterator<Object> iterator = collection.iterator(); Iterator<Object> iterator = collection.iterator();
@ -128,8 +128,8 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Boolean getAsBoolean(String key, Domain... domains) { public Boolean getAsBoolean(String key, Domain domain) {
Object object = get(key, domains); Object object = get(key, domain);
if (object instanceof Collection) { if (object instanceof Collection) {
Collection<Object> collection = (Collection<Object>) object; Collection<Object> collection = (Collection<Object>) object;
Iterator<Object> iterator = collection.iterator(); Iterator<Object> iterator = collection.iterator();
@ -183,18 +183,15 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return object; return object;
} }
public List<Object> getAllDomain(Domain... domains) { public List<Object> getAllDomain(Domain domain) {
Parameter parameter = null; Parameter parameter = null;
for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) {
if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain);
parameter = builder.parameterMap.get(domain); }
} if (parameter != null) {
if (parameter != null) { return parameter.getAllDomain(domain);
List<Object> list = parameter.getAllDomain(domains); }
if (list != null) { if (getDomain().equals(domain)) {
return list;
}
}
return list.stream() return list.stream()
.map(Pair::getValue) .map(Pair::getValue)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -202,44 +199,39 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return null; return null;
} }
public boolean isPresent(Domain... domains) { public boolean isPresent(Domain domain) {
Parameter parameter = null; Parameter parameter = null;
for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) {
if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain);
parameter = builder.parameterMap.get(domain); }
} if (parameter != null) {
if (parameter != null) { return parameter.isPresent(domain);
boolean b = parameter.isPresent(domains); }
if (b) { if (getDomain().equals(domain)) {
return true;
}
}
return list.stream().findAny().isPresent(); return list.stream().findAny().isPresent();
} }
return false; return false;
} }
public Parameter getDomain(Domain... domains) { public Parameter getDomain(Domain domain) {
for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) {
if (builder.parameterMap.containsKey(domain)) { return builder.parameterMap.get(domain);
return builder.parameterMap.get(domain); }
} if (getDomain().equals(domain)) {
return this;
} }
return null; return null;
} }
public List<Object> getAll(String key, Domain... domains) { public List<Object> getAll(String key, Domain domain) {
Parameter parameter = null; Parameter parameter = null;
for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) {
if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain);
parameter = builder.parameterMap.get(domain); }
} if (parameter != null) {
if (parameter != null) { return parameter.getAll(key, domain);
List<Object> list = parameter.getAll(key, domains); }
if (list != null) { if (getDomain().equals(domain)) {
return list;
}
}
return list.stream() return list.stream()
.filter(p -> p.getKey().equals(key)) .filter(p -> p.getKey().equals(key))
.map(Pair::getValue) .map(Pair::getValue)
@ -248,43 +240,36 @@ public class Parameter implements Iterable<Pair<String, Object>>, Comparable<Par
return null; return null;
} }
public boolean containsKey(String key, Domain... domains) { public boolean containsKey(String key, Domain domain) {
Parameter parameter = null; Parameter parameter = null;
for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) {
if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain);
parameter = builder.parameterMap.get(domain); }
} if (parameter != null) {
if (parameter != null) { return parameter.containsKey(key, domain);
boolean b = parameter.containsKey(key, domains); }
if (b) { if (getDomain().equals(domain)) {
return true;
}
}
return list.stream() return list.stream()
.anyMatch(p -> key.equals(p.getKey())); .anyMatch(p -> key.equals(p.getKey()));
} }
return false; return false;
} }
public Object get(String key, Domain... domains) { public Object get(String key, Domain domain) {
Parameter parameter = null; Parameter parameter = null;
for (Domain domain : domains) { if (builder.parameterMap.containsKey(domain)) {
if (builder.parameterMap.containsKey(domain)) { parameter = builder.parameterMap.get(domain);
parameter = builder.parameterMap.get(domain); }
} if (parameter != null) {
if (parameter != null) { return parameter.get(key, domain);
Object object = parameter.get(key, domains); }
if (object != null) { if (getDomain().equals(domain)) {
return object; Optional<Object> optional = list.stream()
} .filter(p -> key.equals(p.getKey()))
} else { .map(Pair::getValue)
Optional<Object> optional = list.stream() .findFirst();
.filter(p -> key.equals(p.getKey())) if (optional.isPresent()) {
.map(Pair::getValue) return optional.get();
.findFirst();
if (optional.isPresent()) {
return optional.get();
}
} }
} }
return null; return null;

View file

@ -156,9 +156,9 @@ public class ParameterTest {
assertEquals("a", p.get("a", Parameter.Domain.QUERY).toString()); assertEquals("a", p.get("a", Parameter.Domain.QUERY).toString());
assertEquals("b", p.get("b", Parameter.Domain.FORM).toString()); assertEquals("b", p.get("b", Parameter.Domain.FORM).toString());
assertEquals("c", p.get("c", Parameter.Domain.HEADER).toString()); assertEquals("c", p.get("c", Parameter.Domain.HEADER).toString());
assertEquals("a", p.get("a", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER)); assertEquals("a", p.get("a", Parameter.Domain.QUERY));
assertEquals("b", p.get("b", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER)); assertEquals("b", p.get("b", Parameter.Domain.FORM));
assertEquals("c", p.get("c", Parameter.Domain.QUERY, Parameter.Domain.FORM, Parameter.Domain.HEADER)); assertEquals("c", p.get("c", Parameter.Domain.HEADER));
assertTrue(p.isPresent(Parameter.Domain.QUERY)); assertTrue(p.isPresent(Parameter.Domain.QUERY));
assertTrue(p.isPresent(Parameter.Domain.FORM)); assertTrue(p.isPresent(Parameter.Domain.FORM));
assertTrue(p.isPresent(Parameter.Domain.HEADER)); assertTrue(p.isPresent(Parameter.Domain.HEADER));