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 private static void checkNull(String value) { 017 if (value == null) { 018 throw new InvalidPathArgumentException("path element is null"); 019 } 020 } 021 022 /** 023 * Convert to int. 024 */ 025 public static int asInt(String value) { 026 checkNull(value); 027 try { 028 return Integer.valueOf(value); 029 } catch (NumberFormatException e) { 030 throw new InvalidPathArgumentException(e); 031 } 032 } 033 034 /** 035 * Convert to long. 036 */ 037 public static long asLong(String value) { 038 checkNull(value); 039 try { 040 return Long.valueOf(value); 041 } catch (NumberFormatException e) { 042 throw new InvalidPathArgumentException(e); 043 } 044 } 045 046 /** 047 * Convert to double. 048 */ 049 public static double asDouble(String value) { 050 checkNull(value); 051 try { 052 return Double.valueOf(value); 053 } catch (RuntimeException e) { 054 throw new InvalidPathArgumentException(e); 055 } 056 } 057 058 /** 059 * Convert to float. 060 */ 061 public static float asFloat(String value) { 062 checkNull(value); 063 try { 064 return Float.valueOf(value); 065 } catch (RuntimeException e) { 066 throw new InvalidPathArgumentException(e); 067 } 068 } 069 070 /** 071 * Convert to boolean. 072 */ 073 public static boolean asBool(String value) { 074 checkNull(value); 075 return Boolean.valueOf(value); 076 } 077 078 /** 079 * Convert to BigDecimal (not nullable). 080 */ 081 public static BigDecimal asDecimal(String value) { 082 checkNull(value); 083 try { 084 return new BigDecimal(value); 085 } catch (RuntimeException e) { 086 throw new InvalidPathArgumentException(e); 087 } 088 } 089 090 /** 091 * Convert to LocalDate (not nullable). 092 */ 093 public static LocalDate asDate(String value) { 094 checkNull(value); 095 try { 096 return LocalDate.parse(value); 097 } catch (RuntimeException e) { 098 throw new InvalidPathArgumentException(e); 099 } 100 } 101 102 /** 103 * Convert to LocalTime (not nullable). 104 */ 105 public static LocalTime asTime(String value) { 106 checkNull(value); 107 try { 108 return LocalTime.parse(value); 109 } catch (RuntimeException e) { 110 throw new InvalidPathArgumentException(e); 111 } 112 } 113 114 /** 115 * Convert to LocalDateTime (not nullable). 116 */ 117 public static LocalDateTime asDateTime(String value) { 118 checkNull(value); 119 try { 120 return LocalDateTime.parse(value); 121 } catch (RuntimeException e) { 122 throw new InvalidPathArgumentException(e); 123 } 124 } 125 126 /** 127 * Convert to UUID (not nullable). 128 */ 129 public static UUID asUUID(String value) { 130 checkNull(value); 131 try { 132 return UUID.fromString(value); 133 } catch (RuntimeException e) { 134 throw new InvalidPathArgumentException(e); 135 } 136 } 137 138 /** 139 * Convert to Integer (not nullable). 140 */ 141 public static Integer asInteger(String value) { 142 checkNull(value); 143 try { 144 return Integer.valueOf(value); 145 } catch (NumberFormatException e) { 146 throw new InvalidPathArgumentException(e); 147 } 148 } 149 150 /** 151 * Convert to Integer (allowing nulls). 152 */ 153 public static Integer toInteger(String value) { 154 if (isNullOrEmpty(value)) { 155 return null; 156 } 157 try { 158 return Integer.valueOf(value); 159 } catch (NumberFormatException e) { 160 throw new InvalidPathArgumentException(e); 161 } 162 } 163 164 /** 165 * Convert to Long (allowing nulls). 166 */ 167 public static Long toLong(String value) { 168 if (isNullOrEmpty(value)) { 169 return null; 170 } 171 try { 172 return Long.valueOf(value); 173 } catch (NumberFormatException e) { 174 throw new InvalidPathArgumentException(e); 175 } 176 } 177 178 /** 179 * Convert to Boolean (allowing nulls). 180 */ 181 public static Boolean toBoolean(String value) { 182 if (isNullOrEmpty(value)) { 183 return null; 184 } 185 return Boolean.valueOf(value); 186 } 187 188 /** 189 * Convert to UUID (allowing nulls). 190 */ 191 public static UUID toUUID(String value) { 192 if (isNullOrEmpty(value)) { 193 return null; 194 } 195 try { 196 return UUID.fromString(value); 197 } catch (Exception e) { 198 throw new InvalidPathArgumentException(e); 199 } 200 } 201 202 /** 203 * Convert to LocalDate (allowing nulls). 204 */ 205 public static LocalDate toLocalDate(String value) { 206 if (isNullOrEmpty(value)) { 207 return null; 208 } 209 try { 210 return LocalDate.parse(value); 211 } catch (Exception e) { 212 throw new InvalidPathArgumentException(e); 213 } 214 } 215 216 /** 217 * Convert to LocalTime (allowing nulls). 218 */ 219 public static LocalTime toTime(String value) { 220 if (isNullOrEmpty(value)) { 221 return null; 222 } 223 try { 224 return LocalTime.parse(value); 225 } catch (Exception e) { 226 throw new InvalidPathArgumentException(e); 227 } 228 } 229 230 /** 231 * Convert to LocalDateTime (allowing nulls). 232 */ 233 public static LocalDateTime toDateTime(String value) { 234 if (isNullOrEmpty(value)) { 235 return null; 236 } 237 try { 238 return LocalDateTime.parse(value); 239 } catch (Exception e) { 240 throw new InvalidPathArgumentException(e); 241 } 242 } 243 244 private static boolean isNullOrEmpty(String value) { 245 return value == null || value.isEmpty(); 246 } 247 248}