A machine identifier policy defines the accepted alphabet, length and equality rule before a value enters a key or path boundary.
Java identifier input: explicit alphabets and normalized display text
Java 8+. The program uses JDK classes and requires no preview flags.
Keep display labels separate
A parcel reference used in a protocol can be restricted to uppercase ASCII letters, digits and hyphens. That narrow policy rejects visually similar characters from other scripts because they are outside the declared alphabet. It is appropriate for this reference field, not a rule for every person’s name.
Unicode normalization does not itself solve look-alike identifiers. Two characters can look similar while remaining different after normalization. Conversely, a compatibility normalization can merge distinctions the application intended to preserve. Decide whether the field is a human label or a protocol key before applying transformations.
The validator rejects lowercase input instead of silently changing it. A different service can choose uppercase canonicalization, but it must apply the same rule on creation and lookup and check collisions under that rule. This example also rejects an empty value, an overlong value and separators at the ends.
Validate before publishing
The function returns the accepted value without touching shared state. If validation fails, the caller has no partially registered reference to undo. That order matters when registration also writes an index, audit record or filesystem path. Accepted syntax still needs authorization and uniqueness checks in the service using it.
Working program
public class ParcelReferencePolicy {
static String accept(String reference){
if(reference==null||reference.length()<3||reference.length()>24)throw new IllegalArgumentException("Reference length");
if(reference.charAt(0)=='-'||reference.charAt(reference.length()-1)=='-')throw new IllegalArgumentException("Separator position");
for(int i=0;i<reference.length();i++){
char c=reference.charAt(i);if(!((c>='A'&&c<='Z')||(c>='0'&&c<='9')||c=='-'))throw new IllegalArgumentException("Reference alphabet");
}return reference;
}
public static void main(String[] args){
System.out.println(accept("PK-204"));
for(String rejected:new String[]{"pk-204","PK‑204","-PK"})
try{accept(rejected);}catch(IllegalArgumentException invalid){System.out.println("rejected");}
}
}Output
PK-204
rejected
rejected
rejectedCosts and boundaries
Validation is O(n) over at most 24 UTF-16 units and uses constant auxiliary storage. The strict alphabet is a field-specific decision. It does not prove that an accepted reference belongs to the current user, is unique, or is safe to concatenate into an arbitrary external command.
Common Mistakes
- Normalization is not a complete confusable-character policy.
- Do not apply a protocol alphabet to international display names.
- Syntax validation does not replace authorization.
