Skip to content

Commit d633632

Browse files
committed
Tidy code for modern Java versions
1 parent 9b95bd0 commit d633632

17 files changed

+96
-145
lines changed

src/main/java/org/jruby/rack/DefaultErrorApplication.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ static Exception getException(RackEnvironment env) {
8686
private class Response implements RackResponse {
8787

8888
private int status = 500;
89-
@SuppressWarnings("rawtypes")
90-
private Map headers = Collections.EMPTY_MAP;
89+
private Map<String, ?> headers = Collections.emptyMap();
9190
private String body;
9291

9392
protected final RackEnvironment env;
@@ -103,14 +102,13 @@ public void setStatus(int status) {
103102
this.status = status;
104103
}
105104

106-
@SuppressWarnings("rawtypes")
107-
public Map getHeaders() {
105+
public Map<String, ?> getHeaders() {
108106
return headers;
109107
}
110108

111109
@SuppressWarnings("unused")
112-
public void setHeaders(@SuppressWarnings("rawtypes") Map headers) {
113-
this.headers = headers == null ? Collections.EMPTY_MAP : headers;
110+
public void setHeaders(Map<String, ?> headers) {
111+
this.headers = headers == null ? Collections.emptyMap() : headers;
114112
}
115113

116114
public String getBody() {
@@ -161,15 +159,12 @@ private void log(RackLogger.Level level, String message, Throwable e) {
161159

162160
}
163161

164-
@SuppressWarnings("rawtypes")
165162
public static void defaultRespond(final RackResponse rackResponse,
166163
final RackResponseEnvironment responseEnv) throws IOException {
167164
responseEnv.setStatus( rackResponse.getStatus() );
168-
@SuppressWarnings("unchecked")
169-
final Set<Map.Entry> headers = rackResponse.getHeaders().entrySet();
170-
for ( Iterator<Map.Entry> it = headers.iterator(); it.hasNext(); ) {
171-
final Map.Entry entry = it.next();
172-
final String key = entry.getKey().toString();
165+
final Set<? extends Map.Entry<String, ?>> headers = rackResponse.getHeaders().entrySet();
166+
for (final Map.Entry<String, ?> entry : headers) {
167+
final String key = entry.getKey();
173168
final Object value = entry.getValue();
174169
responseEnv.addHeader(key, value != null ? value.toString() : null);
175170
}

src/main/java/org/jruby/rack/DefaultRackApplication.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,8 @@ public RackResponse call(final RackEnvironment env) {
5050
final IRubyObject app = getApplication();
5151
final Ruby runtime = getRuntime();
5252
final IRubyObject servlet_env = JavaEmbedUtils.javaToRuby(runtime, env);
53-
//try { // app.call(env) :
5453
final IRubyObject response = app.callMethod(runtime.getCurrentContext(), "call", servlet_env);
55-
return (RackResponse) response.toJava(RackResponse.class);
56-
//}
57-
//catch (RuntimeException e) {
58-
// throw ExceptionUtils.wrapException(runtime, e);
59-
//}
54+
return response.toJava(RackResponse.class);
6055
}
6156

6257
@Override

src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public IRubyObject createErrorApplicationObject(final Ruby runtime) {
199199

200200
public RackApplication newErrorApplication() {
201201
Boolean error = rackContext.getConfig().getBooleanProperty("jruby.rack.error");
202-
if ( error != null && ! error.booleanValue() ) { // jruby.rack.error = false
202+
if ( error != null && !error) { // jruby.rack.error = false
203203
return new DefaultErrorApplication(rackContext);
204204
}
205205
try {
@@ -557,7 +557,7 @@ private void configureDefaults() {
557557
if (iniSize == null) iniSize = RewindableInputStream.INI_BUFFER_SIZE;
558558
Integer maxSize = config.getMaximumMemoryBufferSize();
559559
if (maxSize == null) maxSize = RewindableInputStream.MAX_BUFFER_SIZE;
560-
if (iniSize.intValue() > maxSize.intValue()) iniSize = maxSize;
560+
if (iniSize > maxSize) iniSize = maxSize;
561561

562562
RewindableInputStream.setDefaultInitialBufferSize(iniSize);
563563
RewindableInputStream.setDefaultMaximumBufferSize(maxSize);

src/main/java/org/jruby/rack/DefaultRackConfig.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public boolean isSerialInitialization() {
123123
}
124124
}
125125
}
126-
return serial.booleanValue();
126+
return serial;
127127
}
128128

129129
@Override
@@ -156,10 +156,7 @@ protected RackLogger createLogger(final String loggerClass) {
156156
Constructor<?> ctor = klass.getConstructor(String.class);
157157
return (RackLogger) ctor.newInstance( getLoggerName() );
158158
}
159-
catch (NoSuchMethodException retry) {
160-
return newLoggerInstance(klass, retry);
161-
}
162-
catch (IllegalAccessException retry) {
159+
catch (NoSuchMethodException | IllegalAccessException retry) {
163160
return newLoggerInstance(klass, retry);
164161
}
165162
catch (InstantiationException e) {
@@ -268,7 +265,7 @@ static boolean isIgnoreRUBYOPT(RackConfig config) {
268265
// RUBYOPT ignored if jruby.runtime.env.rubyopt = false
269266
Boolean rubyopt = config.getBooleanProperty("jruby.runtime.env.rubyopt");
270267
if ( rubyopt == null ) return ! config.isIgnoreEnvironment();
271-
return rubyopt != null && ! rubyopt.booleanValue();
268+
return rubyopt != null && !rubyopt;
272269
}
273270

274271
@Override
@@ -282,11 +279,11 @@ public boolean isThrowInitException() {
282279

283280
static boolean isThrowInitException(RackConfig config) {
284281
Boolean error = config.getBooleanProperty("jruby.rack.error");
285-
if ( error != null && error.booleanValue() ) {
282+
if ( error != null && error) {
286283
return false; // jruby.rack.error = true
287284
}
288285
error = config.getBooleanProperty(RackEnvironment.EXCEPTION);
289-
if ( error != null && error.booleanValue() ) {
286+
if ( error != null && error) {
290287
return false; // jruby.rack.exception = true
291288
}
292289
return true;
@@ -335,7 +332,7 @@ private Integer getPositiveInteger(String key) {
335332
if (value == null) return null;
336333
try {
337334
int i = Integer.parseInt(value);
338-
if (i > 0) return Integer.valueOf(i);
335+
if (i > 0) return i;
339336
} catch (Exception e) { /* ignored */ }
340337
return null;
341338
}
@@ -367,12 +364,12 @@ public static Number toNumber(String value, Number defaultValue) {
367364
}
368365
if ( number == ((int) number) )
369366
if ( number > Integer.MAX_VALUE ) {
370-
return Long.valueOf((long) number);
367+
return (long) number;
371368
}
372369
else {
373-
return Integer.valueOf((int) number);
370+
return (int) number;
374371
}
375-
return Float.valueOf(number);
372+
return number;
376373
}
377374
catch (Exception e) { /* ignored */ }
378375
return defaultValue;
@@ -386,7 +383,7 @@ private Map<String, String> toStringMap(final String env) {
386383
GEM_HOME=/opt/local/rvm/gems/jruby-1.6.8@jruby-rack
387384
*/
388385
LineNumberReader reader = new LineNumberReader(new StringReader(env.trim()));
389-
Map<String, String> map = new LinkedHashMap<String, String>(); String line;
386+
Map<String, String> map = new LinkedHashMap<>(); String line;
390387
try {
391388
while ( (line = reader.readLine()) != null ) {
392389
final String[] entries = line.split(",");

src/main/java/org/jruby/rack/DefaultRackDispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected void afterException(
5757
// TODO seems redundant maybe we should let the container decide ?!
5858
context.log(ERROR, "error app failed to handle exception: " + e, re);
5959
Integer errorCode = getErrorApplicationFailureStatusCode();
60-
if ( errorCode != null && errorCode.intValue() > 0 ) {
60+
if ( errorCode != null && errorCode > 0 ) {
6161
response.sendError(errorCode);
6262
}
6363
else {

src/main/java/org/jruby/rack/PoolingRackApplicationFactory.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class PoolingRackApplicationFactory extends RackApplicationFactoryDecorat
4949
// 10 seconds seems still too much for a default, has been 30 previously :
5050
private static final float ACQUIRE_DEFAULT = 10.0f;
5151

52-
protected final Queue<RackApplication> applicationPool = new LinkedList<RackApplication>();
52+
protected final Queue<RackApplication> applicationPool = new LinkedList<>();
5353
private Integer initialSize, maximumSize;
5454

5555
private final AtomicInteger initedApplications = new AtomicInteger(0);
@@ -294,19 +294,15 @@ protected void launchInitialization(final Queue<RackApplication> apps) {
294294
if ( initThreads == null ) initThreads = 4; // quad-core baby
295295

296296
for (int i = 0; i < initThreads; i++) {
297-
new Thread(new Runnable() {
298-
@Override
299-
public void run() {
300-
while (true) {
301-
final RackApplication app;
302-
synchronized (apps) {
303-
if ( apps.isEmpty() ) break;
304-
app = apps.remove();
305-
}
306-
if ( ! initAndPutApplicationToPool(app) ) break;
297+
new Thread(() -> {
298+
while (true) {
299+
final RackApplication app;
300+
synchronized (apps) {
301+
if ( apps.isEmpty() ) break;
302+
app = apps.remove();
307303
}
304+
if ( ! initAndPutApplicationToPool(app) ) break;
308305
}
309-
310306
}, "JRuby-Rack-App-Init-" + i).start();
311307
}
312308
}
@@ -395,14 +391,14 @@ private int getInitialPoolSizeWait() {
395391
if ( waitNum != null ) {
396392
int wait = waitNum.intValue();
397393
if (maximumSize != null && wait > maximumSize) {
398-
wait = maximumSize.intValue();
394+
wait = maximumSize;
399395
}
400396
return wait;
401397
}
402398
// otherwise we assume it to be a boolean true/false flag :
403399
Boolean waitFlag = getConfig().getBooleanProperty("jruby.runtime.init.wait");
404400
if ( waitFlag == null ) waitFlag = Boolean.TRUE;
405-
return waitFlag ? ( initialSize == null ? 1 : initialSize.intValue() ) : 0;
401+
return waitFlag ? ( initialSize == null ? 1 : initialSize) : 0;
406402
// NOTE: this slightly changes the behavior in 1.1.8, in previous
407403
// versions the initialization only waited for 1 application instance
408404
// to be available in the pool - here by default we wait till initial
@@ -411,14 +407,14 @@ private int getInitialPoolSizeWait() {
411407

412408
@Override
413409
public Collection<RackApplication> getManagedApplications() {
414-
int initSize = initialSize != null ? initialSize.intValue() : -1;
410+
int initSize = initialSize != null ? initialSize : -1;
415411
synchronized (applicationPool) {
416412
if ( applicationPool.isEmpty() ) {
417413
if ( initSize > 0 ) return null; // ~ init error
418414
return Collections.emptySet();
419415
}
420416
Collection<RackApplication> snapshot =
421-
new ArrayList<RackApplication>(applicationPool);
417+
new ArrayList<>(applicationPool);
422418
return Collections.unmodifiableCollection(snapshot);
423419
}
424420
}

src/main/java/org/jruby/rack/RackApplicationFactoryDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private RackContext getContextBang() throws IllegalStateException {
204204
public static Collection<Ruby> getManagedRuntimes(RackApplicationFactoryDecorator factory) {
205205
final Collection<RackApplication> apps = factory.getManagedApplications();
206206
if ( apps == null ) return null;
207-
final Set<Ruby> runtimes = new LinkedHashSet<Ruby>(apps.size());
207+
final Set<Ruby> runtimes = new LinkedHashSet<>(apps.size());
208208
for ( RackApplication app : apps ) {
209209
runtimes.add( app.getRuntime() );
210210
}

src/main/java/org/jruby/rack/RackResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface RackResponse {
3232
/**
3333
* @return the response headers (string key names)
3434
*/
35-
Map getHeaders() ;
35+
Map<String, ?> getHeaders() ;
3636

3737
/**
3838
* Note: Normally, this method won't be used at all as we stream the

src/main/java/org/jruby/rack/UnmappedRackFilter.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
import java.io.FileNotFoundException;
1010
import java.io.IOException;
11-
import java.util.Collection;
12-
import java.util.Collections;
13-
import java.util.HashSet;
14-
import java.util.Set;
11+
import java.util.*;
12+
import java.util.stream.Collectors;
1513
import javax.servlet.FilterChain;
1614
import javax.servlet.FilterConfig;
1715
import javax.servlet.ServletException;
@@ -46,7 +44,7 @@ public class UnmappedRackFilter extends AbstractFilter {
4644

4745
private static final Collection<Integer> RESPONSE_NOT_HANDLED_STATUSES;
4846
static {
49-
final HashSet<Integer> statuses = new HashSet<Integer>(8, 1);
47+
final Set<Integer> statuses = new HashSet<>(8, 1);
5048
statuses.add( 404 );
5149
// 403 due containers not supporting PUT/DELETE correctly (Tomcat 6)
5250
statuses.add( 403 );
@@ -89,14 +87,11 @@ public void init(FilterConfig config) throws ServletException {
8987
// ResponseCapture.notHandledStatuses e.g. "403,404,500"
9088
value = config.getInitParameter("responseNotHandledStatuses");
9189
if ( value != null ) {
92-
final Set<Integer> statuses = new HashSet<Integer>();
93-
for ( String status : value.split(",") ) {
94-
status = status.trim();
95-
if ( status.length() > 0 ) {
96-
statuses.add( Integer.parseInt(status) );
97-
}
98-
}
99-
responseNotHandledStatuses = statuses;
90+
responseNotHandledStatuses = Arrays.stream(value.split(",")).
91+
map(String::trim)
92+
.filter(status -> !status.isEmpty())
93+
.map(Integer::parseInt)
94+
.collect(Collectors.toSet());
10095
}
10196
// ResponseCapture.handledByDefault true/false (true by default)
10297
value = config.getInitParameter("responseHandledByDefault");
@@ -180,7 +175,7 @@ public boolean isResetUnhandledResponse() {
180175
}
181176

182177
public void setResetUnhandledResponse(boolean reset) {
183-
this.resetUnhandledResponse = Boolean.valueOf(reset);
178+
this.resetUnhandledResponse = reset;
184179
}
185180

186181
public boolean isResetUnhandledResponseBuffer() {
@@ -204,10 +199,9 @@ public Collection<Integer> getResponseNotHandledStatuses() {
204199
return this.responseNotHandledStatuses;
205200
}
206201

207-
@SuppressWarnings("unchecked")
208202
public void setDefaultNotHandledStatuses(final Collection<Integer> responseNotHandledStatuses) {
209203
this.responseNotHandledStatuses =
210-
responseNotHandledStatuses == null ? Collections.EMPTY_SET : responseNotHandledStatuses;
204+
responseNotHandledStatuses == null ? Collections.emptySet() : responseNotHandledStatuses;
211205
}
212206

213207
public boolean isResponseHandledByDefault() {

src/main/java/org/jruby/rack/ext/Input.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ public class Input extends RubyObject {
6262
CONCAT_WITH_CODERANGE = catWithCR;
6363
}
6464

65-
static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
66-
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
67-
return new Input(runtime, klass);
68-
}
69-
};
65+
static final ObjectAllocator ALLOCATOR = Input::new;
7066

7167
static RubyClass getClass(final Ruby runtime) {
7268
final RubyModule _JRuby_Rack = (RubyModule)
@@ -268,8 +264,7 @@ private static Method getRewindMethod(InputStream input) {
268264
try {
269265
return input.getClass().getMethod("rewind", (Class<?>[]) null);
270266
}
271-
catch (NoSuchMethodException e) { /* NOOP */ }
272-
catch (SecurityException e) { /* NOOP */ }
267+
catch (NoSuchMethodException | SecurityException e) { /* NOOP */ }
273268
return null;
274269
}
275270

0 commit comments

Comments
 (0)