write a java function that implements gray code and returns the next sequence code after a given code in a given direction of rotation
Here's a function that generates the next Gray code after the given one, in the given rotation direction:
```java
public int nextGrayCode(int code, boolean clockwise) {
    int xor = code ^ (code >> 1);
    return clockwise ? xor : xor ^ 0xAAAAAAAA;
}
```
It works by:

XORing the code with a right-shifted version of itself - this flips adjacent bits
Using the clockwise parameter to pick either the result of the XOR, or that result XORed with a pattern that flips alternating groups of 4 bits (this handles the "wraparound" case for counter-clockwise rotation)

So for example:
nextGrayCode(0, true);  // 1
nextGrayCode(1, true);  // 3
nextGrayCode(3, true);  // 2
nextGrayCode(2, true);  // 6
nextGrayCode(6, false); // 7
nextGrayCode(7, false); // 5


The function handles incrementing in either direction, and wraparounding at either end of the sequence.