|
| 1 | +package org.simplejavamail.internal.util; |
| 2 | + |
| 3 | +import javax.annotation.Nullable; |
| 4 | + |
| 5 | +import static org.simplejavamail.internal.util.Preconditions.assumeTrue; |
| 6 | + |
| 7 | +public class SimpleConversions { |
| 8 | + |
| 9 | + @Nullable |
| 10 | + public static String convertToString(@Nullable final Object value) { |
| 11 | + return value != null ? value.toString() : null; |
| 12 | + } |
| 13 | + |
| 14 | + @Nullable |
| 15 | + public static Integer convertToInteger(@Nullable final Object value) { |
| 16 | + if (value == null || value instanceof Integer) { |
| 17 | + return (Integer) value; |
| 18 | + } else if (value instanceof Number) { |
| 19 | + return ((Number) value).intValue(); |
| 20 | + } else if (value instanceof Boolean) { |
| 21 | + return ((Boolean) value) ? 1 : 0; |
| 22 | + } else { |
| 23 | + assumeTrue(value instanceof String, "Wrong property must have been requested"); |
| 24 | + return Integer.valueOf((String) value); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + @Nullable |
| 29 | + public static Boolean convertToBoolean(@Nullable final Object value) { |
| 30 | + if (value == null || value instanceof Boolean) { |
| 31 | + return (Boolean) value; |
| 32 | + } else if (value instanceof Number) { |
| 33 | + return ((Number) value).intValue() != 0; |
| 34 | + } else { |
| 35 | + assumeTrue(value instanceof String, "Wrong property must have been requested"); |
| 36 | + String strValue = (String) value; |
| 37 | + if (strValue.equalsIgnoreCase("true") || strValue.equalsIgnoreCase("1")) { |
| 38 | + return true; |
| 39 | + } else if (strValue.equalsIgnoreCase("false") || strValue.equalsIgnoreCase("0")) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + throw new AssertionError("Wrong property must have been requested"); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments