Sample

int-d2be199aee21-I13

Lower every eligible classic and enhanced for loop to a while loop

Matched original source

Lines160

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Scalabrino0.7370–1; higher is more readable
Dorn0.2150–1; higher is more readable
Mi0.5890–1; higher is more readable
LLOC55Logical lines; fewer is more readable
DeepSeek V4 Pro160–20; mean of three runs
GPT-6.1 Sol170–20; mean of three runs
BTTF0.4550–1; higher is more readable

Views

Source code

package org.springframework.mock.web; import java.io.IOException; import java.util.ArrayList; import java.util.List; import jakarta.servlet.AsyncContext; import jakarta.servlet.AsyncEvent; import jakarta.servlet.AsyncListener; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.jspecify.annotations.Nullable; import org.springframework.beans.BeanUtils; import org.springframework.util.Assert; import org.springframework.web.util.WebUtils; /** * Mock implementation of the {@link AsyncContext} interface. * * @author Rossen Stoyanchev * @since 3.2 */ public class MockAsyncContext implements AsyncContext { private final HttpServletRequest request; private final @Nullable HttpServletResponse response; private final List<AsyncListener> listeners = new ArrayList<>(); private @Nullable String dispatchedPath; private long timeout = 10 * 1000L; private final List<Runnable> dispatchHandlers = new ArrayList<>(); public MockAsyncContext(ServletRequest request, @Nullable ServletResponse response) { this.request = (HttpServletRequest) request; this.response = (HttpServletResponse) response; } public void addDispatchHandler(Runnable handler) { Assert.notNull(handler, "Dispatch handler must not be null"); synchronized (this) { if (this.dispatchedPath == null) { this.dispatchHandlers.add(handler); } else { handler.run(); } } } @Override public ServletRequest getRequest() { return this.request; } @Override public @Nullable ServletResponse getResponse() { return this.response; } @Override public boolean hasOriginalRequestAndResponse() { return (this.request instanceof MockHttpServletRequest && this.response instanceof MockHttpServletResponse); } @Override public void dispatch() { dispatch(this.request.getRequestURI()); } @Override public void dispatch(String path) { dispatch(null, path); } @Override public void dispatch(@Nullable ServletContext context, String path) { synchronized (this) { this.dispatchedPath = path; this.dispatchHandlers.forEach(Runnable::run); } } public @Nullable String getDispatchedPath() { return this.dispatchedPath; } @Override public void complete() { MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(this.request, MockHttpServletRequest.class); if (mockRequest != null) { mockRequest.setAsyncStarted(false); } { java.util.Iterator<?> loopIterator = (this.listeners).iterator(); while (loopIterator.hasNext()) { AsyncListener listener = (AsyncListener) loopIterator.next(); try { listener.onComplete(new AsyncEvent(this, this.request, this.response)); } catch (IOException ex) { throw new IllegalStateException("AsyncListener failure", ex); } } } } @Override public void start(Runnable runnable) { runnable.run(); } @Override public void addListener(AsyncListener listener) { this.listeners.add(listener); } @Override public void addListener(AsyncListener listener, ServletRequest request, ServletResponse response) { this.listeners.add(listener); } public List<AsyncListener> getListeners() { return this.listeners; } @Override public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException { return BeanUtils.instantiateClass(clazz); } /** * By default this is set to 10000 (10 seconds) even though the Servlet API * specifies a default async request timeout of 30 seconds. Keep in mind the * timeout could further be impacted by global configuration through the MVC * Java config or the XML namespace, as well as be overridden per request on * {@link org.springframework.web.context.request.async.DeferredResult DeferredResult} * or on * {@link org.springframework.web.servlet.mvc.method.annotation.SseEmitter SseEmitter}. * @param timeout the timeout value to use. * @see AsyncContext#setTimeout(long) */ @Override public void setTimeout(long timeout) { this.timeout = timeout; } @Override public long getTimeout() { return this.timeout; } }

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.4638.449−153.069
Source linesThe number of non-empty source lines.160+64
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.5.003−7.505

Intercept 8.87 + feature contributions = -87.704 (log-odds).
Score = sigmoid(-87.704) = 0.

