SpecifiedDataStrategy.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package jnpf.base.util.result.Impl;
  2. import jnpf.base.model.dataset.DataFormModel;
  3. import jnpf.base.util.result.ResultStrategy;
  4. import org.springframework.stereotype.Component;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7. @Component
  8. public class SpecifiedDataStrategy implements ResultStrategy {
  9. @Override
  10. public String getChoice() {
  11. return "6";
  12. }
  13. @Override
  14. public List<Map<String, Object>> getResults(List<Map<String, Object>> data, DataFormModel dataFormModel) {
  15. String[] split = dataFormModel.getSpecifyData().split(",");
  16. ArrayList<Integer> integers = new ArrayList<>();
  17. for (String string : split) {
  18. String replace = string.replace(" ", "");
  19. if (string.contains("-")) {
  20. String[] strings = string.split("-");
  21. if (strings.length != 2) {
  22. throw new IllegalArgumentException("非法格式:应包含两个数字");
  23. }
  24. int start = Integer.parseInt(strings[0]);
  25. int end = Integer.parseInt(strings[1]);
  26. if (start > end) {
  27. throw new IllegalArgumentException("起始值不能大于结束值");
  28. }
  29. List<Integer> result = new ArrayList<>();
  30. for (int i = start; i <= end; i++) {
  31. result.add(i);
  32. }
  33. integers.addAll(result);
  34. continue;
  35. }
  36. int num = 0;
  37. try {
  38. num = Integer.parseInt(replace);
  39. } catch (NumberFormatException e) {
  40. return null;
  41. }
  42. if (num > 0) {
  43. integers.add(num);
  44. } else {
  45. return null;
  46. }
  47. }
  48. return integers.stream().map(i -> data.get(i - 1))
  49. .collect(Collectors.toList());
  50. }
  51. }