MCPcopy Create free account
hub / github.com/DiUS/java-faker / imei

Method imei

src/main/java/com/github/javafaker/Code.java:150–186  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

148 }
149
150 public String imei() {
151 char[] str = new char[15];
152 int len = str.length;
153
154 // Fill in the first two values of the string based with the specified prefix.
155 String arr = faker.options().option(REPORTING_BODY_IDENTIFIERS);
156 str[0] = arr.charAt(0);
157 str[1] = arr.charAt(1);
158
159 // Fill all the remaining numbers except for the last one with random values.
160 for (int i=2; i < len - 1; i++) {
161 str[i] = Character.forDigit(faker.number().numberBetween(0, 9), 10);
162 }
163
164 // Calculate the Luhn checksum of the values thus far
165 int lenOffset = (len + 1) % 2;
166 int t = 0;
167 int sum = 0;
168 for (int i = 0; i < len - 1; i++) {
169 if ((i + lenOffset) % 2 != 0) {
170 t = Character.getNumericValue(str[i]) * 2;
171
172 if (t > 9) {
173 t -= 9;
174 }
175
176 sum += t;
177 } else {
178 sum += Character.getNumericValue(str[i]);
179 }
180 }
181
182 // Choose the last digit so that it causes the entire string to pass the checksum.
183 str[len - 1] = Character.forDigit(((10 - (sum % 10)) % 10), 10);
184
185 return new String(str);
186 }
187
188 private static final String [] REPORTING_BODY_IDENTIFIERS
189 = {"01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};

Callers

nothing calls this directly

Calls 4

optionMethod · 0.80
optionsMethod · 0.80
numberBetweenMethod · 0.80
numberMethod · 0.80

Tested by

no test coverage detected