DefaultPrefixFunctionFactory implements PrefixFunctionFactory { @Override public PrefixFunction getInstance(String pattern) { final int[] prefixes = new int[pattern.length()]; prefixes[0] = 0; int length = 0; for (int i = 1; i < pattern.length(); i++) { final char character = pattern.charAt(i); while (length > 0 && character != pattern.charAt(length)) { length = prefixes[length - 1]; } if (character == pattern.charAt(length)) { length ++; } prefixes[i] = length; } return new ArrayPrefixFunction(prefixes); }  @Override PrefixFunction getInstance(String pattern);  }
@Test(dataProvider = "patternProvider") public void testName(String pattern, int[] prefixes) throws Exception { final PrefixFunction function = getFactory().getInstance(pattern); assertThat(function, is(notNullValue())); for (int i = 0; i < prefixes.length; i++) { int prefix = prefixes[i]; assertThat(function.prefix(i), is(prefix)); } }