Resolves checkstyle errors for api-gateway, lazy-loading, leader-election (#1066)
* Reduces checkstyle errors in lazy-loading * Reduces checkstyle errors in leader-election * Reduces checkstyle errors in api-gateway
This commit is contained in:
committed by
Ilkka Seppälä
parent
7f06f3b78c
commit
eae09fc07e
@ -27,22 +27,20 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Lazy loading idiom defers object creation until needed.
|
||||
* <p>
|
||||
* This example shows different implementations of the pattern with increasing sophistication.
|
||||
* <p>
|
||||
* Additional information and lazy loading flavours are described in
|
||||
* http://martinfowler.com/eaaCatalog/lazyLoad.html
|
||||
*
|
||||
* <p>This example shows different implementations of the pattern with increasing sophistication.
|
||||
*
|
||||
* <p>Additional information and lazy loading flavours are described in
|
||||
* http://martinfowler.com/eaaCatalog/lazyLoad.html
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -27,16 +27,14 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Heavy objects are expensive to create.
|
||||
*
|
||||
*/
|
||||
public class Heavy {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Heavy.class);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public Heavy() {
|
||||
LOGGER.info("Creating Heavy ...");
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Simple implementation of the lazy loading idiom. However, this is not thread safe.
|
||||
*
|
||||
*/
|
||||
public class HolderNaive {
|
||||
|
||||
@ -38,14 +36,14 @@ public class HolderNaive {
|
||||
private Heavy heavy;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public HolderNaive() {
|
||||
LOGGER.info("HolderNaive created");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get heavy object
|
||||
* Get heavy object.
|
||||
*/
|
||||
public Heavy getHeavy() {
|
||||
if (heavy == null) {
|
||||
|
@ -27,10 +27,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Same as HolderNaive but with added synchronization. This implementation is thread safe, but each
|
||||
* {@link #getHeavy()} call costs additional synchronization overhead.
|
||||
*
|
||||
*/
|
||||
public class HolderThreadSafe {
|
||||
|
||||
@ -39,14 +37,14 @@ public class HolderThreadSafe {
|
||||
private Heavy heavy;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public HolderThreadSafe() {
|
||||
LOGGER.info("HolderThreadSafe created");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get heavy object
|
||||
* Get heavy object.
|
||||
*/
|
||||
public synchronized Heavy getHeavy() {
|
||||
if (heavy == null) {
|
||||
|
@ -23,16 +23,13 @@
|
||||
|
||||
package com.iluwatar.lazy.loading;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* This lazy loader is thread safe and more efficient than {@link HolderThreadSafe}. It utilizes
|
||||
* Java 8 functional interface {@link Supplier} as {@link Heavy} factory.
|
||||
*
|
||||
*/
|
||||
public class Java8Holder {
|
||||
|
||||
@ -57,9 +54,11 @@ public class Java8Holder {
|
||||
return heavyInstance;
|
||||
}
|
||||
}
|
||||
|
||||
if (!HeavyFactory.class.isInstance(heavy)) {
|
||||
heavy = new HeavyFactory();
|
||||
}
|
||||
|
||||
return heavy.get();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user