189 * 190 * @return A {@code UUID} with the specified value 191 * 192 * @throws IllegalArgumentException 193 * If name does not conform to the string representation as 194 * described in {@link #toString} 195 * 196 */ 197 public static UUID fromString(String name) { 198 int len = name.length(); 199 if (len > 36) { 200 throw new IllegalArgumentException("UUID string too large"); 201 } 202 203 int dash1 = name.indexOf('-', 0); 204 int dash2 = name.indexOf('-', dash1 + 1); 205 int dash3 = name.indexOf('-', dash2 + 1); 206 int dash4 = name.indexOf('-', dash3 + 1); 207 int dash5 = name.indexOf('-', dash4 + 1); 208 209 // For any valid input, dash1 through dash4 will be positive and dash5 210 // negative, but it's enough to check dash4 and dash5: 211 // - if dash1 is -1, dash4 will be -1 212 // - if dash1 is positive but dash2 is -1, dash4 will be -1 213 // - if dash1 and dash2 is positive, dash3 will be -1, dash4 will be 214 // positive, but so will dash5 215 if (dash4 < 0 || dash5 >= 0) { 216 throw new IllegalArgumentException("Invalid UUID string: " + name); 217 } 218 219 long mostSigBits = Long.parseLong(name, 0, dash1, 16) & 0xffffffffL; 220 mostSigBits <<= 16; 221 mostSigBits |= Long.parseLong(name, dash1 + 1, dash2, 16) & 0xffffL; 222 mostSigBits <<= 16; 223 mostSigBits |= Long.parseLong(name, dash2 + 1, dash3, 16) & 0xffffL; 224 long leastSigBits = Long.parseLong(name, dash3 + 1, dash4, 16) & 0xffffL; 225 leastSigBits <<= 48; 226 leastSigBits |= Long.parseLong(name, dash4 + 1, len, 16) & 0xffffffffffffL; 227 228 return new UUID(mostSigBits, leastSigBits); | 189 * 190 * @return A {@code UUID} with the specified value 191 * 192 * @throws IllegalArgumentException 193 * If name does not conform to the string representation as 194 * described in {@link #toString} 195 * 196 */ 197 public static UUID fromString(String name) { 198 int len = name.length(); 199 if (len > 36) { 200 throw new IllegalArgumentException("UUID string too large"); 201 } 202 203 int dash1 = name.indexOf('-', 0); 204 int dash2 = name.indexOf('-', dash1 + 1); 205 int dash3 = name.indexOf('-', dash2 + 1); 206 int dash4 = name.indexOf('-', dash3 + 1); 207 int dash5 = name.indexOf('-', dash4 + 1); 208 209 int len1 = dash1; 210 int len2 = dash2 - dash1 - 1; 211 int len3 = dash3 - dash2 - 1; 212 int len4 = dash4 - dash3 - 1; 213 int len5 = len - dash4 - 1; 214 215 if (len1 > 8 || len2 > 4 || len3 > 4 || len4 > 4 || len5 > 12) { 216 throw new IllegalArgumentException("Invalid UUID string: " + name); 217 } 218 219 // For any valid input, dash1 through dash4 will be positive and dash5 220 // negative, but it's enough to check dash4 and dash5: 221 // - if dash1 is -1, dash4 will be -1 222 // - if dash1 is positive but dash2 is -1, dash4 will be -1 223 // - if dash1 and dash2 is positive, dash3 will be -1, dash4 will be 224 // positive, but so will dash5 225 if (dash4 < 0 || dash5 >= 0) { 226 throw new IllegalArgumentException("Invalid UUID string: " + name); 227 } 228 229 long mostSigBits = Long.parseLong(name, 0, dash1, 16) & 0xffffffffL; 230 mostSigBits <<= 16; 231 mostSigBits |= Long.parseLong(name, dash1 + 1, dash2, 16) & 0xffffL; 232 mostSigBits <<= 16; 233 mostSigBits |= Long.parseLong(name, dash2 + 1, dash3, 16) & 0xffffL; 234 long leastSigBits = Long.parseLong(name, dash3 + 1, dash4, 16) & 0xffffL; 235 leastSigBits <<= 48; 236 leastSigBits |= Long.parseLong(name, dash4 + 1, len, 16) & 0xffffffffffffL; 237 238 return new UUID(mostSigBits, leastSigBits); |