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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.util;
  21. import com.google.common.base.Splitter;
  22. import com.google.common.collect.Lists;
  23. import com.google.common.primitives.Ints;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.sonar.api.utils.DateUtils;
  26. import javax.annotation.CheckForNull;
  27. import javax.annotation.Nullable;
  28. import java.util.Date;
  29. import java.util.List;
  30. /**
  31. * @since 3.6
  32. */
  33. public class RubyUtils {
  34. private RubyUtils() {
  35. // only static methods
  36. }
  37. @CheckForNull
  38. public static List<String> toStrings(@Nullable Object o) {
  39. List<String> result = null;
  40. if (o != null) {
  41. if (o instanceof List) {
  42. // assume that it contains only strings
  43. result = (List) o;
  44. } else if (o instanceof CharSequence) {
  45. result = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().split((CharSequence) o));
  46. }
  47. }
  48. return result;
  49. }
  50. @CheckForNull
  51. public static <E extends Enum<E>> List<E> toEnums(@Nullable Object o, Class<E> enumClass) {
  52. if (o == null) {
  53. return null;
  54. }
  55. List<E> result = Lists.newArrayList();
  56. if (o instanceof List) {
  57. for (String s : (List<String>) o) {
  58. result.add(Enum.valueOf(enumClass, s));
  59. }
  60. } else if (o instanceof CharSequence) {
  61. for (String s : Splitter.on(',').omitEmptyStrings().split((CharSequence) o)) {
  62. result.add(Enum.valueOf(enumClass, s));
  63. }
  64. } else {
  65. throw new IllegalArgumentException("Unsupported type: " + o.getClass());
  66. }
  67. return result;
  68. }
  69. @CheckForNull
  70. public static Integer toInteger(@Nullable Object o) {
  71. if (o == null) {
  72. return null;
  73. }
  74. if (o instanceof Integer) {
  75. return (Integer) o;
  76. }
  77. if (o instanceof Long) {
  78. return Ints.checkedCast((Long) o);
  79. }
  80. if (o instanceof String) {
  81. if (StringUtils.isBlank((String) o)) {
  82. return null;
  83. }
  84. return Integer.parseInt((String) o);
  85. }
  86. throw new IllegalArgumentException("Unsupported type for integer: " + o.getClass());
  87. }
  88. @CheckForNull
  89. public static Double toDouble(@Nullable Object o) {
  90. if (o == null) {
  91. return null;
  92. }
  93. if (o instanceof Double) {
  94. return (Double) o;
  95. }
  96. if (o instanceof Integer) {
  97. return ((Integer) o).doubleValue();
  98. }
  99. if (o instanceof Long) {
  100. return ((Long) o).doubleValue();
  101. }
  102. if (o instanceof String) {
  103. if (StringUtils.isBlank((String) o)) {
  104. return null;
  105. }
  106. return Double.parseDouble((String) o);
  107. }
  108. throw new IllegalArgumentException("Unsupported type for double: " + o.getClass());
  109. }
  110. @CheckForNull
  111. public static Date toDate(@Nullable Object o) {
  112. if (o == null) {
  113. return null;
  114. }
  115. if (o instanceof Date) {
  116. return (Date) o;
  117. }
  118. if (o instanceof String) {
  119. if (StringUtils.isBlank((String) o)) {
  120. return null;
  121. }
  122. Date date = DateUtils.parseDateTimeQuietly((String) o);
  123. if (date != null) {
  124. return date;
  125. }
  126. return DateUtils.parseDate((String) o);
  127. }
  128. throw new IllegalArgumentException("Unsupported type for date: " + o.getClass());
  129. }
  130. @CheckForNull
  131. public static Boolean toBoolean(@Nullable Object o) {
  132. if (o == null) {
  133. return null;
  134. }
  135. if (o instanceof Boolean) {
  136. return (Boolean) o;
  137. }
  138. if (o instanceof String) {
  139. if (StringUtils.isBlank((String) o)) {
  140. return null;
  141. }
  142. return Boolean.parseBoolean((String) o);
  143. }
  144. throw new IllegalArgumentException("Unsupported type for boolean: " + o.getClass());
  145. }
  146. @CheckForNull
  147. public static Long toLong(@Nullable Object o) {
  148. if (o == null) {
  149. return null;
  150. }
  151. if (o instanceof Integer) {
  152. return ((Integer) o).longValue();
  153. }
  154. if (o instanceof Long) {
  155. return (Long) o;
  156. }
  157. if (o instanceof String) {
  158. if (StringUtils.isBlank((String) o)) {
  159. return null;
  160. }
  161. return Long.parseLong((String) o);
  162. }
  163. throw new IllegalArgumentException("Unsupported type for long: " + o.getClass());
  164. }
  165. }