@Test public void testMenuStringParsing() { List<JMenuTracker.MenuNameAndGravity> namesAndGravities = JMenuTracker.parseMenuString("AAA.B[10].CC[22]"); assertEquals(3, namesAndGravities.size()); assertEquals("AAA", namesAndGravities.get(0).getMenuName()); assertEquals(GravityTracker.USE_ALPHABETIC_ORDER, namesAndGravities.get(0).getGravity(), 0.0); assertEquals("B", namesAndGravities.get(1).getMenuName()); assertEquals(10.0, namesAndGravities.get(1).getGravity(), 0.0); assertEquals("CC", namesAndGravities.get(2).getMenuName()); assertEquals(22.0, namesAndGravities.get(2).getGravity(), 0.0); }
static List<MenuNameAndGravity> parseMenuString(final String menuString) { final List<MenuNameAndGravity> namesAndGravities = new ArrayList<MenuNameAndGravity>(); ParseState state = ParseState.LOOKING_FOR_OPENING_BRACKET; StringBuilder menuName = new StringBuilder(); StringBuilder gravityAsString = null; for (int i = 0; i < menuString.length(); ++i) { final char ch = menuString.charAt(i); switch (state) { case LOOKING_FOR_OPENING_BRACKET: if (ch == '.') { if (menuName.length() == 0) throw new IllegalArgumentException("zero-length menu name found."); namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); menuName = new StringBuilder(); } else if (ch == '[') { gravityAsString = new StringBuilder(); state = ParseState.LOOKING_FOR_CLOSING_BRACKET; } else menuName.append(ch); break; case LOOKING_FOR_CLOSING_BRACKET: if (ch != ']') gravityAsString.append(ch); else { double gravity; try { gravity = Double.parseDouble(gravityAsString.toString()); } catch (NumberFormatException e) { throw new IllegalArgumentException("bad \"gravity\" in menu string. (" + menuString + ")"); } namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), gravity)); menuName = new StringBuilder(); state = ParseState.LOOKING_FOR_PERIOD; } break; case LOOKING_FOR_PERIOD: if (ch != '.') throw new IllegalArgumentException("period expected in menu string. (" + menuString + ")"); state = ParseState.LOOKING_FOR_OPENING_BRACKET; break; } } if (state == ParseState.LOOKING_FOR_OPENING_BRACKET) namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); else if (state != ParseState.LOOKING_FOR_PERIOD) throw new IllegalArgumentException("incomplete \"gravity\" specification in menu string (" + menuName + "). (" + menuString + ")"); return namesAndGravities; }
JMenuTracker { static List<MenuNameAndGravity> parseMenuString(final String menuString) { final List<MenuNameAndGravity> namesAndGravities = new ArrayList<MenuNameAndGravity>(); ParseState state = ParseState.LOOKING_FOR_OPENING_BRACKET; StringBuilder menuName = new StringBuilder(); StringBuilder gravityAsString = null; for (int i = 0; i < menuString.length(); ++i) { final char ch = menuString.charAt(i); switch (state) { case LOOKING_FOR_OPENING_BRACKET: if (ch == '.') { if (menuName.length() == 0) throw new IllegalArgumentException("zero-length menu name found."); namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); menuName = new StringBuilder(); } else if (ch == '[') { gravityAsString = new StringBuilder(); state = ParseState.LOOKING_FOR_CLOSING_BRACKET; } else menuName.append(ch); break; case LOOKING_FOR_CLOSING_BRACKET: if (ch != ']') gravityAsString.append(ch); else { double gravity; try { gravity = Double.parseDouble(gravityAsString.toString()); } catch (NumberFormatException e) { throw new IllegalArgumentException("bad \"gravity\" in menu string. (" + menuString + ")"); } namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), gravity)); menuName = new StringBuilder(); state = ParseState.LOOKING_FOR_PERIOD; } break; case LOOKING_FOR_PERIOD: if (ch != '.') throw new IllegalArgumentException("period expected in menu string. (" + menuString + ")"); state = ParseState.LOOKING_FOR_OPENING_BRACKET; break; } } if (state == ParseState.LOOKING_FOR_OPENING_BRACKET) namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); else if (state != ParseState.LOOKING_FOR_PERIOD) throw new IllegalArgumentException("incomplete \"gravity\" specification in menu string (" + menuName + "). (" + menuString + ")"); return namesAndGravities; } }
JMenuTracker { static List<MenuNameAndGravity> parseMenuString(final String menuString) { final List<MenuNameAndGravity> namesAndGravities = new ArrayList<MenuNameAndGravity>(); ParseState state = ParseState.LOOKING_FOR_OPENING_BRACKET; StringBuilder menuName = new StringBuilder(); StringBuilder gravityAsString = null; for (int i = 0; i < menuString.length(); ++i) { final char ch = menuString.charAt(i); switch (state) { case LOOKING_FOR_OPENING_BRACKET: if (ch == '.') { if (menuName.length() == 0) throw new IllegalArgumentException("zero-length menu name found."); namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); menuName = new StringBuilder(); } else if (ch == '[') { gravityAsString = new StringBuilder(); state = ParseState.LOOKING_FOR_CLOSING_BRACKET; } else menuName.append(ch); break; case LOOKING_FOR_CLOSING_BRACKET: if (ch != ']') gravityAsString.append(ch); else { double gravity; try { gravity = Double.parseDouble(gravityAsString.toString()); } catch (NumberFormatException e) { throw new IllegalArgumentException("bad \"gravity\" in menu string. (" + menuString + ")"); } namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), gravity)); menuName = new StringBuilder(); state = ParseState.LOOKING_FOR_PERIOD; } break; case LOOKING_FOR_PERIOD: if (ch != '.') throw new IllegalArgumentException("period expected in menu string. (" + menuString + ")"); state = ParseState.LOOKING_FOR_OPENING_BRACKET; break; } } if (state == ParseState.LOOKING_FOR_OPENING_BRACKET) namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); else if (state != ParseState.LOOKING_FOR_PERIOD) throw new IllegalArgumentException("incomplete \"gravity\" specification in menu string (" + menuName + "). (" + menuString + ")"); return namesAndGravities; } JMenuTracker(final JPopupMenu rootPopupMenu);  JMenuTracker(final JMenuBar rootMenuBar); }
JMenuTracker { static List<MenuNameAndGravity> parseMenuString(final String menuString) { final List<MenuNameAndGravity> namesAndGravities = new ArrayList<MenuNameAndGravity>(); ParseState state = ParseState.LOOKING_FOR_OPENING_BRACKET; StringBuilder menuName = new StringBuilder(); StringBuilder gravityAsString = null; for (int i = 0; i < menuString.length(); ++i) { final char ch = menuString.charAt(i); switch (state) { case LOOKING_FOR_OPENING_BRACKET: if (ch == '.') { if (menuName.length() == 0) throw new IllegalArgumentException("zero-length menu name found."); namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); menuName = new StringBuilder(); } else if (ch == '[') { gravityAsString = new StringBuilder(); state = ParseState.LOOKING_FOR_CLOSING_BRACKET; } else menuName.append(ch); break; case LOOKING_FOR_CLOSING_BRACKET: if (ch != ']') gravityAsString.append(ch); else { double gravity; try { gravity = Double.parseDouble(gravityAsString.toString()); } catch (NumberFormatException e) { throw new IllegalArgumentException("bad \"gravity\" in menu string. (" + menuString + ")"); } namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), gravity)); menuName = new StringBuilder(); state = ParseState.LOOKING_FOR_PERIOD; } break; case LOOKING_FOR_PERIOD: if (ch != '.') throw new IllegalArgumentException("period expected in menu string. (" + menuString + ")"); state = ParseState.LOOKING_FOR_OPENING_BRACKET; break; } } if (state == ParseState.LOOKING_FOR_OPENING_BRACKET) namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); else if (state != ParseState.LOOKING_FOR_PERIOD) throw new IllegalArgumentException("incomplete \"gravity\" specification in menu string (" + menuName + "). (" + menuString + ")"); return namesAndGravities; } JMenuTracker(final JPopupMenu rootPopupMenu);  JMenuTracker(final JMenuBar rootMenuBar); GravityTracker getGravityTracker(final String menuString); }
JMenuTracker { static List<MenuNameAndGravity> parseMenuString(final String menuString) { final List<MenuNameAndGravity> namesAndGravities = new ArrayList<MenuNameAndGravity>(); ParseState state = ParseState.LOOKING_FOR_OPENING_BRACKET; StringBuilder menuName = new StringBuilder(); StringBuilder gravityAsString = null; for (int i = 0; i < menuString.length(); ++i) { final char ch = menuString.charAt(i); switch (state) { case LOOKING_FOR_OPENING_BRACKET: if (ch == '.') { if (menuName.length() == 0) throw new IllegalArgumentException("zero-length menu name found."); namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); menuName = new StringBuilder(); } else if (ch == '[') { gravityAsString = new StringBuilder(); state = ParseState.LOOKING_FOR_CLOSING_BRACKET; } else menuName.append(ch); break; case LOOKING_FOR_CLOSING_BRACKET: if (ch != ']') gravityAsString.append(ch); else { double gravity; try { gravity = Double.parseDouble(gravityAsString.toString()); } catch (NumberFormatException e) { throw new IllegalArgumentException("bad \"gravity\" in menu string. (" + menuString + ")"); } namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), gravity)); menuName = new StringBuilder(); state = ParseState.LOOKING_FOR_PERIOD; } break; case LOOKING_FOR_PERIOD: if (ch != '.') throw new IllegalArgumentException("period expected in menu string. (" + menuString + ")"); state = ParseState.LOOKING_FOR_OPENING_BRACKET; break; } } if (state == ParseState.LOOKING_FOR_OPENING_BRACKET) namesAndGravities.add(new MenuNameAndGravity(menuName.toString(), GravityTracker.USE_ALPHABETIC_ORDER)); else if (state != ParseState.LOOKING_FOR_PERIOD) throw new IllegalArgumentException("incomplete \"gravity\" specification in menu string (" + menuName + "). (" + menuString + ")"); return namesAndGravities; } JMenuTracker(final JPopupMenu rootPopupMenu);  JMenuTracker(final JMenuBar rootMenuBar); GravityTracker getGravityTracker(final String menuString);  }