001package io.dinject.controller; 002 003import java.math.BigDecimal; 004import java.time.Instant; 005import java.time.LocalDate; 006import java.time.LocalDateTime; 007import java.time.LocalTime; 008import java.time.OffsetDateTime; 009import java.util.UUID; 010 011/** 012 * Helper type conversion methods. 013 * <p/> 014 * These methods are intended to be used by APT source generators. 015 */ 016public class PathTypeConversion { 017 018 private static void checkNull(String value) { 019 if (value == null) { 020 throw new InvalidPathArgumentException("path element is null"); 021 } 022 } 023 024 /** 025 * Convert to int. 026 */ 027 public static int asInt(String value) { 028 checkNull(value); 029 try { 030 return Integer.parseInt(value); 031 } catch (NumberFormatException e) { 032 throw new InvalidPathArgumentException(e); 033 } 034 } 035 036 /** 037 * Convert to long. 038 */ 039 public static long asLong(String value) { 040 checkNull(value); 041 try { 042 return Long.parseLong(value); 043 } catch (NumberFormatException e) { 044 throw new InvalidPathArgumentException(e); 045 } 046 } 047 048 /** 049 * Convert to double. 050 */ 051 public static double asDouble(String value) { 052 checkNull(value); 053 try { 054 return Double.valueOf(value); 055 } catch (RuntimeException e) { 056 throw new InvalidPathArgumentException(e); 057 } 058 } 059 060 /** 061 * Convert to float. 062 */ 063 public static float asFloat(String value) { 064 checkNull(value); 065 try { 066 return Float.valueOf(value); 067 } catch (RuntimeException e) { 068 throw new InvalidPathArgumentException(e); 069 } 070 } 071 072 /** 073 * Convert to boolean. 074 */ 075 public static boolean asBoolean(String value) { 076 checkNull(value); 077 return Boolean.parseBoolean(value); 078 } 079 080 /** 081 * Convert to boolean. 082 */ 083 public static boolean asBool(String value) { 084 return asBoolean(value); 085 } 086 087 /** 088 * Convert to BigDecimal (not nullable). 089 */ 090 public static BigDecimal asBigDecimal(String value) { 091 checkNull(value); 092 try { 093 return new BigDecimal(value); 094 } catch (RuntimeException e) { 095 throw new InvalidPathArgumentException(e); 096 } 097 } 098 099 /** 100 * Convert to LocalDate (not nullable). 101 */ 102 public static LocalDate asLocalDate(String value) { 103 checkNull(value); 104 try { 105 return LocalDate.parse(value); 106 } catch (RuntimeException e) { 107 throw new InvalidPathArgumentException(e); 108 } 109 } 110 111 /** 112 * Convert to LocalTime (not nullable). 113 */ 114 public static LocalTime asLocalTime(String value) { 115 checkNull(value); 116 try { 117 return LocalTime.parse(value); 118 } catch (RuntimeException e) { 119 throw new InvalidPathArgumentException(e); 120 } 121 } 122 123 /** 124 * Convert to Instant (not nullable). 125 */ 126 public static Instant asInstant(String value) { 127 checkNull(value); 128 try { 129 return Instant.parse(value); 130 } catch (RuntimeException e) { 131 throw new InvalidPathArgumentException(e); 132 } 133 } 134 135 /** 136 * Convert to OffsetDateTime (not nullable). 137 */ 138 public static OffsetDateTime asOffsetDateTime(String value) { 139 checkNull(value); 140 try { 141 return OffsetDateTime.parse(value); 142 } catch (RuntimeException e) { 143 throw new InvalidPathArgumentException(e); 144 } 145 } 146 147 /** 148 * Convert to LocalDateTime (not nullable). 149 */ 150 public static LocalDateTime asLocalDateTime(String value) { 151 checkNull(value); 152 try { 153 return LocalDateTime.parse(value); 154 } catch (RuntimeException e) { 155 throw new InvalidPathArgumentException(e); 156 } 157 } 158 159 /** 160 * Convert to UUID (not nullable). 161 */ 162 public static UUID asUUID(String value) { 163 checkNull(value); 164 try { 165 return UUID.fromString(value); 166 } catch (RuntimeException e) { 167 throw new InvalidPathArgumentException(e); 168 } 169 } 170 171 /** 172 * Convert to Integer (not nullable). 173 */ 174 public static Integer asInteger(String value) { 175 checkNull(value); 176 try { 177 return Integer.valueOf(value); 178 } catch (NumberFormatException e) { 179 throw new InvalidPathArgumentException(e); 180 } 181 } 182 183 /** 184 * Convert to Integer (allowing nulls). 185 */ 186 public static Integer toInteger(String value) { 187 if (isNullOrEmpty(value)) { 188 return null; 189 } 190 try { 191 return Integer.valueOf(value); 192 } catch (NumberFormatException e) { 193 throw new InvalidTypeArgumentException(e); 194 } 195 } 196 197 /** 198 * Convert to Long (allowing nulls). 199 */ 200 public static Long toLong(String value) { 201 if (isNullOrEmpty(value)) { 202 return null; 203 } 204 try { 205 return Long.valueOf(value); 206 } catch (NumberFormatException e) { 207 throw new InvalidTypeArgumentException(e); 208 } 209 } 210 211 /** 212 * Convert to BigDecimal (allowing nulls). 213 */ 214 public static BigDecimal toBigDecimal(String value) { 215 if (isNullOrEmpty(value)) { 216 return null; 217 } 218 try { 219 return new BigDecimal(value); 220 } catch (Exception e) { 221 throw new InvalidTypeArgumentException(e); 222 } 223 } 224 225 /** 226 * Convert to Boolean (allowing nulls). 227 */ 228 public static Boolean toBoolean(String value) { 229 if (isNullOrEmpty(value)) { 230 return null; 231 } 232 return Boolean.valueOf(value); 233 } 234 235 /** 236 * Convert to UUID (allowing nulls). 237 */ 238 public static UUID toUUID(String value) { 239 if (isNullOrEmpty(value)) { 240 return null; 241 } 242 try { 243 return UUID.fromString(value); 244 } catch (Exception e) { 245 throw new InvalidTypeArgumentException(e); 246 } 247 } 248 249 /** 250 * Convert to LocalDate (allowing nulls). 251 */ 252 public static LocalDate toLocalDate(String value) { 253 if (isNullOrEmpty(value)) { 254 return null; 255 } 256 try { 257 return LocalDate.parse(value); 258 } catch (Exception e) { 259 throw new InvalidTypeArgumentException(e); 260 } 261 } 262 263 /** 264 * Convert to LocalTime (allowing nulls). 265 */ 266 public static LocalTime toLocalTime(String value) { 267 if (isNullOrEmpty(value)) { 268 return null; 269 } 270 try { 271 return LocalTime.parse(value); 272 } catch (Exception e) { 273 throw new InvalidTypeArgumentException(e); 274 } 275 } 276 277 /** 278 * Convert to Instant (allowing nulls). 279 */ 280 public static Instant toInstant(String value) { 281 if (isNullOrEmpty(value)) { 282 return null; 283 } 284 try { 285 return Instant.parse(value); 286 } catch (Exception e) { 287 throw new InvalidTypeArgumentException(e); 288 } 289 } 290 291 /** 292 * Convert to OffsetDateTime (allowing nulls). 293 */ 294 public static OffsetDateTime toOffsetDateTime(String value) { 295 if (isNullOrEmpty(value)) { 296 return null; 297 } 298 try { 299 return OffsetDateTime.parse(value); 300 } catch (Exception e) { 301 throw new InvalidTypeArgumentException(e); 302 } 303 } 304 305 /** 306 * Convert to LocalDateTime (allowing nulls). 307 */ 308 public static LocalDateTime toLocalDateTime(String value) { 309 if (isNullOrEmpty(value)) { 310 return null; 311 } 312 try { 313 return LocalDateTime.parse(value); 314 } catch (Exception e) { 315 throw new InvalidTypeArgumentException(e); 316 } 317 } 318 319 private static boolean isNullOrEmpty(String value) { 320 return value == null || value.isEmpty(); 321 } 322 323}