SearchUtil { public static List<String> splitForSearch(final String phrase, final int charThreshold) { if (phrase != null) { String [] token = StringUtils.splitPreserveAllTokens(phrase, "| ;/\\"); List<String> words = new ArrayList<>(token.length); for (final String aToken : token) { if (StringUtils.isNotBlank(aToken)) { String clean = aToken.trim(); if (clean.length() >= charThreshold) { int pos = clean.length() - 1; for (; pos >= 0; pos--) { char last = clean.charAt(pos); if (last != ',' && last != '.' && last != '!' && last != '?' && last != ')' && last != '}' && last != ']') { break; } } if (pos < clean.length() - 1) { clean = clean.substring(0, pos + 1); } pos = 0; for (; pos < clean.length(); pos++) { char first = clean.charAt(pos); if (first != '(' && first != '{' && first != '[') { break; } } if (pos > 0) { clean = clean.substring(pos); } if (clean.length() >= charThreshold) { pos = clean.indexOf('-'); if (pos >= charThreshold && clean.length() > charThreshold * 2 + 1 && pos != 0 && (pos < clean.length() - charThreshold) && clean.charAt(pos + 1) != '-') { words.add(clean.substring(0, pos)); words.add(clean.substring(pos + 1, clean.length())); } else { words.add(clean); } } } } } return words; } return Collections.EMPTY_LIST; }  static List<String> splitForSearch(final String phrase, final int charThreshold); static String priceFacetName(final long shopId, final String currency); static long priceToLong(final BigDecimal price); static Long valToLong(final String value, final int scale); static String longToVal(final String value, final int scale); static void destroy(); static void addDestroyable(final Destroyable destroyable);  }
@Test public void testSplitCommonUseCases() { List<String> rez; rez = SearchUtil.splitForSearch("HP Prelude Toploader-Tasche, 15,6 Zoll", 2); assertArrayEquals(new String[] {"HP", "Prelude", "Toploader", "Tasche", "15,6", "Zoll" }, rez.toArray()); rez = SearchUtil.splitForSearch("HP 410A - Schwarz - Original - LaserJet - Tonerpatrone (CF410A) - für Color LaserJet Pro M452, LaserJet Pro MFP M377, MFP M477", 2); assertArrayEquals(new String[] {"HP", "410A", "Schwarz", "Original", "LaserJet", "Tonerpatrone", "CF410A", "für", "Color", "LaserJet", "Pro", "M452", "LaserJet", "Pro", "MFP", "M377", "MFP", "M477"}, rez.toArray()); rez = SearchUtil.splitForSearch("1y SecureDoc WinEntr Supp 5K+ E-LTU", 2); assertArrayEquals(new String[] {"1y", "SecureDoc", "WinEntr", "Supp", "5K+", "E-LTU" }, rez.toArray()); rez = SearchUtil.splitForSearch("HP Monitor EliteDisplay E271i / 68,5cm (27\") IPS LED / 1920 x 1080 (16:9) / DVI-D Display Port VGA / 1000:1 / 250cd/m2 / 7ms", 2); assertArrayEquals(new String[] {"HP", "Monitor", "EliteDisplay", "E271i", "68,5cm", "27\"", "IPS", "LED", "1920", "1080", "16:9", "DVI-D", "Display", "Port", "VGA", "1000:1", "250cd", "m2", "7ms" }, rez.toArray()); rez = SearchUtil.splitForSearch("iPad Wi-Fi + Cellular 128GB - Gold", 2); assertArrayEquals(new String[] {"iPad", "Wi-Fi", "Cellular", "128GB", "Gold" }, rez.toArray()); rez = SearchUtil.splitForSearch("TS/ThinkPad T570/i5-7200U/8GB /256GB SSD/15.6\"/Integrated Intel HD Graphics 620/W10P/Black - Kaby Lake", 2); assertArrayEquals(new String[] {"TS", "ThinkPad", "T570", "i5", "7200U", "8GB", "256GB", "SSD", "15.6\"", "Integrated", "Intel", "HD", "Graphics", "620", "W10P", "Black", "Kaby", "Lake" }, rez.toArray()); }
@Test public void testSplitBrutal() { List<String> rez; rez = SearchUtil.splitForSearch(" | just, phraze--to split;for.test,,hehe++wow", 0); assertArrayEquals(new String[] {"just", "phraze--to", "split", "for.test,,hehe++wow" }, rez.toArray()); rez = SearchUtil.splitForSearch(" | just, phraze-to split;for.test,,hehe++wow", 0); assertArrayEquals(new String[] {"just", "phraze", "to", "split", "for.test,,hehe++wow" }, rez.toArray()); rez = SearchUtil.splitForSearch(" | just, two-phraze split;for.test,,hehe++wow", 3); assertArrayEquals(new String[] {"just", "two", "phraze", "split", "for.test,,hehe++wow" }, rez.toArray()); rez = SearchUtil.splitForSearch(" | just, phraze-to split;for.test,,hehe++wow", 3); assertArrayEquals(new String[] {"just", "phraze-to", "split", "for.test,,hehe++wow" }, rez.toArray()); rez = SearchUtil.splitForSearch(" | just, t-phraze split;for.test,,hehe++wow", 3); assertArrayEquals(new String[] {"just", "t-phraze", "split", "for.test,,hehe++wow" }, rez.toArray()); }
@Test public void testSplitNullValue() { List<String> rez = SearchUtil.splitForSearch(null, 0); assertEquals(0, rez.size()); }
@Test public void testSplitEmptyValue() { List<String> rez = SearchUtil.splitForSearch("", 0); assertTrue(rez.isEmpty()); rez = SearchUtil.splitForSearch(" ,,++||-- + - ", 3); assertArrayEquals(new String[] {",,++" }, rez.toArray()); rez = SearchUtil.splitForSearch(" ,,++||-- + - ", 0); assertArrayEquals(new String[] {",,++", "--", "+", "-" }, rez.toArray()); }