Skip to content

Commit

Permalink
Formatting and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
urbim committed Sep 9, 2019
1 parent de029ac commit 9621821
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 179 deletions.
48 changes: 20 additions & 28 deletions src/main/java/com/kumuluz/ee/config/microprofile/ConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,22 @@
* out of or in connection with the software or the use or other dealings in the
* software. See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package com.kumuluz.ee.config.microprofile;

import com.kumuluz.ee.config.microprofile.converters.ImplicitConverter;
import com.kumuluz.ee.config.microprofile.utils.AlternativeTypesUtil;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;

import com.kumuluz.ee.config.microprofile.converters.ImplicitConverter;
import com.kumuluz.ee.config.microprofile.utils.AlternativeTypesUtil;

/**
* Microprofile Config implementation that exposes KumuluzEE configuration framework.
*
Expand All @@ -48,13 +42,11 @@
* @since 1.1
*/
public class ConfigImpl implements Config, Serializable {
private static final Logger logger = Logger.getLogger(ConfigImpl.class.getCanonicalName());

private static final String ARRAY_SEPARATOR_REGEX = "(?<!\\\\)" + Pattern.quote(",");
private Map<Type, Converter> converters;
private List<ConfigSource> configSources;

private static final String ARRAY_SEPARATOR_REGEX = "(?<!\\\\)" + Pattern.quote(",");

public ConfigImpl(List<ConfigSource> configSources, Map<Type, Converter> converters) {
this.configSources = configSources;
this.converters = converters;
Expand Down Expand Up @@ -111,7 +103,7 @@ public <T> T convert(String value, Class<T> asType) {
Array.set(arr, i, a.get(i));
}

return (T)arr;
return (T) arr;
}

Converter<T> converter = getConverter(asType);
Expand All @@ -120,7 +112,7 @@ public <T> T convert(String value, Class<T> asType) {

public <T> List<T> convertList(String value, Class<T> listType) {

String[] tokens = value.split(ARRAY_SEPARATOR_REGEX);
String[] tokens = value.split(ARRAY_SEPARATOR_REGEX);

Converter<T> converter = getConverter(listType);
List<T> convertedList = new ArrayList<>(tokens.length);
Expand Down Expand Up @@ -152,13 +144,13 @@ private <T> Converter<T> getConverter(Class asType) {
return converter;
}

@Override
public Iterable<String> getPropertyNames() {
return this.configSources.stream().flatMap(e -> e.getPropertyNames().stream()).collect(Collectors.toSet());
}
@Override
public Iterable<String> getPropertyNames() {
return this.configSources.stream().flatMap(e -> e.getPropertyNames().stream()).collect(Collectors.toSet());
}

@Override
public Iterable<ConfigSource> getConfigSources() {
return this.configSources;
}
}
@Override
public Iterable<ConfigSource> getConfigSources() {
return this.configSources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
*/
package com.kumuluz.ee.config.microprofile.adapters;

import com.kumuluz.ee.configuration.ConfigurationSource;
import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.microprofile.config.spi.ConfigSource;

import com.kumuluz.ee.configuration.ConfigurationSource;

/**
* Adapts KumuluzEE configuration framework {@link ConfigurationSource} to MicroProfile Config {@link ConfigSource}.
*
Expand All @@ -37,6 +36,7 @@
* @since 1.3
*/
public class ConfigSourceAdapter implements ConfigSource {

private ConfigurationSource configurationSource;

public ConfigSourceAdapter(ConfigurationSource configurationSource) {
Expand All @@ -56,21 +56,21 @@ public Map<String, String> getProperties() {
public int getOrdinal() {
return configurationSource.getOrdinal();
}

@Override
public String getValue(String s) {
String val = configurationSource.get(s).orElse(null);
if (val != null) {
Optional<Integer> listSize = this.configurationSource.getListSize(s);
//this is a list or an array
if (listSize.isPresent()) {
//we ignore the returned value and build the array
return buildArray(s, listSize.get());
}
}
String val = configurationSource.get(s).orElse(null);

if (val != null) {
Optional<Integer> listSize = this.configurationSource.getListSize(s);

//this is a list or an array
if (listSize.isPresent()) {
//we ignore the returned value and build the array
return buildArray(s, listSize.get());
}
}

return val;
}

Expand Down Expand Up @@ -106,28 +106,23 @@ private void buildPropertiesMap(Map<String, String> map, String prefix) {
}
}
}

private String buildArray(String propertyName, int size) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++) {
String prefix = String.format("%s[%d]", propertyName, i);
Optional<List<String>> objectKeys = this.configurationSource.getMapKeys(prefix);

//array item is an object, so we just omit it
if (objectKeys.isPresent()) {
sb.append("");
}
else {
Optional<String> item = this.configurationSource.get(String.format("%s[%d]", propertyName, i));
item.ifPresent(sb::append);
}

if (i < size - 1) {
sb.append(',');
}
}

return sb.toString();
}

private String buildArray(String propertyName, int size) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++) {
String prefix = String.format("%s[%d]", propertyName, i);
Optional<List<String>> objectKeys = this.configurationSource.getMapKeys(prefix);

if (!objectKeys.isPresent()) {
Optional<String> item = this.configurationSource.get(String.format("%s[%d]", propertyName, i));
if (i > 0) {
sb.append(',');
}
item.ifPresent(sb::append);
} // else array item is an object, so we just omit it
}

return sb.toString();
}
}
Loading

0 comments on commit 9621821

Please sign in to comment.