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;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.OutputStream;
  22. import java.io.OutputStreamWriter;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.ArrayList;
  26. import java.util.Collection;
  27. import java.util.List;
  28. import java.util.Optional;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import com.esotericsoftware.yamlbeans.YamlReader;
  32. import com.esotericsoftware.yamlbeans.YamlWriter;
  33. import static com.dmdirc.util.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. /** The charset to use when reading and writing files. */
  40. private static final String CHARSET = "UTF-8";
  41. /** Logger to use. */
  42. private static final Logger LOG = LoggerFactory.getLogger(BaseYamlStore.class);
  43. /**
  44. * Reads a list of items from the YAML file at the specified path.
  45. *
  46. * <p>Each item is converted from an object into some form of model by invoking the
  47. * {@link #convertFromYaml} method.
  48. *
  49. * @param path The path of the file to read.
  50. * @return A list of successfully converted model objects.
  51. */
  52. protected List<T> read(final Path path) {
  53. final List<T> results = new ArrayList<>();
  54. if (Files.exists(path)) {
  55. try (final InputStream stream = Files.newInputStream(path);
  56. final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
  57. final YamlReader yamlReader = new YamlReader(reader);
  58. results.addAll(asList(yamlReader.read(), this::convertFromYaml));
  59. yamlReader.close();
  60. } catch (IOException | IllegalArgumentException ex) {
  61. LOG.warn("Unable to read from {}", path, ex);
  62. }
  63. }
  64. return results;
  65. }
  66. /**
  67. * Writes a collection of items to a YAML file at the specified path.
  68. *
  69. * <p>Each item is converted into a "plain" object by invoking the {@link #convertToYaml}
  70. * method.
  71. *
  72. * @param path The path of the file to write.
  73. * @param items A collection of items to write.
  74. */
  75. protected void write(final Path path, final Collection<T> items) {
  76. try (final OutputStream stream = Files.newOutputStream(path);
  77. final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
  78. final YamlWriter yamlWriter = new YamlWriter(writer);
  79. yamlWriter.write(items.parallelStream().map(this::convertToYaml).collect(toList()));
  80. yamlWriter.close();
  81. } catch (IOException ex) {
  82. LOG.error(LogUtils.USER_ERROR, "Unable to write to {}", path, ex);
  83. }
  84. }
  85. protected abstract Optional<T> convertFromYaml(final Object object);
  86. protected abstract Object convertToYaml(final T object);
  87. }