You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseYamlStore.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io.yaml;
  18. import com.esotericsoftware.yamlbeans.YamlReader;
  19. import com.esotericsoftware.yamlbeans.YamlWriter;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.OutputStream;
  24. import java.io.OutputStreamWriter;
  25. import java.nio.file.Files;
  26. import java.nio.file.Path;
  27. import java.util.ArrayList;
  28. import java.util.Collection;
  29. import java.util.List;
  30. import java.util.Optional;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import static com.dmdirc.util.io.yaml.YamlReaderUtils.asList;
  34. import static java.util.stream.Collectors.toList;
  35. /**
  36. * Base class for a store that is backed by a YAML file.
  37. */
  38. public abstract class BaseYamlStore<T> {
  39. /**
  40. * The charset to use when reading and writing files.
  41. */
  42. private static final String CHARSET = "UTF-8";
  43. /**
  44. * Logger to use.
  45. */
  46. private static final Logger LOG = LoggerFactory.getLogger(BaseYamlStore.class);
  47. /**
  48. * Reads a list of items from the YAML file at the specified path.
  49. * <p>
  50. * Each item is converted from an object into some form of model by invoking the {@link #convertFromYaml} method.
  51. *
  52. * @param path The path of the file to read.
  53. * @return A list of successfully converted model objects.
  54. */
  55. protected List<T> read(final Path path) {
  56. final List<T> results = new ArrayList<>();
  57. if (Files.exists(path)) {
  58. try (final InputStream stream = Files.newInputStream(path);
  59. final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
  60. final YamlReader yamlReader = new YamlReader(reader);
  61. results.addAll(asList(yamlReader.read(), this::convertFromYaml));
  62. yamlReader.close();
  63. } catch (IOException | IllegalArgumentException ex) {
  64. LOG.warn("Unable to read from {}", path, ex);
  65. }
  66. }
  67. return results;
  68. }
  69. /**
  70. * Writes a collection of items to a YAML file at the specified path.
  71. * <p>
  72. * Each item is converted into a "plain" object by invoking the {@link #convertToYaml} method.
  73. *
  74. * @param path The path of the file to write.
  75. * @param items A collection of items to write.
  76. */
  77. protected void write(final Path path, final Collection<T> items) {
  78. try (final OutputStream stream = Files.newOutputStream(path);
  79. final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
  80. final YamlWriter yamlWriter = new YamlWriter(writer);
  81. yamlWriter.write(items.parallelStream().map(this::convertToYaml).collect(toList()));
  82. yamlWriter.close();
  83. } catch (IOException ex) {
  84. LOG.warn("Unable to write to {}", path, ex);
  85. }
  86. }
  87. protected abstract Optional<T> convertFromYaml(final Object object);
  88. protected abstract Object convertToYaml(final T object);
  89. }