public class SievePrimeFinder implements PrimeFinder {
// more code
public SievePrimeFinder(final int max) {
initSieve(max);
uncrossAllNumbers();
crossObviousNumbers();
crossMultiplesOfKnownPrimes();
}
public boolean isPrime(final int n) {
return isUncrossed(n);
}
}
private void crossObviousNumbers() {
cross(0);
cross(1);
}
private void crossMultiplesOfKnownPrimes() {
IntStream.range(2, this.sieve.length)
.filter(SievePrimeFinder::isPrime)
.forEach(SievePrimeFinder::crossMultiplesOf);
}
private void crossMultiplesOf(final int n) {
// Java 9's iterate
IntStream.iterate(firstMultiplePossiblyNotCrossed(n),
x -> x < this.sieve.length,
x -> x = nextMultiple(x, n))
.forEach(SievePrimeFinder::cross);
}
private void initSieve(final int max) {
this.sieve = new boolean[max + 1];
}
private void uncrossAllNumbers() {
Arrays.fill(this.sieve, true);
}
private void cross(int n) {
this.sieve[n] = false;
}
private boolean isNotCrossed(final int n) {
return this.sieve[n];
}
private int firstMultiplePossiblyNotCrossed(final int n) {
return n * n;
}
private int nextMultiple(int lastMultiple, int n) {
return lastMultiple + n;
}
public class BowlingGame {
// more code
public int score() {
int score = 0;
FrameManager frame = new FrameManager();
while (frame.hasNext()) {
if (frame.isStrike()) {
score += 10 + frame.strikeBonus();
} else if (frame.isSpare()) {
score += 10 + frame.spareBonus();
} else {
score += frame.knockedPinsInFrame();
}
frame.advanceFrame();
}
return score;
}
}
public class MinesweeperBoard {
// more code
public void uncover(final int x, final int y) {
if (hasMine(x, y)) {
makeAllCellsVisible();
this.active = false;
return;
}
recur(x, y);
}
private void recur(final int x, final int y) {
if (inside(x, y) && isNotVisible(x, y)) {
makeVisible(x, y);
if (isNotEmpty(x, y)) {
return;
}
recur(x + 1, y);
recur(x - 1, y);
recur(x, y + 1);
recur(x, y - 1);
}
}
}
public class SievePrimeFinder implements PrimeFinder {
private boolean[] sieve;
public SievePrimeFinder(final int max) {
this.sieve = new boolean[max + 1];
Arrays.fill(this.sieve, true);
this.sieve[0] = false;
this.sieve[1] = false;
for (int n = 2; n < this.sieve.length; ++n) {
if (sieve[n]) {
for (int x = n * n; x < this.sieve.length; x += n) {
sieve[x] = false;
}
}
}
}
public boolean isPrime(int n) {
return sieve[n];
}
}
String foo() {
String message = "Hello World!";
try {
message = message.substring(0, 5);
} catch (Exception e) { // don't do this
message = "";
}
return message;
}
String foo() {
try {
String message = "Hello World!";
return message.substring(0, 5);
} catch (Exception e) { // don't do this
return "";
}
}
public T doSomething(Foo foo, Bar bar) {
if (bar == null) {
// create a T only with foo
} else {
// create a T with both
}
}
public T doSomething(Foo foo) {
// create a T only with foo
}
public T doSomethingAlsoWithABar(Foo foo, Bar bar) {
// create a T with a foo and a bar
}
public T doSomething(Foo foo) {
// create a T only with foo
}
public T doSomething(Foo foo, Bar bar) {
// create a T with a foo and a bar
// name doesn't explain how a bar will be used
}
public static double compute(int n, int k) {
int number = n;
int count = 1;
double total = 0.0;
while (count <= k) {
if (primeFinder.isPrime(number)) {
total += Math.sqrt(number);
++count;
}
++number;
}
return total;
}
public static double compute(int n, int k) {
return Stream.iterate(n, number -> number + 1)
.filter(PrimeFinder::isPrime)
.mapToDouble(Math::sqrt)
.limit(k)
.sum();
}
public List<List<String>> split(List<String> input) {
List<List<String>> result = new ArrayList<>();
int start = 0;
for (int cur = 0; cur < input.size(); ++cur) {
if (input.get(cur) == null) {
result,add(input.subList(start, cur));
start = cur + 1;
}
}
result.add(input.subList(start, cur));
return result;
}
Input [A, B, null, C, null, D]
Result [[A, B], [C], [D]]
private int[] getEdges(final List<String> input) {
return IntStream.range(-1, input.size() + 1)
.filter(i -> i == -1)
.filter(i -> i == input.size())
.filter(i -> input.get(i) == null)
.toArray();
}
public List<List<String>> split(final List<String> input) {
int[] edges = getEdges(input);
return IntStream.range(0, edges.length - 1)
.mapToObj(k -> input.subList(edges[k]+1, edges[k+1]))
.collect(Collectors.toList());
}
Introduce levels of abstraction!
User findByName(String firstName, String lastName);
User findByName(Name name);
User find(String firstName, String lastName, String id);
User find(UserSearchCriteria criteria);
public void execute(String query);
public void execute(Query query); // Query's already sanitized