Score0.737

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.161−4.664
Indentation frequencyHow rapidly indentation changes from line to line.145+4.278
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.145−2.906
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.159.874+1.825
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.109.981−0.993
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.109.957+0.97
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.084+0.126

Intercept 0.07 + feature contributions = -1.294 (log-odds).
Score = sigmoid(-1.294) = 0.215.

Score0.589
Score55
Run 1

The code is generally well-structured with clear class-level organization and consistent formatting. Methods are short and focused, following single responsibility. Imports are organized, and the Javadoc explains non-obvious timeout defaults. However, the `complete()` method has unusual manual iterator looping with an unnecessary block scope, breaking the otherwise modern style (could use for-each). The `dispatchHandlers` synchronization logic is somewhat implicit, requiring careful reading to understand the dispatch path check. Minor inconsistency exists in nullability annotations and handling. Grounds for scoring lower include the slightly awkward iterator block and synchronized blocks adding cognitive load without clear comments. Overall, the code is readable but not exemplary.

Run 2

The code is generally well-structured with clear class organization, separate methods for distinct actions, and appropriate use of JavaDoc. It maintains a flat structure in most methods. However, the 'complete()' method stands out as problematic: it uses a manually-written while loop instead of an enhanced for-loop, with unnecessary braces and an awkward 'loopIterator' variable name. This reduces readability and consistency compared to the rest of the class, which otherwise uses modern Java patterns. The constructor and field declarations are clean, but the cast of ServletRequest/Response to HttpServletRequest/Response without validation is slightly concerning. Overall, the code is readable but has one notably awkward section that could be simplified.

Run 3

The code is well-structured with clear class organization and logical method grouping. It maintains a flat nesting structure overall, with only minor deep nesting in the 'complete()' method. Intent is generally obvious, though some methods have redundant overloads. Code length is concise and appropriate. Action granularity is mostly one action per line, but the 'complete()' method could be clearer. Reading flow is natural, with methods arranged logically. Deductions come from the overly generic 'dispatch(null, path)' call which obscures intent, the unnecessarily verbose iterator block in 'complete()' where a for-each loop would be cleaner, and the redundant overloaded methods that don't add value. Overall, this is readable production-quality code with minor improvements possible.

Run 1

Code Structure: Fields, constructor, and focused interface methods are logically organized. Nesting: Mostly flat, with modest synchronization and conditionals; complete() introduces unnecessary block, loop, and try/catch nesting. Clarity of intent: Descriptive names, override annotations, nullability markers, and documentation make the mock's purpose clear, though ignored overload parameters are unexplained. Code length: Most methods are concise, but the explicit wildcard iterator and cast in complete() are unnecessarily verbose compared with an enhanced for-loop. Action granularity: Statements generally perform one action per line, supporting easy scanning. Reading flow: Dispatch overloads and simple accessors read naturally; inconsistent indentation and the extra block in complete() interrupt otherwise consistent formatting.

Run 2

Code Structure: Fields, constructor, and focused interface methods are logically organized. Nesting: Mostly shallow, though complete() adds an unnecessary scope around a loop containing try/catch. Clarity of intent: Descriptive names, annotations, and documentation make the mock's purpose clear; ignored parameters and the manually expanded iterator slightly obscure behavior. Code length: Most methods are concise, but complete() could use an enhanced for-loop instead of a wildcard iterator and cast. Action granularity: Statements generally perform one action per line, with no compressed multi-statement lines. Reading flow: Dispatch overloads delegate naturally, and accessors are easy to scan. Inconsistent indentation in complete(), fully qualified iterator usage, and extra blank lines interrupt otherwise clean flow.

Run 3

Code Structure: Fields, constructor, and focused interface methods are logically organized. Nesting: Mostly shallow, but complete() adds an unnecessary block around an iterator loop and nested try/catch. Clarity of intent: Descriptive names, annotations, and documentation make the mock's purpose clear; ignored parameters in some overloads leave minor ambiguity. Code length: Most methods are concise, though the explicit wildcard iterator and cast are unnecessarily verbose compared with an enhanced for-loop. Action granularity: Statements generally perform one action per line, supporting easy scanning. Reading flow: Dispatch overloads delegate naturally, and consistent method formatting helps navigation. However, inconsistent indentation in complete(), redundant parentheses, and the fully qualified iterator type interrupt the otherwise clean flow.