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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.function.Function;
  27. import java.util.stream.Collectors;
  28. import javax.annotation.Nullable;
  29. import static com.google.common.base.Preconditions.checkNotNull;
  30. /**
  31. * Provides utility methods for classes that read from YAML files.
  32. */
  33. public final class YamlReaderUtils {
  34. private YamlReaderUtils() {
  35. // Shouldn't be instantiated
  36. }
  37. /**
  38. * Performs an unchecked cast from a wildcard map to an object map. This will always work, but
  39. * generates an unchecked warning anyway. This method allows the scope of the warning
  40. * suppression to be reduced just to the single line.
  41. *
  42. * @param map The wildcard map to be cast.
  43. *
  44. * @return A usable object-to-object map.
  45. */
  46. @SuppressWarnings("unchecked")
  47. private static Map<Object, Object> uncheckedCast(final Map<?, ?> map) {
  48. return (Map<Object, Object>) map;
  49. }
  50. /**
  51. * Performs an unchecked cast from a wildcard list to an object list. This will always work, but
  52. * generates an unchecked warning anyway. This method allows the scope of the warning
  53. * suppression to be reduced just to the single line.
  54. *
  55. * @param list The wildcard list to be cast.
  56. *
  57. * @return A usable object list.
  58. */
  59. @SuppressWarnings("unchecked")
  60. private static <T> List<T> uncheckedCast(final List<?> list) {
  61. return (List<T>) list;
  62. }
  63. /**
  64. * Checks that the specified object is a map, and casts it.
  65. *
  66. * @param object The object to be cast.
  67. *
  68. * @return The given object cast as a map.
  69. *
  70. * @throws IllegalArgumentException If the specified object is not a map.
  71. */
  72. public static Map<Object, Object> asMap(@Nullable final Object object)
  73. throws IllegalArgumentException {
  74. if (object instanceof Map) {
  75. return uncheckedCast((Map<?, ?>) object);
  76. }
  77. throw new IllegalArgumentException("Unexpected element. Found "
  78. + simpleName(object) + ", expected Map");
  79. }
  80. /**
  81. * Checks that the specified object is a list, and casts it.
  82. *
  83. * @param object The object to be cast.
  84. *
  85. * @return The given object cast as a list.
  86. *
  87. * @throws IllegalArgumentException If the specified object is not a list.
  88. */
  89. public static List<Object> asList(@Nullable final Object object)
  90. throws IllegalArgumentException {
  91. if (object instanceof List) {
  92. return uncheckedCast((List<?>) object);
  93. }
  94. throw new IllegalArgumentException("Unexpected element. Found "
  95. + simpleName(object) + ", expected List");
  96. }
  97. /**
  98. * Checks that the specified object is a list, and casts it.
  99. *
  100. * @param object The object to be cast.
  101. * @param convertor The function to use to convert each list member to the desired type.
  102. *
  103. * @return The given object cast as a list.
  104. *
  105. * @throws IllegalArgumentException If the specified object is not a list.
  106. */
  107. public static <T> List<T> asList(@Nullable final Object object,
  108. final Function<Object, Optional<T>> convertor) throws IllegalArgumentException {
  109. return asList(object).parallelStream()
  110. .map(convertor).filter(Optional::isPresent)
  111. .map(Optional::get).collect(Collectors.toList());
  112. }
  113. /**
  114. * Validates and returns a required string value from the given map.
  115. *
  116. * @param map The map to retrieve the entry from.
  117. * @param key The key of the entry to retrieve.
  118. *
  119. * @return The string representation of the value of the key.
  120. *
  121. * @throws IllegalArgumentException If the specified key is not present, or is empty.
  122. */
  123. public static String requiredString(final Map<Object, Object> map, final String key)
  124. throws IllegalArgumentException {
  125. checkNotNull(map);
  126. checkNotNull(key);
  127. if (!map.containsKey(key)) {
  128. throw new IllegalArgumentException("Required key not present: " + key);
  129. }
  130. final String value = map.get(key).toString();
  131. if (value.trim().isEmpty()) {
  132. throw new IllegalArgumentException("Required key is empty: " + key);
  133. }
  134. return value;
  135. }
  136. /**
  137. * Returns an optional string value from the given map.
  138. *
  139. * @param map The map to retrieve the entry from.
  140. * @param key The key of the entry to retrieve.
  141. *
  142. * @return The string value of the entry with the given key, or {@code null} if not present.
  143. */
  144. @Nullable
  145. public static String optionalString(final Map<Object, Object> map, final String key) {
  146. checkNotNull(map);
  147. checkNotNull(key);
  148. final Object value = map.get(key);
  149. return value == null ? null : value.toString();
  150. }
  151. /**
  152. * Gets the simple name of the given object's class.
  153. *
  154. * @param object The object to get the class name of
  155. *
  156. * @return The simple class name of the object, or the string {@code "null"}.
  157. */
  158. private static String simpleName(@Nullable final Object object) {
  159. return object == null ? "null" : object.getClass().getSimpleName();
  160. }
  161. }