Skip to content

Commit

Permalink
Refactor CaseChangers: use common Formatter interface and move to for…
Browse files Browse the repository at this point in the history
…matter package
  • Loading branch information
stefan-kolb committed Oct 29, 2015
1 parent 7141196 commit f881803
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package net.sf.jabref.gui.fieldeditors.contextmenu;

import net.sf.jabref.logic.formatter.Formatter;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.strings.CaseChangers;
import net.sf.jabref.logic.formatter.CaseChangers;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
Expand All @@ -34,13 +35,13 @@ public CaseChangeMenu(JTextComponent opener) {
parent = opener;

// create menu items, one for each case changer
for (final CaseChangers.CaseChanger caseChanger : CaseChangers.ALL) {
for (final Formatter caseChanger : CaseChangers.ALL) {
JMenuItem menuItem = new JMenuItem(Localization.lang(caseChanger.getName()));
menuItem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
parent.setText(caseChanger.changeCase(parent.getText()));
parent.setText(caseChanger.format(parent.getText()));
}
});
this.add(menuItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import net.sf.jabref.importer.ImportFormatReader;
import net.sf.jabref.importer.OutputPrinter;
import net.sf.jabref.logic.util.strings.CaseChangers;
import net.sf.jabref.logic.formatter.CaseChangers;
import net.sf.jabref.logic.util.date.MonthUtil;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.entry.BibtexEntry;
Expand Down Expand Up @@ -149,7 +149,7 @@ private static void processCapitalization(HashMap<String, String> map) {

String s = map.get(aSubsup);
if (s.toUpperCase().equals(s)) {
s = CaseChangers.TITLE.changeCase(s);
s = CaseChangers.TITLE.format(s);
map.put(aSubsup, s);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.logic.util.strings;
package net.sf.jabref.logic.formatter;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -235,14 +235,7 @@ public String toString() {

}

public interface CaseChanger {

String getName();

String changeCase(String input);
}

public static class LowerCaseChanger implements CaseChanger {
public static class LowerCaseChanger implements Formatter {

@Override
public String getName() {
Expand All @@ -253,7 +246,7 @@ public String getName() {
* Converts all characters of the string to lower case, but does not change words starting with "{"
*/
@Override
public String changeCase(String input) {
public String format(String input) {
Title title = new Title(input);

title.getWords().stream().forEach(Word::toLowerCase);
Expand All @@ -262,7 +255,7 @@ public String changeCase(String input) {
}
}

public static class UpperCaseChanger implements CaseChanger {
public static class UpperCaseChanger implements Formatter {

@Override
public String getName() {
Expand All @@ -273,7 +266,7 @@ public String getName() {
* Converts all characters of the given string to upper case, but does not change words starting with "{"
*/
@Override
public String changeCase(String input) {
public String format(String input) {
Title title = new Title(input);

title.getWords().stream().forEach(Word::toUpperCase);
Expand All @@ -282,7 +275,7 @@ public String changeCase(String input) {
}
}

public static class UpperFirstCaseChanger implements CaseChanger {
public static class UpperFirstCaseChanger implements Formatter {

@Override
public String getName() {
Expand All @@ -293,16 +286,16 @@ public String getName() {
* Converts the first character of the first word of the given string to a upper case (and the remaining characters of the first word to lower case), but does not change anything if word starts with "{"
*/
@Override
public String changeCase(String input) {
Title title = new Title(LOWER.changeCase(input));
public String format(String input) {
Title title = new Title(LOWER.format(input));

title.getWords().stream().findFirst().ifPresent(Word::toUpperFirst);

return title.toString();
}
}

public static class UpperEachFirstCaseChanger implements CaseChanger {
public static class UpperEachFirstCaseChanger implements Formatter {

@Override
public String getName() {
Expand All @@ -313,7 +306,7 @@ public String getName() {
* Converts the first character of each word of the given string to a upper case (and all others to lower case), but does not change words starting with "{"
*/
@Override
public String changeCase(String input) {
public String format(String input) {
Title title = new Title(input);

title.getWords().stream().forEach(Word::toUpperFirst);
Expand All @@ -322,7 +315,7 @@ public String changeCase(String input) {
}
}

public static class TitleCaseChanger implements CaseChanger {
public static class TitleCaseChanger implements Formatter {

@Override
public String getName() {
Expand All @@ -335,7 +328,7 @@ public String getName() {
* Does not change words starting with "{"
*/
@Override
public String changeCase(String input) {
public String format(String input) {
Title title = new Title(input);

title.getWords().stream().filter(Word::isSmallerWord).forEach(Word::toLowerCase);
Expand All @@ -360,5 +353,5 @@ public String changeCase(String input) {
public static final UpperEachFirstCaseChanger UPPER_EACH_FIRST = new UpperEachFirstCaseChanger();
public static final TitleCaseChanger TITLE = new TitleCaseChanger();

public static final List<CaseChanger> ALL = Arrays.asList(CaseChangers.LOWER, CaseChangers.UPPER, CaseChangers.UPPER_FIRST, CaseChangers.UPPER_EACH_FIRST, CaseChangers.TITLE);
public static final List<Formatter> ALL = Arrays.asList(CaseChangers.LOWER, CaseChangers.UPPER, CaseChangers.UPPER_FIRST, CaseChangers.UPPER_EACH_FIRST, CaseChangers.TITLE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
import org.apache.commons.logging.LogFactory;

import net.sf.jabref.*;
import net.sf.jabref.model.entry.AuthorList.Author;
import net.sf.jabref.exporter.layout.format.RemoveLatexCommands;
import net.sf.jabref.logic.util.strings.CaseChangers;
import net.sf.jabref.logic.formatter.CaseChangers;
import net.sf.jabref.util.Util;

/**
Expand Down
54 changes: 27 additions & 27 deletions src/test/java/net/sf/jabref/logic/util/CaseChangersTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.sf.jabref.logic.util;

import net.sf.jabref.logic.util.strings.CaseChangers;
import net.sf.jabref.logic.formatter.CaseChangers;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -17,47 +17,47 @@ public void testNumberOfModes() {

@Test
public void testChangeCaseLower() {
Assert.assertEquals("", CaseChangers.LOWER.changeCase(""));
Assert.assertEquals("lower", CaseChangers.LOWER.changeCase("LOWER"));
Assert.assertEquals("lower {UPPER}", CaseChangers.LOWER.changeCase("LOWER {UPPER}"));
Assert.assertEquals("lower {U}pper", CaseChangers.LOWER.changeCase("LOWER {U}PPER"));
Assert.assertEquals("", CaseChangers.LOWER.format(""));
Assert.assertEquals("lower", CaseChangers.LOWER.format("LOWER"));
Assert.assertEquals("lower {UPPER}", CaseChangers.LOWER.format("LOWER {UPPER}"));
Assert.assertEquals("lower {U}pper", CaseChangers.LOWER.format("LOWER {U}PPER"));
}

@Test
public void testChangeCaseUpper() {
Assert.assertEquals("", CaseChangers.UPPER.changeCase(""));
Assert.assertEquals("LOWER", CaseChangers.UPPER.changeCase("LOWER"));
Assert.assertEquals("UPPER", CaseChangers.UPPER.changeCase("upper"));
Assert.assertEquals("UPPER", CaseChangers.UPPER.changeCase("UPPER"));
Assert.assertEquals("UPPER {lower}", CaseChangers.UPPER.changeCase("upper {lower}"));
Assert.assertEquals("UPPER {l}OWER", CaseChangers.UPPER.changeCase("upper {l}ower"));
Assert.assertEquals("", CaseChangers.UPPER.format(""));
Assert.assertEquals("LOWER", CaseChangers.UPPER.format("LOWER"));
Assert.assertEquals("UPPER", CaseChangers.UPPER.format("upper"));
Assert.assertEquals("UPPER", CaseChangers.UPPER.format("UPPER"));
Assert.assertEquals("UPPER {lower}", CaseChangers.UPPER.format("upper {lower}"));
Assert.assertEquals("UPPER {l}OWER", CaseChangers.UPPER.format("upper {l}ower"));
}

@Test
public void testChangeCaseUpperFirst() {
Assert.assertEquals("", CaseChangers.UPPER_FIRST.changeCase(""));
Assert.assertEquals("Upper first", CaseChangers.UPPER_FIRST.changeCase("upper First"));
Assert.assertEquals("Upper first", CaseChangers.UPPER_FIRST.changeCase("uPPER FIRST"));
Assert.assertEquals("Upper {NOT} first", CaseChangers.UPPER_FIRST.changeCase("upper {NOT} FIRST"));
Assert.assertEquals("Upper {N}ot first", CaseChangers.UPPER_FIRST.changeCase("upper {N}OT FIRST"));
Assert.assertEquals("", CaseChangers.UPPER_FIRST.format(""));
Assert.assertEquals("Upper first", CaseChangers.UPPER_FIRST.format("upper First"));
Assert.assertEquals("Upper first", CaseChangers.UPPER_FIRST.format("uPPER FIRST"));
Assert.assertEquals("Upper {NOT} first", CaseChangers.UPPER_FIRST.format("upper {NOT} FIRST"));
Assert.assertEquals("Upper {N}ot first", CaseChangers.UPPER_FIRST.format("upper {N}OT FIRST"));
}

@Test
public void testChangeCaseUpperEachFirst() {
Assert.assertEquals("", CaseChangers.UPPER_EACH_FIRST.changeCase(""));
Assert.assertEquals("Upper Each First", CaseChangers.UPPER_EACH_FIRST.changeCase("upper each First"));
Assert.assertEquals("Upper Each First {NOT} {this}", CaseChangers.UPPER_EACH_FIRST.changeCase("upper each first {NOT} {this}"));
Assert.assertEquals("Upper Each First {N}ot {t}his", CaseChangers.UPPER_EACH_FIRST.changeCase("upper each first {N}OT {t}his"));
Assert.assertEquals("", CaseChangers.UPPER_EACH_FIRST.format(""));
Assert.assertEquals("Upper Each First", CaseChangers.UPPER_EACH_FIRST.format("upper each First"));
Assert.assertEquals("Upper Each First {NOT} {this}", CaseChangers.UPPER_EACH_FIRST.format("upper each first {NOT} {this}"));
Assert.assertEquals("Upper Each First {N}ot {t}his", CaseChangers.UPPER_EACH_FIRST.format("upper each first {N}OT {t}his"));
}

@Test
public void testChangeCaseTitle() {
Assert.assertEquals("", CaseChangers.TITLE.changeCase(""));
Assert.assertEquals("Upper Each First", CaseChangers.TITLE.changeCase("upper each first"));
Assert.assertEquals("An Upper Each First And", CaseChangers.TITLE.changeCase("an upper each first and"));
Assert.assertEquals("An Upper Each of the and First And", CaseChangers.TITLE.changeCase("an upper each of the and first and"));
Assert.assertEquals("An Upper Each of: The and First And", CaseChangers.TITLE.changeCase("an upper each of: the and first and"));
Assert.assertEquals("An Upper First with and without {CURLY} {brackets}", CaseChangers.TITLE.changeCase("AN UPPER FIRST WITH AND WITHOUT {CURLY} {brackets}"));
Assert.assertEquals("An Upper First with {A}nd without {C}urly {b}rackets", CaseChangers.TITLE.changeCase("AN UPPER FIRST WITH {A}ND WITHOUT {C}URLY {b}rackets"));
Assert.assertEquals("", CaseChangers.TITLE.format(""));
Assert.assertEquals("Upper Each First", CaseChangers.TITLE.format("upper each first"));
Assert.assertEquals("An Upper Each First And", CaseChangers.TITLE.format("an upper each first and"));
Assert.assertEquals("An Upper Each of the and First And", CaseChangers.TITLE.format("an upper each of the and first and"));
Assert.assertEquals("An Upper Each of: The and First And", CaseChangers.TITLE.format("an upper each of: the and first and"));
Assert.assertEquals("An Upper First with and without {CURLY} {brackets}", CaseChangers.TITLE.format("AN UPPER FIRST WITH AND WITHOUT {CURLY} {brackets}"));
Assert.assertEquals("An Upper First with {A}nd without {C}urly {b}rackets", CaseChangers.TITLE.format("AN UPPER FIRST WITH {A}ND WITHOUT {C}URLY {b}rackets"));
}
}

0 comments on commit f881803

Please sign in to comment.