|
| 1 | +/****************************************************************************************** |
| 2 | + * This file is part of the Apex UUID project, released under the MIT License. * |
| 3 | + * See LICENSE file or go to https://github.com/jongpie/ApexUuid for full license details. * |
| 4 | + ******************************************************************************************/ |
| 5 | +public without sharing class Uuid { |
| 6 | + private static final Integer HEX_BASE = HEX_CHARACTERS.length(); |
| 7 | + private static final String HEX_CHARACTERS = '0123456789abcdef'; |
| 8 | + private static final String HEX_PREFIX = '0x'; |
| 9 | + private static final List<String> HEX_CHARACTER_LIST = HEX_CHARACTERS.split( |
| 10 | + '' |
| 11 | + ); |
| 12 | + private static final Integer UUID_V4_LENGTH = 36; |
| 13 | + private static final String UUID_V4_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}'; |
| 14 | + |
| 15 | + public static String formatValue(String unformattedValue) { |
| 16 | + final String invalidValueError = |
| 17 | + unformattedValue + ' is not a valid UUID value'; |
| 18 | + |
| 19 | + // Remove any non-alphanumeric characters |
| 20 | + unformattedValue = unformattedValue.replaceAll('[^a-zA-Z0-9]', ''); |
| 21 | + |
| 22 | + // If the unformatted value isn't even the right length to be valid, then throw an exception |
| 23 | + // Subtract 4 because the UUID_V4_LENGTH includes 4 '-' characters in the UUID pattern |
| 24 | + if (unformattedValue.length() != (UUID_V4_LENGTH - 4)) |
| 25 | + throw new UuidException(invalidValueError); |
| 26 | + |
| 27 | + // UUID Pattern: 8-4-4-4-12 |
| 28 | + String formattedValue = |
| 29 | + unformattedValue.substring(0, 8) + |
| 30 | + '-' + |
| 31 | + unformattedValue.substring(8, 12) + |
| 32 | + '-' + |
| 33 | + unformattedValue.substring(12, 16) + |
| 34 | + '-' + |
| 35 | + unformattedValue.substring(16, 20) + |
| 36 | + '-' + |
| 37 | + unformattedValue.substring(20); |
| 38 | + |
| 39 | + formattedValue = formattedValue.toLowerCase(); |
| 40 | + |
| 41 | + if (!Uuid.isValid(formattedValue)) |
| 42 | + throw new UuidException(invalidValueError); |
| 43 | + |
| 44 | + return formattedValue; |
| 45 | + } |
| 46 | + |
| 47 | + public static Boolean isValid(String uuidValue) { |
| 48 | + if (String.isBlank(uuidValue)) |
| 49 | + return false; |
| 50 | + if (uuidValue.length() != UUID_V4_LENGTH) |
| 51 | + return false; |
| 52 | + |
| 53 | + Pattern uuidPattern = Pattern.compile(UUID_V4_REGEX.toLowerCase()); |
| 54 | + Matcher uuidMatcher = uuidPattern.matcher(uuidValue.toLowerCase()); |
| 55 | + |
| 56 | + return uuidMatcher.matches(); |
| 57 | + } |
| 58 | + |
| 59 | + public static Uuid valueOf(String uuidValue) { |
| 60 | + return new Uuid(uuidValue); |
| 61 | + } |
| 62 | + |
| 63 | + private final String value; |
| 64 | + |
| 65 | + public Uuid() { |
| 66 | + this.value = this.generateValue(); |
| 67 | + } |
| 68 | + |
| 69 | + private Uuid(String uuidValue) { |
| 70 | + this.value = Uuid.formatValue(uuidValue); |
| 71 | + } |
| 72 | + |
| 73 | + public String getValue() { |
| 74 | + return this.value; |
| 75 | + } |
| 76 | + |
| 77 | + private String generateValue() { |
| 78 | + String hexValue = EncodingUtil.convertToHex(Crypto.generateAesKey(128)); |
| 79 | + |
| 80 | + // Version Calculation: (i & 0x0f) | 0x40 |
| 81 | + // Version Format: Always begins with 4 |
| 82 | + String versionShiftedHexBits = this.getShiftedHexBits( |
| 83 | + hexValue.substring(14, 16), |
| 84 | + this.convertHexToInteger('0x0f'), |
| 85 | + this.convertHexToInteger('0x40') |
| 86 | + ); |
| 87 | + |
| 88 | + // Variant Calculation: (i & 0x3f) | 0x80 |
| 89 | + // Variant Format: Always begins with 8, 9, A or B |
| 90 | + String variantShiftedHexBits = this.getShiftedHexBits( |
| 91 | + hexValue.substring(18, 20), |
| 92 | + this.convertHexToInteger('0x3f'), |
| 93 | + this.convertHexToInteger('0x80') |
| 94 | + ); |
| 95 | + |
| 96 | + String uuidValue = |
| 97 | + hexValue.substring(0, 8) + // time-low |
| 98 | + hexValue.substring(8, 12) + // time-mid |
| 99 | + versionShiftedHexBits + |
| 100 | + hexValue.substring(14, 16) + // time-high-and-version |
| 101 | + variantShiftedHexBits + |
| 102 | + hexValue.substring(18, 20) + // clock-seq-and-reserved + clock-seq-low |
| 103 | + hexValue.substring(20); // node |
| 104 | + |
| 105 | + return Uuid.formatValue(uuidValue); |
| 106 | + } |
| 107 | + |
| 108 | + private String getShiftedHexBits( |
| 109 | + String hexSubstring, |
| 110 | + Integer lowerThreshold, |
| 111 | + Integer upperThreshold |
| 112 | + ) { |
| 113 | + Integer shiftedIntegerBits = |
| 114 | + (this.convertHexToInteger(hexSubstring) & lowerThreshold) | |
| 115 | + upperThreshold; |
| 116 | + return this.convertIntegerToHex(shiftedIntegerBits); |
| 117 | + } |
| 118 | + |
| 119 | + private Integer convertHexToInteger(String hexValue) { |
| 120 | + hexValue = hexValue.toLowerCase(); |
| 121 | + |
| 122 | + if (hexValue.startsWith(HEX_PREFIX)) |
| 123 | + hexValue = hexValue.substringAfter(HEX_PREFIX); |
| 124 | + |
| 125 | + Integer integerValue = 0; |
| 126 | + for (String hexCharacter : hexValue.split('')) { |
| 127 | + Integer hexCharacterIndex = HEX_CHARACTERS.indexOf(hexCharacter); |
| 128 | + |
| 129 | + integerValue = HEX_BASE * integerValue + hexCharacterIndex; |
| 130 | + } |
| 131 | + return integerValue; |
| 132 | + } |
| 133 | + |
| 134 | + private String convertIntegerToHex(Integer integerValue) { |
| 135 | + String hexValue = ''; |
| 136 | + while (integerValue > 0) { |
| 137 | + Integer hexCharacterIndex = Math.mod(integerValue, HEX_BASE); |
| 138 | + |
| 139 | + hexValue = HEX_CHARACTER_LIST[hexCharacterIndex] + hexValue; |
| 140 | + integerValue = integerValue / HEX_BASE; |
| 141 | + } |
| 142 | + return hexValue; |
| 143 | + } |
| 144 | + |
| 145 | + private class UuidException extends Exception { |
| 146 | + } |
| 147 | +} |
0 commit comments