+ /**
+ * Return the list of authors, grouped by starting letter(s) if needed.
+ * <p>
+ * If the number of author is not too high, only one group with an empty
+ * name and all the authors will be returned.
+ * <p>
+ * If not, the authors will be separated into groups:
+ * <ul>
+ * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
+ * </li>
+ * <li><tt>0-9</tt>: any authors whose name starts with a number</li>
+ * <li><tt>A-C</tt> (for instance): any author whose name starts with
+ * <tt>A</tt>, <tt>B</tt> or <tt>C</tt></li>
+ * </ul>
+ * Note that the letters used in the groups can vary (except <tt>*</tt> and
+ * <tt>0-9</tt>, which may only be present or not).
+ *
+ * @return the authors' names, grouped by letter(s)
+ *
+ * @throws IOException
+ * in case of IOException
+ */
+ public Map<String, List<String>> getAuthorsGrouped() throws IOException {
+ int MAX = 20;
+
+ Map<String, List<String>> groups = new TreeMap<String, List<String>>();
+ List<String> authors = getAuthors();
+
+ // If all authors fit the max, just report them as is
+ if (authors.size() <= MAX) {
+ groups.put("", authors);
+ return groups;
+ }
+
+ // Create groups A to Z, which can be empty here
+ for (char car = 'A'; car <= 'Z'; car++) {
+ groups.put(Character.toString(car), getAuthorsGroup(authors, car));
+ }
+
+ // Collapse them
+ List<String> keys = new ArrayList<String>(groups.keySet());
+ for (int i = 0; i + 1 < keys.size(); i++) {
+ String keyNow = keys.get(i);
+ String keyNext = keys.get(i + 1);
+
+ List<String> now = groups.get(keyNow);
+ List<String> next = groups.get(keyNext);
+
+ int currentTotal = now.size() + next.size();
+ if (currentTotal <= MAX) {
+ String key = keyNow.charAt(0) + "-"
+ + keyNext.charAt(keyNext.length() - 1);
+
+ List<String> all = new ArrayList<String>();
+ all.addAll(now);
+ all.addAll(next);
+
+ groups.remove(keyNow);
+ groups.remove(keyNext);
+ groups.put(key, all);
+
+ keys.set(i, key); // set the new key instead of key(i)
+ keys.remove(i + 1); // remove the next, consumed key
+ i--; // restart at key(i)
+ }
+ }
+
+ // Add "special" groups
+ groups.put("*", getAuthorsGroup(authors, '*'));
+ groups.put("0-9", getAuthorsGroup(authors, '0'));
+
+ // Prune empty groups
+ keys = new ArrayList<String>(groups.keySet());
+ for (String key : keys) {
+ if (groups.get(key).isEmpty()) {
+ groups.remove(key);
+ }
+ }
+
+ return groups;
+ }
+
+ /**
+ * Get all the authors that start with the given character:
+ * <ul>
+ * <li><tt>*</tt>: any author whose name doesn't contain letters nor numbers
+ * </li>
+ * <li><tt>0</tt>: any authors whose name starts with a number</li>
+ * <li><tt>A</tt> (any capital latin letter): any author whose name starts
+ * with <tt>A</tt></li>
+ * </ul>
+ *
+ * @param authors
+ * the full list of authors
+ * @param car
+ * the starting character, <tt>*</tt>, <tt>0</tt> or a capital
+ * letter
+ *
+ * @return the authors that fulfill the starting letter
+ *
+ * @throws IOException
+ * in case of IOException
+ */
+ private List<String> getAuthorsGroup(List<String> authors, char car)
+ throws IOException {
+ List<String> accepted = new ArrayList<String>();
+ for (String author : authors) {
+ char first = '*';
+ for (int i = 0; first == '*' && i < author.length(); i++) {
+ String san = StringUtils.sanitize(author, true, true);
+ char c = san.charAt(i);
+ if (c >= '0' && c <= '9') {
+ first = '0';
+ } else if (c >= 'a' && c <= 'z') {
+ first = (char) (c - 'a' + 'A');
+ } else if (c >= 'A' && c <= 'Z') {
+ first = c;
+ }
+ }
+
+ if (first == car) {
+ accepted.add(author);
+ }
+ }
+
+ return accepted;
+ }
+