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.3KB

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