Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@

import lombok.experimental.UtilityClass;

import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static io.github.selcukes.databind.utils.StringHelper.isNullOrEmpty;

@UtilityClass
public class CollectionUtils {
Expand All @@ -34,20 +32,6 @@ public String[][] toArray(List<List<String>> cells) {
.toArray(String[][]::new);
}

public Map<String, String> toMap(List<String> keys, List<String> values) {
return IntStream.range(0, keys.size()).boxed()
.filter(i -> !isNullOrEmpty(keys.get(i))).collect(Collectors.toMap(keys::get, values::get));
}

public Map<String, String> toMap(Properties properties) {
return properties.entrySet().stream().collect(
Collectors.toMap(
entry -> String.valueOf(entry.getKey()),
entry -> String.valueOf(entry.getValue()),
(prev, next) -> next, LinkedHashMap::new
));
}

public List<String> trim(List<String> list) {
return list.stream()
.map(String::trim)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* Copyright (c) Ramesh Babu Prudhvi.
* Copyright (c) Ramesh Babu Prudhvi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.selcukes.commons.tests;

import io.github.selcukes.commons.helper.DateHelper;
import io.github.selcukes.commons.logging.Logger;
import io.github.selcukes.commons.logging.LoggerFactory;
import io.github.selcukes.commons.properties.LinkedProperties;
import io.github.selcukes.commons.properties.PropertiesMapper;
import io.github.selcukes.commons.properties.SelcukesTestProperties;
import io.github.selcukes.databind.properties.PropertiesMapper;
import io.github.selcukes.databind.properties.LinkedProperties;
import org.testng.annotations.Test;

import java.nio.file.Path;
Expand All @@ -38,15 +38,15 @@ public class PropertiesTest {
public void createPropertyFileTest() {
Map<String, String> data = Map.of("userName", "QA",
"Time", DateHelper.get().timeStamp());
Path filePath = Path.of("target/temp.properties");
Path filePath = Path.of(PROPS_FILE);
PropertiesMapper.write(filePath.toAbsolutePath().toString(), data);
assertTrue(filePath.toFile().exists());
}

@Test(dependsOnMethods = "createPropertyFileTest")
public void updatePropertyFileTest() {
PropertiesMapper.updateProperty(PROPS_FILE, "userName", "Master");
final Map<String, String> propertiesMap = PropertiesMapper.readAsMap(PROPS_FILE);
final Map<String, String> propertiesMap = PropertiesMapper.parse(PROPS_FILE);
assertEquals(propertiesMap.get("userName"), "Master");
propertiesMap.forEach((k, v) -> logger.info(() -> String.format("Key :[%s] Value :[%s]", k, v)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.selcukes.commons.tests.helper;

import io.github.selcukes.commons.helper.CollectionUtils;
import io.github.selcukes.databind.utils.Maps;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -48,9 +49,10 @@ public void enumTest() {

@Test
public void mapTest() {
Map<String,String> stringMap=CollectionUtils.toMap(headers,values);
Map<String, String> stringMap = Maps.of(headers, values);
Assert.assertEquals(stringMap.get("WAITING"), "4");
}

@Test
public void trimTest() {
Assert.assertEquals(CollectionUtils.trim(values).get(5), "6");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) Ramesh Babu Prudhvi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.selcukes.databind;

import io.github.selcukes.databind.annotation.Key;
import io.github.selcukes.databind.converters.Converter;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Optional;

import static io.github.selcukes.databind.utils.Reflections.newInstance;
import static io.github.selcukes.databind.utils.Reflections.setField;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;

public class DataField<T> {
private final Field field;
@Getter
private final Converter<T> converter;
private final List<Converter<T>> defaultConverters;
@Setter
private T convertedValue;

public DataField(
final Field field,
final List<Converter<T>> defaultConverters
) {
this.field = field;
this.defaultConverters = defaultConverters;
this.converter = findMatchingConverter();

}

public <R> void assignValue(final R instance) {
ofNullable(convertedValue)
.ifPresent(value -> setField(instance, getFieldName(), value));
}

public String getFieldName() {
return field.getName();
}

public Type getFieldType() {
return field.getType();
}

public Optional<Key> getColumn() {
return ofNullable(field.getDeclaredAnnotation(Key.class));
}

@SuppressWarnings("unchecked")
private Converter<T> findMatchingConverter() {
return getColumn()
.map(Key::converter)
.map(converterClass -> (Converter<T>) newInstance(converterClass))
.filter(converterInstance -> converterInstance.getType().equals(getFieldType()))
.orElseGet(() -> defaultConverters.stream()
.filter(converterInstance -> converterInstance.getType().equals(getFieldType()))
.findFirst()
.orElseThrow(() -> new IllegalStateException(format(
"There's no matching converter found for %s field of type %s", getFieldName(), getFieldType()))
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
public @interface Key {
String name();

String format() default "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Ramesh Babu Prudhvi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.selcukes.databind.converters;

import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.github.selcukes.databind.utils.Reflections.newInstance;

@UtilityClass
public class Converters {
@SuppressWarnings("unchecked")
public static <T> List<Converter<T>> defaultConverters() {
return Stream.of(
BooleanConverter.class,
StringConverter.class,
IntegerConverter.class,
DoubleConverter.class,
LocalDateConverter.class,
LocalDateTimeConverter.class
)
.map(cls -> (Converter<T>) newInstance(cls))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,97 +16,54 @@

package io.github.selcukes.databind.excel;

import io.github.selcukes.databind.annotation.Column;
import io.github.selcukes.databind.annotation.Key;
import io.github.selcukes.databind.converters.Converter;
import io.github.selcukes.databind.DataField;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;


import static io.github.selcukes.databind.utils.Reflections.newInstance;
import static io.github.selcukes.databind.utils.Reflections.setField;
import static io.github.selcukes.databind.utils.StringHelper.toFieldName;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;
import static org.apache.poi.ss.usermodel.Row.MissingCellPolicy.RETURN_BLANK_AS_NULL;


class ExcelCell<T> {
class ExcelCell<T> extends DataField<T> {
private final int index;
private final Field field;
private final Converter<T> converter;
private final DataFormatter formatter;
private final List<Converter<T>> defaultIConverters;

private T convertedValue;

public ExcelCell(
final Field field,
final Map<String, Integer> headers,
final List<Converter<T>> defaultIConverters
final List<Converter<T>> defaultConverters
) {
this.field = field;
this.defaultIConverters = defaultIConverters;
super(field, defaultConverters);
this.index = getIndex(headers);
this.converter = findMatchingConverter();
this.formatter = new DataFormatter();
}

public ExcelCell<T> parse(final Row row) {
var cellValue = ofNullable(row.getCell(index, RETURN_BLANK_AS_NULL))
.map(cell -> formatter.formatCellValue(cell).trim())
.orElse("");
this.convertedValue = converter.convert(cellValue, getColumn().map(Column::format).orElse(""));
setConvertedValue(getConverter().convert(cellValue, getColumn().map(Key::format).orElse("")));
return this;
}

private int getIndex(Map<String, Integer> headers) {
String header = getColumn()
.map(Column::name)
.map(Key::name)
.orElse(toFieldName(getFieldName()));
Map<String, Integer> headersMap = new TreeMap<>(CASE_INSENSITIVE_ORDER);
headersMap.putAll(headers);
return ofNullable(headersMap.get(header))
.orElseThrow(() -> new IllegalArgumentException(format("Column %s not found", field.getName())));
}

public <R> void assignValue(final R instance) {
ofNullable(convertedValue)
.ifPresent(value -> setField(instance, getFieldName(), value));
}

private String getFieldName() {
return field.getName();
}

private Type getFieldType() {
return field.getType();
}

private Optional<Column> getColumn() {
return ofNullable(field.getDeclaredAnnotation(Column.class));
}

@SuppressWarnings("unchecked")
private Converter<T> findMatchingConverter() {
return getColumn()
.map(Column::converter)
.map(converterClass -> (Converter<T>) newInstance(converterClass))
.filter(converterInstance -> converterInstance.getType().equals(getFieldType()))
.orElseGet(() -> defaultIConverters.stream()
.filter(converterInstance -> converterInstance.getType().equals(getFieldType()))
.findFirst()
.orElseThrow(() -> new IllegalStateException(format(
"There's no matching converter found for %s field of type %s", getFieldName(), getFieldType()))
)
);
.orElseThrow(() -> new IllegalArgumentException(format("Column %s not found", getFieldName())));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
* limitations under the License.
*/

package io.github.selcukes.databind;
package io.github.selcukes.databind.excel;

import io.github.selcukes.databind.exception.DataMapperException;
import io.github.selcukes.databind.utils.DataFileHelper;
import io.github.selcukes.databind.excel.ExcelParser;
import lombok.experimental.UtilityClass;

import java.util.stream.Stream;
Expand Down
Loading