patternjavaMinor
Is this Sudoku a good use of the Fluent Builder Pattern?
Viewed 0 times
thisthebuilderfluentgoodusesudokupattern
Problem
Code
Sudoku.java
```
public class Sudoku implements ISudoku {
/**
* Generates wildcard values in case of conflict with the keys of the
* Sudoku. Ensures all characters are alphanumeric, does not generate the
* same wildcard twice, and tries to keep the wildcard as short as
* possible.
*/
protected static class WildcardGenerator {
/**
* Used to build the next wildcard.
*/
protected static final List SINGLE_CHARACTER_WILDCARDS =
createSingleCharacterWildcards();
/**
* Returns a wildcard whose generation is based on wildcard so that
* the same wildcard will not be generated twice by the same
* WildcardGenerator.
* @param wildcard The current wildcard.
* @return The next wildcard generated.
*/
public String nextWildcard(String wildcard) {
String last = wildcard.substring(wildcard.length() - 1);
int index = SINGLE_CHARACTER_WILDCARDS.indexOf(last);
if (index == SINGLE_CHARACTER_WILDCARDS.size() - 1) {
return wildcard + SINGLE_CHARACTER_WILDCARDS.get(0);
}
String replacement = SINGLE_CHARACTER_WILDCARDS.get(index + 1);
return wildcard.replace(last, replacement);
}
/**
* Creates and returns the single character wildcards used in the
* WildcardGenerators.
* @return The single character wildcards created.
*/
protected static List createSingleCharacterWildcards() {
List result = new ArrayList();
for (char ch = '0'; ch
* strict
* withWildcard
* withBoxWidth
* withAddedRow
* finish
*
*/
public static class Builder {
/**
* A regex that matches a sequence of non-alphanumeric characters in a
* String.
*/
protected static final String DELIMITERS = "[\\W_]+";
/*
Sudoku.java
```
public class Sudoku implements ISudoku {
/**
* Generates wildcard values in case of conflict with the keys of the
* Sudoku. Ensures all characters are alphanumeric, does not generate the
* same wildcard twice, and tries to keep the wildcard as short as
* possible.
*/
protected static class WildcardGenerator {
/**
* Used to build the next wildcard.
*/
protected static final List SINGLE_CHARACTER_WILDCARDS =
createSingleCharacterWildcards();
/**
* Returns a wildcard whose generation is based on wildcard so that
* the same wildcard will not be generated twice by the same
* WildcardGenerator.
* @param wildcard The current wildcard.
* @return The next wildcard generated.
*/
public String nextWildcard(String wildcard) {
String last = wildcard.substring(wildcard.length() - 1);
int index = SINGLE_CHARACTER_WILDCARDS.indexOf(last);
if (index == SINGLE_CHARACTER_WILDCARDS.size() - 1) {
return wildcard + SINGLE_CHARACTER_WILDCARDS.get(0);
}
String replacement = SINGLE_CHARACTER_WILDCARDS.get(index + 1);
return wildcard.replace(last, replacement);
}
/**
* Creates and returns the single character wildcards used in the
* WildcardGenerators.
* @return The single character wildcards created.
*/
protected static List createSingleCharacterWildcards() {
List result = new ArrayList();
for (char ch = '0'; ch
* strict
* withWildcard
* withBoxWidth
* withAddedRow
* finish
*
*/
public static class Builder {
/**
* A regex that matches a sequence of non-alphanumeric characters in a
* String.
*/
protected static final String DELIMITERS = "[\\W_]+";
/*
Solution
List is not good way, use Set to store (return false if wildcard exists) your wildcard.So you will avoid the
"" + (very bad way) and use Character.ValueOf(char));Context
StackExchange Code Review Q#13914, answer score: 3
Revisions (0)
No revisions yet.