Skip to content

Instantly share code, notes, and snippets.

@andrezimmermann
Last active August 29, 2015 14:25
Show Gist options
  • Save andrezimmermann/80b572becf936a1323d3 to your computer and use it in GitHub Desktop.
Save andrezimmermann/80b572becf936a1323d3 to your computer and use it in GitHub Desktop.
Code Review - PE.SE
public class ColorFinder {
public static void main(String[] args) {
Set<String> colorSet = new HashSet<String>();
colorSet.add("red");
colorSet.add("blue");
colorSet.add("green");
ColorFinder colorFinder = new ColorFinder(colorSet);
System.out.println(colorFinder.findColor("xxred"));
System.out.println(colorFinder.findColor("greenxx"));
System.out.println(colorFinder.findColor("xxbluexx"));
}
private Set<String> colorSet;
public ColorFinder(Set<String> colorSet) {
Set<String> lowerCaseSet = new HashSet<String>(colorSet.size());
for (String color : colorSet) {
lowerCaseSet.add(color.toLowerCase());
}
this.colorSet = lowerCaseSet;
}
public String findColor(String input) {
if (input == null) {
throw new IllegalArgumentException("Input can't be null");
}
String lowerCaseInput = input.toLowerCase();
for (String color : colorSet) {
if (lowerCaseInput.startsWith(color)) {
int start = lowerCaseInput.indexOf(color);
int end = start + color.length();
return lowerCaseInput.substring(start, end);
}
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment