Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CsvConverter
- public class CsvConverter {
- public static void convert(String file, List<Order> out) {
- BlockingQueue<String> data = new LinkedBlockingQueue<>();
- ReadService.readFile("src/main/resources/" + file, data);
- CsvMapper mapper = new CsvMapper();
- CsvSchema schema = CsvSchema.builder()
- .addColumn("orderId")
- .addColumn("amount")
- .addColumn("currency")
- .addColumn("comment")
- .build();
- data.parallelStream().forEachOrdered(s -> {
- try {
- out.add((Order) mapper
- .readerFor(Order.class)
- .with(schema)
- .readValues(s).nextValue());
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- }
- }
- //JsonConverter
- public class JsonConverter {
- public static void convert(String file, List<Order> out) {
- BlockingQueue<String> data = new LinkedBlockingQueue<>();
- ReadService.readFile("src/main/resources/" + file, data);
- ObjectMapper mapper = new ObjectMapper();
- data.parallelStream().forEachOrdered(s -> {
- try {
- out.add(mapper.readValue(s, Order.class));
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- }
- });
- }
- }
- //ReadService
- public class ReadService {
- public static void readFile(String fileName, BlockingQueue<String> data) {
- try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
- for (String line; (line = br.readLine()) != null; ) {
- data.add(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //App
- @SpringBootApplication
- public class ParsingProjApplication {
- public static void main(String[] args) {
- SpringApplication.run(ParsingProjApplication.class, args);
- BlockingQueue<String> data = new LinkedBlockingQueue<>();
- List<String> permittedExt = new ArrayList<String>(Arrays.asList("json", "csv"));
- List<Order> res = new ArrayList<>();
- for (String arg : args) {
- String ext = null;
- try {
- ext = FilenameUtils.getExtension(arg);
- if (!permittedExt.contains(ext))
- throw new IllegalArgumentException();
- System.out.println(ext);
- } catch (IllegalArgumentException e) {
- System.out.println("File format is not correct");
- }
- if (Objects.equals(ext, "json")){
- JsonConverter.convert(arg, res);
- } else {
- CsvConverter.convert(arg, res);
- }
- }
- System.out.println(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement