001package io.dinject.controller; 002 003import java.math.BigDecimal; 004import java.time.LocalDate; 005import java.time.LocalDateTime; 006import java.time.LocalTime; 007import java.util.UUID; 008 009/** 010 * Helper type conversion methods. 011 * <p/> 012 * These methods are intended to be used by APT source generators. 013 */ 014public class PathTypeConversion { 015 016 /** 017 * Convert to int. 018 */ 019 public static int asInt(String value) { 020 return Integer.valueOf(value); 021 } 022 023 /** 024 * Convert to long. 025 */ 026 public static long asLong(String value) { 027 return Long.valueOf(value); 028 } 029 030 /** 031 * Convert to double. 032 */ 033 public static double asDouble(String value) { 034 return Double.valueOf(value); 035 } 036 037 /** 038 * Convert to float. 039 */ 040 public static float asFloat(String value) { 041 return Float.valueOf(value); 042 } 043 044 /** 045 * Convert to boolean. 046 */ 047 public static boolean asBool(String value) { 048 return Boolean.valueOf(value); 049 } 050 051 /** 052 * Convert to BigDecimal (not nullable). 053 */ 054 public static BigDecimal asDecimal(String value) { 055 return new BigDecimal(value); 056 } 057 058 /** 059 * Convert to LocalDate (not nullable). 060 */ 061 public static LocalDate asDate(String value) { 062 return LocalDate.parse(value); 063 } 064 065 /** 066 * Convert to LocalTime (not nullable). 067 */ 068 public static LocalTime asTime(String value) { 069 return LocalTime.parse(value); 070 } 071 072 /** 073 * Convert to LocalDateTime (not nullable). 074 */ 075 public static LocalDateTime asDateTime(String value) { 076 return LocalDateTime.parse(value); 077 } 078 079 /** 080 * Convert to UUID (not nullable). 081 */ 082 public static UUID asUUID(String value) { 083 return UUID.fromString(value); 084 } 085 086 /** 087 * Convert to Integer (not nullable). 088 */ 089 public static Integer asInteger(String value) { 090 return Integer.valueOf(value); 091 } 092 093 /** 094 * Convert to Integer (allowing nulls). 095 */ 096 public static Integer toInteger(String value) { 097 if (isNullOrEmpty(value)) { 098 return null; 099 } 100 return Integer.valueOf(value); 101 } 102 103 /** 104 * Convert to Long (allowing nulls). 105 */ 106 public static Long toLong(String value) { 107 if (isNullOrEmpty(value)) { 108 return null; 109 } 110 return Long.valueOf(value); 111 } 112 113 /** 114 * Convert to Boolean (allowing nulls). 115 */ 116 public static Boolean toBoolean(String value) { 117 if (isNullOrEmpty(value)) { 118 return null; 119 } 120 return Boolean.valueOf(value); 121 } 122 123 /** 124 * Convert to UUID (allowing nulls). 125 */ 126 public static UUID toUUID(String value) { 127 if (isNullOrEmpty(value)) { 128 return null; 129 } 130 return UUID.fromString(value); 131 } 132 133 /** 134 * Convert to LocalDate (allowing nulls). 135 */ 136 public static LocalDate toLocalDate(String value) { 137 if (isNullOrEmpty(value)) { 138 return null; 139 } 140 return LocalDate.parse(value); 141 } 142 143 /** 144 * Convert to LocalTime (allowing nulls). 145 */ 146 public static LocalTime toTime(String value) { 147 if (isNullOrEmpty(value)) { 148 return null; 149 } 150 return LocalTime.parse(value); 151 } 152 153 /** 154 * Convert to LocalDateTime (allowing nulls). 155 */ 156 public static LocalDateTime toDateTime(String value) { 157 if (isNullOrEmpty(value)) { 158 return null; 159 } 160 return LocalDateTime.parse(value); 161 } 162 163 private static boolean isNullOrEmpty(String value) { 164 return value == null || value.isEmpty(); 165 } 166 167}