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