Z*p Generator (Cyclic Group) in java
Well, if $g$ is a generator of $\bmod\  p$ for prime $p$; that is, if all values in the range $[1, p-1]$ are possible values for $g^i \bmod p$, then we have $g^a \neq 1 \bmod p$ for any $a = (p-1)/r$ where $r$ is a prime factor of $p-1$.

You select $p$ to be a "safe prime", that is $p-1 = 2 \times q$ where $q$ is also a prime.  This implies that, in this case, there are only two values of $a$ that are of interest:


$a = q$ (with $r = 2$), this implies the condition that $g^q \ne 1 \bmod p$
$a = 2$ (with $r = q$), this implies the condition that $g^2 \ne 1 \bmod p$


Both conditions must be true, however your code tests only the first condition; it doesn't test the second.  Now, it turns out that the only possible values of $g$ that do not meet this second condition is $g=1$ (which doesn't pass the first either), and $g=p-1$, or in your case, $g=10$.

Because you're not testing this second condition, you're seeing $g=10$ as looking like a generator when it is not.

Now, $p-1$ is the only value which is excluded by this second condition, in practice it's probably easy just to test values in the range $[2, p-2]$, and not bother explicitly testing this second condition.