Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase performance by computing multiply needed values lazily #478

Open
wants to merge 2 commits into
base: uinverse
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.caching;

import java.util.function.Supplier;

public class LazilyComputedValue<T> {

private final Object lockForValueComputation = new Object();
private volatile Supplier<T> lazyValueComputationFunction;
private T lazilyComputedValue;

public LazilyComputedValue(Supplier<T> lazyValueComputationFunction) {
this.lazyValueComputationFunction = lazyValueComputationFunction;
}

public T getLazilyComputedValue() {
if (lazyValueComputationFunction != null) {
synchronized (lockForValueComputation) {
if (lazyValueComputationFunction != null) {
lazilyComputedValue = lazyValueComputationFunction.get();
lazyValueComputationFunction = null;
return lazilyComputedValue;
}
}
}
return lazilyComputedValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.factories;

import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.caching.LazilyComputedValue;
import java.util.function.Supplier;
import org.springframework.stereotype.Service;

@Service
public class LazyFactory {

public <T> LazilyComputedValue<T> createLazy(Supplier<T> lazyValueComputationFunction) {
return new LazilyComputedValue<>(lazyValueComputationFunction);
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
package com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.strategies;

import org.springframework.stereotype.Service;

import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.interfaces.strategies.IsEvenlyDivisibleStrategy;
import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.strategies.constants.NoFizzNoBuzzStrategyConstants;
import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.caching.LazilyComputedValue;
import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.factories.LazyFactory;
import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.math.arithmetics.NumberIsMultipleOfAnotherNumberVerifier;
import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.strategies.constants.NoFizzNoBuzzStrategyConstants;
import com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.interfaces.strategies.IsEvenlyDivisibleStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* Strategy for NoFizzNoBuzz
*/
@Service
public class NoFizzNoBuzzStrategy implements IsEvenlyDivisibleStrategy {

private final LazyFactory lazyFactory;

@Autowired
public NoFizzNoBuzzStrategy(LazyFactory lazyFactory) {
this.lazyFactory = lazyFactory;
}

/**
* @param theInteger int
* @return boolean
*/
public boolean isEvenlyDivisible(final int theInteger) {
if (!NumberIsMultipleOfAnotherNumberVerifier.numberIsMultipleOfAnotherNumber(theInteger,
NoFizzNoBuzzStrategyConstants.NO_FIZZ_INTEGER_CONSTANT_VALUE)) {
if (!NumberIsMultipleOfAnotherNumberVerifier.numberIsMultipleOfAnotherNumber(theInteger,
NoFizzNoBuzzStrategyConstants.NO_BUZZ_INTEGER_CONSTANT_VALUE)) {
LazilyComputedValue<Boolean> lazilyComputedIsFizzMultiple = this.lazyFactory.createLazy(
() -> NumberIsMultipleOfAnotherNumberVerifier.numberIsMultipleOfAnotherNumber(theInteger,
NoFizzNoBuzzStrategyConstants.NO_FIZZ_INTEGER_CONSTANT_VALUE));
LazilyComputedValue<Boolean> lazilyComputedIsBuzzMultiple = this.lazyFactory.createLazy(
() -> NumberIsMultipleOfAnotherNumberVerifier.numberIsMultipleOfAnotherNumber(theInteger,
NoFizzNoBuzzStrategyConstants.NO_BUZZ_INTEGER_CONSTANT_VALUE));
if (!lazilyComputedIsFizzMultiple.getLazilyComputedValue()) {
if (!lazilyComputedIsBuzzMultiple.getLazilyComputedValue()) {
return true;
} else {
return false;
}
} else if (!NumberIsMultipleOfAnotherNumberVerifier.numberIsMultipleOfAnotherNumber(theInteger,
NoFizzNoBuzzStrategyConstants.NO_BUZZ_INTEGER_CONSTANT_VALUE)) {
if (!NumberIsMultipleOfAnotherNumberVerifier.numberIsMultipleOfAnotherNumber(theInteger,
NoFizzNoBuzzStrategyConstants.NO_FIZZ_INTEGER_CONSTANT_VALUE)) {
} else if (!lazilyComputedIsBuzzMultiple.getLazilyComputedValue()) {
if (!lazilyComputedIsFizzMultiple.getLazilyComputedValue()) {
return true;
} else {
return false;
Expand Down