KeyValueResource.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package org.ssssssss.magicapi.adapter.resource;
  2. import org.ssssssss.magicapi.adapter.Resource;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.*;
  6. import java.util.function.Function;
  7. import java.util.stream.Collectors;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipOutputStream;
  10. public abstract class KeyValueResource implements Resource {
  11. protected String separator;
  12. protected String path;
  13. protected KeyValueResource parent;
  14. protected boolean readonly = false;
  15. public KeyValueResource(String separator, String path, KeyValueResource parent) {
  16. this.separator = separator;
  17. this.path = path;
  18. this.parent = parent;
  19. }
  20. public KeyValueResource(String separator, String path, boolean readonly, KeyValueResource parent) {
  21. this.separator = separator;
  22. this.path = path;
  23. this.parent = parent;
  24. this.readonly = readonly;
  25. }
  26. @Override
  27. public String separator() {
  28. return this.separator;
  29. }
  30. @Override
  31. public boolean isDirectory() {
  32. return this.path.endsWith(separator);
  33. }
  34. @Override
  35. public final boolean renameTo(Resource resource) {
  36. if(resource.name().equalsIgnoreCase(this.name())){
  37. return true;
  38. }
  39. if (!(resource instanceof KeyValueResource)) {
  40. throw new IllegalArgumentException("无法将" + this.getAbsolutePath() + "重命名为:" + resource.getAbsolutePath());
  41. }
  42. KeyValueResource targetResource = (KeyValueResource) resource;
  43. // 判断移动的是否是文件夹
  44. if (resource.isDirectory()) {
  45. Set<String> oldKeys = this.keys();
  46. Map<String, String> mappings = new HashMap<>(oldKeys.size());
  47. int keyLen = this.path.length();
  48. oldKeys.forEach(oldKey -> mappings.put(oldKey, targetResource.path + oldKey.substring(keyLen)));
  49. return renameTo(mappings);
  50. } else {
  51. return renameTo(Collections.singletonMap(this.path, targetResource.path));
  52. }
  53. }
  54. @Override
  55. public boolean delete() {
  56. return this.keys().stream().allMatch(this::deleteByKey);
  57. }
  58. protected boolean deleteByKey(String key) {
  59. return false;
  60. }
  61. /**
  62. * 需要做修改的key,原key: 新key
  63. */
  64. protected abstract boolean renameTo(Map<String, String> renameKeys);
  65. @Override
  66. public String name() {
  67. String name = this.path;
  68. if (isDirectory()) {
  69. name = this.path.substring(0, name.length() - 1);
  70. }
  71. int index = name.lastIndexOf(separator);
  72. return index > -1 ? name.substring(index + 1) : name;
  73. }
  74. @Override
  75. public Resource getResource(String name) {
  76. name = (isDirectory() ? this.path : this.path + separator) + name;
  77. return mappedFunction().apply(name);
  78. }
  79. @Override
  80. public Resource getDirectory(String name) {
  81. return getResource(name + separator);
  82. }
  83. @Override
  84. public boolean mkdir() {
  85. if (!isDirectory()) {
  86. this.path += separator;
  87. }
  88. return write("this is directory");
  89. }
  90. @Override
  91. public Resource parent() {
  92. return this.parent;
  93. }
  94. @Override
  95. public boolean write(byte[] bytes) {
  96. return write(new String(bytes, StandardCharsets.UTF_8));
  97. }
  98. @Override
  99. public List<Resource> resources() {
  100. return keys().stream().map(mappedFunction()).collect(Collectors.toList());
  101. }
  102. protected abstract Function<String, Resource> mappedFunction();
  103. protected abstract Set<String> keys();
  104. @Override
  105. public List<Resource> dirs() {
  106. return resources().stream().filter(Resource::isDirectory).collect(Collectors.toList());
  107. }
  108. @Override
  109. public List<Resource> files(String suffix) {
  110. return resources().stream().filter(it -> it.name().endsWith(suffix)).collect(Collectors.toList());
  111. }
  112. @Override
  113. public String getAbsolutePath() {
  114. return this.path;
  115. }
  116. }