StringService extends AbstractService { public void putAttributeValues(String column, boolean ignoreBlanks) { HashSet<String> longestValues = new HashSet<>(); longestValueMap.put(column, longestValues); HashSet<String> shortestValues = new HashSet<>(); shortestValueMap.put(column, shortestValues); HashSet<String> secondLongestValues = new HashSet<>(); secondLongestValueMap.put(column, secondLongestValues); HashSet<String> secondShortestValues = new HashSet<>(); secondShortestValueMap.put(column, secondShortestValues); int max = 0; int secondMax = 0; int min = -1; int secondMin = -1; for (Attribute attr : dataset.getAttributesByColumn(column)) { if (attr.isAlive()) { String value = (String) attr.getValue(); if (value == null || (ignoreBlanks && "".equals(value.trim()))) { continue; } int length = value.codePointCount(0, value.length()); if (length > max) { secondLongestValues.clear(); secondLongestValues.addAll(longestValues); secondMax = secondMax == -1 ? Integer.MIN_VALUE : max; longestValues.clear(); longestValues.add(value); max = length; } else if (length == max) { longestValues.add(value); } else if (secondMax < length && length < max) { secondLongestValues.clear(); secondLongestValues.add(value); secondMax = length; } else if (length == secondMax) { secondLongestValues.add(value); } if (length < min || min == -1) { secondShortestValues.clear(); secondShortestValues.addAll(shortestValues); secondMin = secondMin == -1 ? Integer.MAX_VALUE : min; shortestValues.clear(); shortestValues.add(value); min = length; } else if (length == min) { shortestValues.add(value); } else if (secondMin > length && length > min) { secondShortestValues.clear(); secondShortestValues.add(value); secondMin = length; } else if (length == secondMin) { secondShortestValues.add(value); } } } } StringService(DataSet dataset); void putAttributeValues(String column, boolean ignoreBlanks); boolean isLongestValue(Object var, String column, boolean ignoreBlanks); boolean isSecondLongestValue(Object var, String column, boolean ignoreBlanks); boolean isShortestValue(Object var, String column, boolean ignoreBlanks); boolean isSecondShortestValue(Object var, String column, boolean ignoreBlanks); @Override void init();  }
@Test public void testPutAttributeValues() { List<Column> columnList = generateColumnList(); DataSet dataSet = new DataSet(columnList); dataSet.initData(generateInputData()); StringService ss = new StringService(dataSet); ss.putAttributeValues("firstName", false); ss.putAttributeValues("lastName", false); Assert.assertEquals("longestValueMap size should be 2", 2, ss.longestValueMap.size()); Assert.assertEquals("secondLongestValueMap size should be 2", 2, ss.secondLongestValueMap.size()); Assert.assertEquals("secondShortestValueMap size should be 2", 2, ss.secondShortestValueMap.size()); Assert.assertEquals("shortestValueMap size should be 2", 2, ss.shortestValueMap.size()); Assert.assertEquals("The result of firstName column should be 1", 1, ss.longestValueMap.get("firstName").size()); Assert.assertEquals("The result of lastName column should be 3", 3, ss.longestValueMap.get("lastName").size()); Assert.assertEquals("The second LongestValueMap of firstName column should be 1", 1, ss.secondLongestValueMap.get("firstName").size()); Assert.assertEquals("The second LongestValueMap of lastName column should be 0", 0, ss.secondLongestValueMap.get("lastName").size()); Assert.assertEquals("The second ShortestValueMap of firstName column should be 3", 3, ss.secondShortestValueMap .get("firstName") .size()); Assert.assertEquals("The second ShortestValueMap of lastName column should be 0", 0, ss.secondShortestValueMap .get("lastName") .size()); Assert.assertEquals("The shortestValueMap of firstName column should be 1", 1, ss.shortestValueMap .get("firstName") .size()); Assert.assertEquals("The shortestValueMap of lastName column should be 3", 3, ss.shortestValueMap .get("lastName") .size()); ss.init(); }