TestClass.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package thyyxxk.webserver.utils;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.Data;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.concurrent.ConcurrentHashMap;
  11. import java.util.function.Consumer;
  12. /**
  13. * 物品领用的格式化
  14. * */
  15. public class TestClass {
  16. private static final Map<String, Consumer<String>> FUNC_MAP = new ConcurrentHashMap<>();
  17. private static final String MAN = "man";
  18. private static final String WOMAN = "woman";
  19. private static final String NONE = "";
  20. static {
  21. FUNC_MAP.put(MAN, person -> System.out.println(person + "是男人。"));
  22. FUNC_MAP.put(WOMAN, person -> System.out.println(person + "是女人。"));
  23. FUNC_MAP.put(NONE, person -> System.out.println(person + "没有性别。"));
  24. }
  25. @Data
  26. static class Person {
  27. private String gender;
  28. private String name;
  29. public Person(String gender, String name) {
  30. this.gender = null == gender ? "" : gender;
  31. this.name = name;
  32. }
  33. }
  34. public static void main(String[] args) {
  35. Person p = new Person(MAN, "张三");
  36. Person p2 = new Person(WOMAN, "李四");
  37. Person p3 = new Person(null, "王五");
  38. FUNC_MAP.get(p.getGender()).accept(p.getName());
  39. FUNC_MAP.get(p2.getGender()).accept(p2.getName());
  40. FUNC_MAP.get(p3.getGender()).accept(p3.getName());
  41. }
  42. // public static void main(String[] args) {
  43. // String ori = readFile("D:\\yyhc.txt");
  44. // JSONArray arr = JSONArray.parseArray(ori);
  45. // Map<String, JSONObject> map = new HashMap<>();
  46. // for (int i = 0; i < arr.size(); i++) {
  47. // JSONObject itm = arr.getJSONObject(i);
  48. // if (!map.containsKey(itm.getString("code"))) {
  49. // JSONArray sizes = new JSONArray();
  50. // JSONObject size = new JSONObject();
  51. // size.put("val", itm.getString("size"));
  52. // sizes.add(size);
  53. // itm.replace("size", sizes);
  54. // map.put(itm.getString("code"), itm);
  55. // } else {
  56. // JSONObject size = new JSONObject();
  57. // size.put("val", itm.getString("size"));
  58. // map.get(itm.getString("code")).getJSONArray("size").add(size);
  59. // }
  60. // }
  61. //
  62. // JSONArray dest = new JSONArray();
  63. // for (Map.Entry<String, JSONObject> entry : map.entrySet()) {
  64. // dest.add(entry.getValue());
  65. // }
  66. // System.out.println(dest);
  67. // }
  68. //
  69. // public static String readFile(String path) {
  70. // StringBuilder result = new StringBuilder();
  71. // try {
  72. // BufferedReader br = new BufferedReader(new FileReader(new File(path)));
  73. // String temp;
  74. // while ((temp = br.readLine()) != null) {
  75. // result.append(temp);
  76. // }
  77. // } catch (Exception e) {
  78. // e.printStackTrace();
  79. // }
  80. // return result.toString();
  81. // }
  82. }