|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package io.micronaut.core.convert.value; |
|
|
|
import io.micronaut.core.convert.ArgumentConversionContext; |
|
import io.micronaut.core.convert.ConversionContext; |
|
import io.micronaut.core.convert.ConversionService; |
|
import io.micronaut.core.reflect.GenericTypeUtils; |
|
import io.micronaut.core.type.Argument; |
|
import io.micronaut.core.value.ValueResolver; |
|
|
|
import java.util.*; |
|
import java.util.function.BiConsumer; |
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface ConvertibleValues<V> extends ValueResolver<CharSequence>, Iterable<Map.Entry<String, V>> { |
|
|
|
ConvertibleValues EMPTY = new ConvertibleValuesMap<>(Collections.emptyMap()); |
|
|
|
|
|
|
|
|
|
Set<String> names(); |
|
|
|
|
|
|
|
|
|
Collection<V> values(); |
|
|
|
|
|
|
|
|
|
default boolean isEmpty() { |
|
return this == ConvertibleValues.EMPTY || names().isEmpty(); |
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
default Class<V> getValueType() { |
|
Optional<Class> type = GenericTypeUtils.resolveInterfaceTypeArgument(getClass(), ConvertibleValues.class); |
|
return type.orElse(Object.class); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default boolean contains(String name) { |
|
|
|
|
|
|
|
return get(name, Argument.OBJECT_ARGUMENT).isPresent(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default void forEach(BiConsumer<String, V> action) { |
|
Objects.requireNonNull(action, "Consumer cannot be null"); |
|
|
|
Collection<String> headerNames = names(); |
|
for (String headerName : headerNames) { |
|
Optional<V> vOptional = this.get(headerName, getValueType()); |
|
vOptional.ifPresent(v -> action.accept(headerName, v)); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
default Map<String, V> asMap() { |
|
Map<String, V> newMap = new LinkedHashMap<>(); |
|
for (Map.Entry<String, V> entry : this) { |
|
String key = entry.getKey(); |
|
newMap.put(key, entry.getValue()); |
|
} |
|
return newMap; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default <KT, VT> Map<KT, VT> asMap(Class<KT> keyType, Class<VT> valueType) { |
|
Map<KT, VT> newMap = new LinkedHashMap<>(); |
|
for (Map.Entry<String, V> entry : this) { |
|
String key = entry.getKey(); |
|
Optional<KT> convertedKey = ConversionService.SHARED.convert(key, keyType); |
|
if (convertedKey.isPresent()) { |
|
Optional<VT> convertedValue = ConversionService.SHARED.convert(entry.getValue(), valueType); |
|
convertedValue.ifPresent(vt -> newMap.put(convertedKey.get(), vt)); |
|
} |
|
} |
|
return newMap; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default Properties asProperties() { |
|
Properties props = new Properties(); |
|
|
|
for (Map.Entry<String, V> entry : this) { |
|
String key = entry.getKey(); |
|
V value = entry.getValue(); |
|
if (value instanceof CharSequence || value instanceof Number) { |
|
props.setProperty(key, value.toString()); |
|
} |
|
} |
|
return props; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
default Map<String, V> subMap(String prefix, Class<V> valueType) { |
|
return subMap(prefix, Argument.of(valueType)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
default Map<String, V> subMap(String prefix, Argument<V> valueType) { |
|
return subMap(prefix, ConversionContext.of(valueType)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
default Map<String, V> subMap(String prefix, ArgumentConversionContext<V> valueType) { |
|
|
|
String finalPrefix = prefix + '.'; |
|
return names().stream() |
|
.filter(name -> name.startsWith(finalPrefix)) |
|
.collect(Collectors.toMap((name) -> name.substring(finalPrefix.length()), (name) -> get(name, valueType).orElse(null))); |
|
} |
|
|
|
@SuppressWarnings("NullableProblems") |
|
@Override |
|
default Iterator<Map.Entry<String, V>> iterator() { |
|
Iterator<String> names = names().iterator(); |
|
return new Iterator<Map.Entry<String, V>>() { |
|
@Override |
|
public boolean hasNext() { |
|
return names.hasNext(); |
|
} |
|
|
|
@Override |
|
public Map.Entry<String, V> next() { |
|
if (!hasNext()) { |
|
throw new NoSuchElementException(); |
|
} |
|
|
|
String name = names.next(); |
|
return new Map.Entry<String, V>() { |
|
@Override |
|
public String getKey() { |
|
return name; |
|
} |
|
|
|
@Override |
|
public V getValue() { |
|
return get(name, getValueType()).orElse(null); |
|
} |
|
|
|
@Override |
|
public V setValue(V value) { |
|
throw new UnsupportedOperationException("Not mutable"); |
|
} |
|
}; |
|
} |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static <T> ConvertibleValues<T> of(Map<? extends CharSequence, T> values) { |
|
if (values == null) { |
|
return ConvertibleValuesMap.empty(); |
|
} else { |
|
return new ConvertibleValuesMap<>(values); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
static <V> ConvertibleValues<V> empty() { |
|
return ConvertibleValues.EMPTY; |
|
} |
|
} |
|
|