class RectangleUtils {

    public static PDRectangle rotate(int degrees, PDRectangle oldRectangle, PDRectangle mediaBox) {
        while(degrees < 0) {
            degrees = 360 + degrees;
        }

        switch (degrees) {
            case 0:
                return oldRectangle;
            case 90:
                return new PDRectangle(oldRectangle.getLowerLeftY(),
                        mediaBox.getWidth() - oldRectangle.getLowerLeftX() - oldRectangle.getWidth(),
                        oldRectangle.getHeight(), oldRectangle.getWidth()
                );
            case 180:
                return new PDRectangle(mediaBox.getWidth() - oldRectangle.getLowerLeftX() - oldRectangle.getWidth(),
                        mediaBox.getHeight() - oldRectangle.getLowerLeftY() - oldRectangle.getHeight(),
                        oldRectangle.getWidth(), oldRectangle.getHeight()
                );
            case 270:
                return new PDRectangle(mediaBox.getHeight() - oldRectangle.getLowerLeftY() - oldRectangle.getHeight(),
                        oldRectangle.getLowerLeftX(),
                        oldRectangle.getHeight(), oldRectangle.getWidth()
                );
            default:
                throw new RuntimeException("Cannot rotate rectangle by degrees: " + degrees);
        }
    }

}