ArrayUtils { public static void shift2D(double[] a, int w, int h, int x, int y){ assertPositive(w, ()->"specified width is not positive. w="+w); assertPositive(h, ()->"specified height is not positive. h="+h); while(x < 0) x = w+x; while(y < 0) y = h+y; x %=w; y %=h; if(x==0 && y==0){ return; } for(int row = 0; row < h; row++){ int offset = row*w; rotateArray(a, w, offset, x); } rotateArray(a,w*h, 0, y*w); } private  ArrayUtils(); static void shift2D(double[] a, int w, int h, int x, int y); static void rotateArray(double[] a, final int size, final int offset, int distance); static void assertPositive(int x, Supplier<String> errmsg); static void assertArraySize(int size, double[] array, Supplier<String> errmsg); static void scaleArray(final double[] array, final double factor);  }
@Test public void testShift2D(){ for(int w = 1; w < 16; w++) for(int h = 1; h < 16; h++) testShift2Dwith(w, h); }