Skip to content

Commit fd15daa

Browse files
committed
[chore] Use Java 8 syntax where possible
1 parent 19200d1 commit fd15daa

13 files changed

+37
-92
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.PrintWriter;
2828
import java.io.StringWriter;
2929
import java.util.Collections;
30-
import java.util.Iterator;
3130
import java.util.Map;
3231
import java.util.Set;
3332

@@ -161,8 +160,7 @@ public static void defaultRespond(final RackResponse rackResponse,
161160
responseEnv.setStatus( rackResponse.getStatus() );
162161
@SuppressWarnings("unchecked")
163162
final Set<Map.Entry> headers = rackResponse.getHeaders().entrySet();
164-
for ( Iterator<Map.Entry> it = headers.iterator(); it.hasNext(); ) {
165-
final Map.Entry entry = it.next();
163+
for (final Map.Entry entry : headers) {
166164
final String key = entry.getKey().toString();
167165
final Object value = entry.getValue();
168166
responseEnv.addHeader(key, value != null ? value.toString() : null);

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ public void init(final RackContext rackContext) {
100100
*/
101101
@Override
102102
public RackApplication newApplication() {
103-
return createApplication(new ApplicationObjectFactory() {
104-
@Override
105-
public IRubyObject create(Ruby runtime) {
106-
return createApplicationObject(runtime);
107-
}
108-
});
103+
return createApplication(this::createApplicationObject);
109104
}
110105

111106
/**
@@ -212,12 +207,7 @@ public RackApplication newErrorApplication() {
212207
}
213208
try {
214209
RackApplication app = createErrorApplication(
215-
new ApplicationObjectFactory() {
216-
@Override
217-
public IRubyObject create(Ruby runtime) {
218-
return createErrorApplicationObject(runtime);
219-
}
220-
}
210+
this::createErrorApplicationObject
221211
);
222212
app.init();
223213
return app;
@@ -533,10 +523,7 @@ private static String getContextLoaderScript(final String name, final boolean si
533523
InputStream is = contextLoader.getResourceAsStream(name);
534524
return IOHelpers.inputStreamToString(is);
535525
}
536-
catch (IOException e) {
537-
if ( silent ) return null; throw e;
538-
}
539-
catch (RuntimeException e) {
526+
catch (IOException | RuntimeException e) {
540527
if ( silent ) return null; throw e;
541528
}
542529
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,7 @@ protected RackLogger createLogger(final String loggerClass) {
184184
Constructor<?> ctor = klass.getConstructor(String.class);
185185
return (RackLogger) ctor.newInstance( getLoggerName() );
186186
}
187-
catch (NoSuchMethodException retry) {
188-
return newLoggerInstance(klass, retry);
189-
}
190-
catch (IllegalAccessException retry) {
187+
catch (NoSuchMethodException | IllegalAccessException retry) {
191188
return newLoggerInstance(klass, retry);
192189
}
193190
catch (InstantiationException e) {
@@ -290,13 +287,13 @@ public Map<String, String> getRuntimeEnvironment() {
290287
// jruby.runtime.env = false clear env (return empty)
291288
//return keep ? null : new HashMap<String, String>();
292289
if ( keep ) {
293-
return new HashMap<String, String>(System.getenv());
290+
return new HashMap<>(System.getenv());
294291
}
295292
else {
296-
return new HashMap<String, String>();
293+
return new HashMap<>();
297294
}
298295
}
299-
if ( isIgnoreEnvironment() ) return new HashMap<String, String>();
296+
if ( isIgnoreEnvironment() ) return new HashMap<>();
300297
// TODO maybe support custom value 'servlet' to use init params ?
301298
return toStringMap(env);
302299
}
@@ -425,7 +422,7 @@ private Map<String, String> toStringMap(final String env) {
425422
GEM_HOME=/opt/local/rvm/gems/jruby-1.6.8@jruby-rack
426423
*/
427424
LineNumberReader reader = new LineNumberReader(new StringReader(env.trim()));
428-
Map<String, String> map = new LinkedHashMap<String, String>(); String line;
425+
Map<String, String> map = new LinkedHashMap<>(); String line;
429426
try {
430427
while ( (line = reader.readLine()) != null ) {
431428
final String[] entries = line.split(",");
@@ -453,7 +450,7 @@ private Map<String, String> toStringMap(final String env) {
453450
}
454451

455452
private static Map<String,String> getLoggerTypes() {
456-
final Map<String,String> loggerTypes = new HashMap<String, String>(8);
453+
final Map<String,String> loggerTypes = new HashMap<>(8);
457454
loggerTypes.put("commons_logging", "org.jruby.rack.logging.CommonsLoggingLogger");
458455
loggerTypes.put("clogging", "org.jruby.rack.logging.CommonsLoggingLogger");
459456
loggerTypes.put("slf4j", "org.jruby.rack.logging.Slf4jLogger");

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

Lines changed: 10 additions & 14 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);
@@ -293,19 +293,15 @@ protected void launchInitialization(final Queue<RackApplication> apps) {
293293
if ( initThreads == null ) initThreads = 4; // quad-core baby
294294

295295
for (int i = 0; i < initThreads; i++) {
296-
new Thread(new Runnable() {
297-
@Override
298-
public void run() {
299-
while (true) {
300-
final RackApplication app;
301-
synchronized (apps) {
302-
if ( apps.isEmpty() ) break;
303-
app = apps.remove();
304-
}
305-
if ( ! initAndPutApplicationToPool(app) ) break;
296+
new Thread(() -> {
297+
while (true) {
298+
final RackApplication app;
299+
synchronized (apps) {
300+
if ( apps.isEmpty() ) break;
301+
app = apps.remove();
306302
}
303+
if ( ! initAndPutApplicationToPool(app) ) break;
307304
}
308-
309305
}, "JRuby-Rack-App-Init-" + i).start();
310306
}
311307
}
@@ -332,7 +328,7 @@ private boolean initAndPutApplicationToPool(final RackApplication app) {
332328
}
333329

334330
protected Queue<RackApplication> createApplications() throws RackInitializationException {
335-
Queue<RackApplication> apps = new LinkedList<RackApplication>();
331+
Queue<RackApplication> apps = new LinkedList<>();
336332
for (int i = 0; i < initialSize; i++) {
337333
apps.add( createApplication(false) );
338334
}
@@ -417,7 +413,7 @@ public Collection<RackApplication> getManagedApplications() {
417413
return Collections.emptySet();
418414
}
419415
Collection<RackApplication> snapshot =
420-
new ArrayList<RackApplication>(applicationPool);
416+
new ArrayList<>(applicationPool);
421417
return Collections.unmodifiableCollection(snapshot);
422418
}
423419
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import org.jruby.Ruby;
3131

32-
import static org.jruby.rack.RackLogger.Level.*;
32+
import static org.jruby.rack.RackLogger.Level.DEBUG;
3333

3434
/**
3535
* An abstract base class for decorating factories.
@@ -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/UnmappedRackFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class UnmappedRackFilter extends AbstractFilter {
4646

4747
private static final Collection<Integer> RESPONSE_NOT_HANDLED_STATUSES;
4848
static {
49-
final HashSet<Integer> statuses = new HashSet<Integer>(8, 1);
49+
final HashSet<Integer> statuses = new HashSet<>(8, 1);
5050
statuses.add( 404 );
5151
// 403 due containers not supporting PUT/DELETE correctly (Tomcat 6)
5252
statuses.add( 403 );
@@ -89,7 +89,7 @@ public void init(FilterConfig config) throws ServletException {
8989
// ResponseCapture.notHandledStatuses e.g. "403,404,500"
9090
value = config.getInitParameter("responseNotHandledStatuses");
9191
if ( value != null ) {
92-
final Set<Integer> statuses = new HashSet<Integer>();
92+
final Set<Integer> statuses = new HashSet<>();
9393
for ( String status : value.split(",") ) {
9494
status = status.trim();
9595
if ( status.length() > 0 ) {

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

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

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

7166
static RubyClass getClass(final Ruby runtime) {
7267
final RubyModule _JRuby_Rack = (RubyModule)
@@ -268,8 +263,7 @@ private static Method getRewindMethod(InputStream input) {
268263
try {
269264
return input.getClass().getMethod("rewind", (Class<?>[]) null);
270265
}
271-
catch (NoSuchMethodException e) { /* NOOP */ }
272-
catch (SecurityException e) { /* NOOP */ }
266+
catch (NoSuchMethodException | SecurityException e) { /* NOOP */ }
273267
return null;
274268
}
275269

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@
5555
@JRubyClass(name="JRuby::Rack::Logger")
5656
public class Logger extends RubyObject { // implements RackLogger
5757

58-
static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
59-
@Override
60-
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
61-
return new Logger(runtime, klass);
62-
}
63-
};
58+
static final ObjectAllocator ALLOCATOR = Logger::new;
6459

6560
// Logger::Severity :
6661

@@ -542,12 +537,7 @@ public IRubyObject stub(final ThreadContext context) {
542537
@JRubyClass(name="JRuby::Rack::ServletLog")
543538
public static class ServletLog extends RubyObject {
544539

545-
static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
546-
@Override
547-
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
548-
return new ServletLog(runtime, klass);
549-
}
550-
};
540+
static final ObjectAllocator ALLOCATOR = ServletLog::new;
551541

552542
private RackLogger context;
553543

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,7 @@ public static IRubyObject set_channel_buffer_size(final IRubyObject self, final
209209
return value;
210210
}
211211

212-
static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
213-
@Override
214-
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
215-
return new Response(runtime, klass);
216-
}
217-
};
212+
static final ObjectAllocator ALLOCATOR = Response::new;
218213

219214
protected Response(Ruby runtime, RubyClass metaClass) {
220215
super(runtime, metaClass);
@@ -511,8 +506,7 @@ public IRubyObject yield(ThreadContext context, IRubyObject line) {
511506
catch (WrappedException e) { throw e.getIOCause(); }
512507
}
513508
}
514-
catch (IOException e) { if ( ! handledAsClientAbort(e) ) throw e; }
515-
catch (RuntimeException e) { if ( ! handledAsClientAbort(e) ) throw e; }
509+
catch (IOException | RuntimeException e) { if ( ! handledAsClientAbort(e) ) throw e; }
516510
finally {
517511
if ( body.respondsTo("close") ) {
518512
body.callMethod(currentContext(), "close");

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@
4141
@JRubyClass(name="Rack::Handler::Servlet")
4242
public class Servlet extends RubyObject {
4343

44-
static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
45-
@Override
46-
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
47-
return new Servlet(runtime, klass);
48-
}
49-
};
44+
static final ObjectAllocator ALLOCATOR = Servlet::new;
5045

5146
protected Servlet(Ruby runtime, RubyClass metaClass) {
5247
super(runtime, metaClass);

0 commit comments

Comments
 (0)