int (int usrValue, @NotNull ScaleContext ctx, @NotNull RoundingMode rm, @Nullable ParityMode pm) { if (rm != FLOOR && rm != CEIL) { throw new IllegalArgumentException("Invalid value of rounding mode: " + rm + ", only FLOOR and CEIL are supported"); } int result = usrValue; int attempts = 0; final int maxAttempts = pm == null ? 4 : 8; // should be enough if scaling is a multiple of 0.25 while (result >= 0 && isNotSuitablyAlignedToInt(result, ctx, pm)) { result += rm == FLOOR ? -1 : 1; if (++attempts > maxAttempts) { return usrValue; // unusual scaling (not a multiple of 0.25) } } return result; }