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 5.5KB

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