123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.server.util;
-
- import com.google.common.base.Splitter;
- import com.google.common.collect.Lists;
- import com.google.common.primitives.Ints;
- import org.apache.commons.lang.StringUtils;
- import org.sonar.api.utils.DateUtils;
-
- import javax.annotation.CheckForNull;
- import javax.annotation.Nullable;
- import java.util.Date;
- import java.util.List;
-
- /**
- * @since 3.6
- */
- public class RubyUtils {
-
- private RubyUtils() {
- // only static methods
- }
-
- @CheckForNull
- public static List<String> toStrings(@Nullable Object o) {
- List<String> result = null;
- if (o != null) {
- if (o instanceof List) {
- // assume that it contains only strings
- result = (List) o;
- } else if (o instanceof CharSequence) {
- result = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().split((CharSequence) o));
- }
- }
- return result;
- }
-
- @CheckForNull
- public static <E extends Enum<E>> List<E> toEnums(@Nullable Object o, Class<E> enumClass) {
- if (o == null) {
- return null;
- }
- List<E> result = Lists.newArrayList();
- if (o instanceof List) {
- for (String s : (List<String>) o) {
- result.add(Enum.valueOf(enumClass, s));
- }
- } else if (o instanceof CharSequence) {
- for (String s : Splitter.on(',').omitEmptyStrings().split((CharSequence) o)) {
- result.add(Enum.valueOf(enumClass, s));
- }
- } else {
- throw new IllegalArgumentException("Unsupported type: " + o.getClass());
- }
- return result;
- }
-
- @CheckForNull
- public static Integer toInteger(@Nullable Object o) {
- if (o == null) {
- return null;
- }
- if (o instanceof Integer) {
- return (Integer) o;
- }
- if (o instanceof Long) {
- return Ints.checkedCast((Long) o);
- }
- if (o instanceof String) {
- if (StringUtils.isBlank((String) o)) {
- return null;
- }
- return Integer.parseInt((String) o);
- }
- throw new IllegalArgumentException("Unsupported type for integer: " + o.getClass());
- }
-
- @CheckForNull
- public static Double toDouble(@Nullable Object o) {
- if (o == null) {
- return null;
- }
- if (o instanceof Double) {
- return (Double) o;
- }
- if (o instanceof Integer) {
- return ((Integer) o).doubleValue();
- }
- if (o instanceof Long) {
- return ((Long) o).doubleValue();
- }
- if (o instanceof String) {
- if (StringUtils.isBlank((String) o)) {
- return null;
- }
- return Double.parseDouble((String) o);
- }
- throw new IllegalArgumentException("Unsupported type for double: " + o.getClass());
- }
-
- @CheckForNull
- public static Date toDate(@Nullable Object o) {
- if (o == null) {
- return null;
- }
- if (o instanceof Date) {
- return (Date) o;
- }
- if (o instanceof String) {
- if (StringUtils.isBlank((String) o)) {
- return null;
- }
- Date date = DateUtils.parseDateTimeQuietly((String) o);
- if (date != null) {
- return date;
- }
- return DateUtils.parseDate((String) o);
- }
- throw new IllegalArgumentException("Unsupported type for date: " + o.getClass());
- }
-
- @CheckForNull
- public static Boolean toBoolean(@Nullable Object o) {
- if (o == null) {
- return null;
- }
- if (o instanceof Boolean) {
- return (Boolean) o;
- }
- if (o instanceof String) {
- if (StringUtils.isBlank((String) o)) {
- return null;
- }
- return Boolean.parseBoolean((String) o);
- }
- throw new IllegalArgumentException("Unsupported type for boolean: " + o.getClass());
- }
-
- @CheckForNull
- public static Long toLong(@Nullable Object o) {
- if (o == null) {
- return null;
- }
- if (o instanceof Integer) {
- return ((Integer) o).longValue();
- }
- if (o instanceof Long) {
- return (Long) o;
- }
- if (o instanceof String) {
- if (StringUtils.isBlank((String) o)) {
- return null;
- }
- return Long.parseLong((String) o);
- }
- throw new IllegalArgumentException("Unsupported type for long: " + o.getClass());
- }
- }
|