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.

YamlReaderUtils.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 java.util.List;
  19. import java.util.Map;
  20. import java.util.Optional;
  21. import java.util.function.Function;
  22. import java.util.stream.Collectors;
  23. import javax.annotation.Nullable;
  24. /**
  25. * Provides utility methods for classes that read from YAML files.
  26. */
  27. public final class YamlReaderUtils {
  28. private YamlReaderUtils() {
  29. // Shouldn't be instantiated
  30. }
  31. /**
  32. * Performs an unchecked cast from a wildcard map to an object map. This will always work, but
  33. * generates an unchecked warning anyway. This method allows the scope of the warning
  34. * suppression to be reduced just to the single line.
  35. *
  36. * @param map The wildcard map to be cast.
  37. * @return A usable object-to-object map.
  38. */
  39. @SuppressWarnings("unchecked")
  40. private static Map<Object, Object> uncheckedCast(final Map<?, ?> map) {
  41. return (Map<Object, Object>) map;
  42. }
  43. /**
  44. * Performs an unchecked cast from a wildcard list to an object list. This will always work, but
  45. * generates an unchecked warning anyway. This method allows the scope of the warning
  46. * suppression to be reduced just to the single line.
  47. *
  48. * @param list The wildcard list to be cast.
  49. * @return A usable object list.
  50. */
  51. @SuppressWarnings("unchecked")
  52. private static <T> List<T> uncheckedCast(final List<?> list) {
  53. return (List<T>) list;
  54. }
  55. /**
  56. * Checks that the specified object is a map, and casts it.
  57. *
  58. * @param object The object to be cast.
  59. * @return The given object cast as a map.
  60. * @throws IllegalArgumentException If the specified object is not a map.
  61. */
  62. public static Map<Object, Object> asMap(@Nullable final Object object) throws IllegalArgumentException {
  63. if (object instanceof Map) {
  64. return uncheckedCast((Map<?, ?>) object);
  65. }
  66. throw new IllegalArgumentException("Unexpected element. Found " + simpleName(object) + ", expected Map");
  67. }
  68. /**
  69. * Checks that the specified object is a list, and casts it.
  70. *
  71. * @param object The object to be cast.
  72. * @return The given object cast as a list.
  73. * @throws IllegalArgumentException If the specified object is not a list.
  74. */
  75. public static List<Object> asList(@Nullable final Object object) throws IllegalArgumentException {
  76. if (object instanceof List) {
  77. return uncheckedCast((List<?>) object);
  78. }
  79. throw new IllegalArgumentException("Unexpected element. Found " + simpleName(object) + ", expected List");
  80. }
  81. /**
  82. * Checks that the specified object is a list, and casts it.
  83. *
  84. * @param object The object to be cast.
  85. * @param converter The function to use to convert each list member to the desired type.
  86. * @return The given object cast as a list.
  87. * @throws IllegalArgumentException If the specified object is not a list.
  88. */
  89. public static <T> List<T> asList(@Nullable final Object object, final Function<Object, Optional<T>> converter)
  90. throws IllegalArgumentException {
  91. return asList(object).parallelStream()
  92. .map(converter)
  93. .filter(Optional::isPresent)
  94. .map(Optional::get)
  95. .collect(Collectors.toList());
  96. }
  97. /**
  98. * Validates and returns a required string value from the given map.
  99. *
  100. * @param map The map to retrieve the entry from.
  101. * @param key The key of the entry to retrieve.
  102. * @return The string representation of the value of the key.
  103. * @throws IllegalArgumentException If the specified key is not present, or is empty.
  104. */
  105. public static String requiredString(final Map<Object, Object> map, final String key)
  106. throws IllegalArgumentException {
  107. if (!map.containsKey(key)) {
  108. throw new IllegalArgumentException("Required key not present: " + key);
  109. }
  110. final String value = map.get(key).toString();
  111. if (value.trim().isEmpty()) {
  112. throw new IllegalArgumentException("Required key is empty: " + key);
  113. }
  114. return value;
  115. }
  116. /**
  117. * Returns an optional string value from the given map.
  118. *
  119. * @param map The map to retrieve the entry from.
  120. * @param key The key of the entry to retrieve.
  121. * @return The string value of the entry with the given key, or {@code null} if not present.
  122. */
  123. @Nullable
  124. public static String optionalString(final Map<Object, Object> map, final String key) {
  125. final Object value = map.get(key);
  126. return value == null ? null : value.toString();
  127. }
  128. /**
  129. * Gets the simple name of the given object's class.
  130. *
  131. * @param object The object to get the class name of
  132. * @return The simple class name of the object, or the string {@code "null"}.
  133. */
  134. private static String simpleName(@Nullable final Object object) {
  135. return object == null ? "null" : object.getClass().getSimpleName();
  136. }
  137. }