Skip to content

Commit

Permalink
Replace Apache Commons Lang3 by Google Guava whereever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Jul 16, 2017
1 parent 4c805ef commit cd5a87c
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 37 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ dependencies {

compile 'commons-logging:commons-logging:1.2'

compile 'org.apache.commons:commons-lang3:3.6'

compile 'org.jsoup:jsoup:1.10.3'
compile 'com.mashape.unirest:unirest-java:1.4.9'
compile 'info.debatty:java-string-similarity:0.24'
Expand All @@ -152,6 +150,9 @@ dependencies {
testCompile 'com.github.tomakehurst:wiremock:2.6.0'
testCompile 'org.assertj:assertj-swing-junit:3.6.0'
testCompile 'org.reflections:reflections:0.9.11'

testCompile 'com.tngtech.archunit:archunit-junit:0.4.0'
testCompile 'org.slf4j:slf4j-jcl:1.7.25' // required by ArchUnit to enable logging over jcl
}

sourceSets {
Expand Down
3 changes: 3 additions & 0 deletions external-libraries.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
This document lists the fonts, icons, and libraries used by JabRef.
This file is manually kept in sync with build.gradle and the binary jars contained in the lib/ directory.

One can list all depdencies by using Gradle task `depdencyReport`.
It generated the file [build/reports/project/dependencies.txt](build/reports/project/dependencies.txt).

# Legend

## License
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import org.jabref.logic.util.BuildInfo;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.base.StandardSystemProperty;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.telemetry.SessionState;
import org.apache.commons.lang3.SystemUtils;

public class Globals {

Expand Down Expand Up @@ -87,11 +87,11 @@ private static void startTelemetryClient() {
telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.shouldCollectTelemetry());
telemetryClient = new TelemetryClient(telemetryConfiguration);
telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.getVersion().toString());
telemetryClient.getContext().getProperties().put("Java version", SystemUtils.JAVA_RUNTIME_VERSION);
telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value());
telemetryClient.getContext().getUser().setId(Globals.prefs.getOrCreateUserId());
telemetryClient.getContext().getSession().setId(UUID.randomUUID().toString());
telemetryClient.getContext().getDevice().setOperatingSystem(SystemUtils.OS_NAME);
telemetryClient.getContext().getDevice().setOperatingSystemVersion(SystemUtils.OS_VERSION);
telemetryClient.getContext().getDevice().setOperatingSystem(StandardSystemProperty.OS_NAME.value());
telemetryClient.getContext().getDevice().setOperatingSystemVersion(StandardSystemProperty.OS_VERSION.value());
telemetryClient.getContext().getDevice().setScreenResolution(
Toolkit.getDefaultToolkit().getScreenSize().toString());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jabref.architecture;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to indicate that usage of ApacheCommonsLang3 is explicitly allowed.
* The intention is to fully switch to Google Guava and only use Apache Commons Lang3 if there is no other possibility
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface ApacheCommonsLang3Allowed {

// The rationale
String value();

}
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.jabref.preferences.SearchPreferences;
import org.jabref.shared.prefs.SharedDatabasePreferences;

import org.apache.commons.lang3.exception.ExceptionUtils;
import com.google.common.base.Throwables;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -273,7 +273,7 @@ private boolean exportMatches(List<ParserResult> loaded) {
matches);
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file") + " '" + data[1] + "': "
+ ExceptionUtils.getStackTrace(ex));
+ Throwables.getStackTraceAsString(ex));
}
}
} else {
Expand Down Expand Up @@ -451,7 +451,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
} catch (Exception ex) {

System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': "
+ ExceptionUtils.getStackTrace(ex));
+ Throwables.getStackTraceAsString(ex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jabref.gui.IconTheme;
import org.jabref.logic.util.OS;

import org.apache.commons.lang3.exception.ExceptionUtils;
import com.google.common.base.Throwables;
import org.apache.logging.log4j.core.LogEvent;

public class LogEventViewModel {
Expand Down Expand Up @@ -46,7 +46,7 @@ public IconTheme.JabRefIcon getIcon() {
}

public Optional<String> getStackTrace() {
return Optional.ofNullable(logEvent.getMessage().getThrowable()).map(ExceptionUtils::getStackTrace);
return Optional.ofNullable(logEvent.getMessage().getThrowable()).map(Throwables::getStackTraceAsString);
}

public String getDetailedText() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.strings.StringUtil;

import com.google.common.base.Enums;
import com.google.common.eventbus.Subscribe;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import org.apache.commons.lang3.EnumUtils;
import org.fxmisc.easybind.EasyBind;

public class GroupNodeViewModel {
Expand Down Expand Up @@ -187,7 +187,7 @@ public MaterialDesignIcon getIcon() {
}

private Optional<MaterialDesignIcon> parseIcon(String iconCode) {
return Optional.ofNullable(EnumUtils.getEnum(MaterialDesignIcon.class, iconCode.toUpperCase(Locale.ENGLISH)));
return Enums.getIfPresent(MaterialDesignIcon.class, iconCode.toUpperCase(Locale.ENGLISH)).toJavaUtil();
}

public ObservableList<GroupNodeViewModel> getChildren() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/preftabs/PreviewPrefsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.jabref.logic.util.TestEntry;
import org.jabref.preferences.PreviewPreferences;

import com.google.common.primitives.Ints;
import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.factories.Paddings;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -95,7 +95,7 @@ private void setupLogic() {
chosenModel.add(newIndex, chosenModel.remove(oldIndex));
newSelectedIndices.add(newIndex);
}
chosen.setSelectedIndices(ArrayUtils.toPrimitive(newSelectedIndices.toArray(new Integer[newSelectedIndices.size()])));
chosen.setSelectedIndices(Ints.toArray(newSelectedIndices));
});

btnDown.addActionListener(event -> {
Expand All @@ -108,7 +108,7 @@ private void setupLogic() {
chosenModel.add(newIndex, chosenModel.remove(oldIndex));
newSelectedIndices.add(newIndex);
}
chosen.setSelectedIndices(ArrayUtils.toPrimitive(newSelectedIndices.toArray(new Integer[newSelectedIndices.size()])));
chosen.setSelectedIndices(Ints.toArray(newSelectedIndices));
});

btnDefault.addActionListener(event -> layout.setText(Globals.prefs.getPreviewPreferences()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.jabref.logic.formatter.bibtexfields;

import org.jabref.architecture.ApacheCommonsLang3Allowed;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.LayoutFormatter;
import org.jabref.model.cleanup.Formatter;

import org.apache.commons.lang3.StringEscapeUtils;

@ApacheCommonsLang3Allowed("There is no equivalent in Google's Guava")
public class HtmlToUnicodeFormatter implements LayoutFormatter, Formatter {

@Override
Expand Down
26 changes: 8 additions & 18 deletions src/main/java/org/jabref/logic/integrity/IntegrityMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

import org.jabref.model.entry.BibEntry;

import org.apache.commons.lang3.builder.EqualsBuilder;

public final class IntegrityMessage implements Cloneable {

private final BibEntry entry;

private final String fieldName;
private final String message;

Expand Down Expand Up @@ -41,22 +40,13 @@ public Object clone() {
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj.getClass() != getClass()) {
return false;
}
IntegrityMessage that = (IntegrityMessage) obj;
return new EqualsBuilder()
.append(entry, that.entry)
.append(fieldName, that.fieldName)
.append(message, that.message)
.isEquals();
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IntegrityMessage that = (IntegrityMessage) o;
return Objects.equals(entry, that.entry) &&
Objects.equals(fieldName, that.fieldName) &&
Objects.equals(message, that.message);
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/model/strings/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.common.base.CharMatcher;
import org.apache.commons.lang3.StringUtils;

//@ApacheCommonsLang3Allowed("There is no equivalent in Google's Guava")
public class StringUtil {

// Non-letters which are used to denote accents in LaTeX-commands, e.g., in {\"{a}}
Expand Down Expand Up @@ -624,7 +625,11 @@ public static boolean isBlank(Optional<String> string) {
return !isNotBlank(string);
}

/**
* Checks if a CharSequence is not empty (""), not null and not whitespace only.
*/
public static boolean isNotBlank(String string) {
// No Guava equivalent existing
return StringUtils.isNotBlank(string);
}

Expand Down Expand Up @@ -679,6 +684,7 @@ public static String unquote(String toUnquote, char quoteChar) {
}

public static String stripAccents(String searchQuery) {
// No Guava equivalent existing - see https://stackoverflow.com/q/3322152/873282 for a list of other implementations
return StringUtils.stripAccents(searchQuery);
}

Expand Down Expand Up @@ -708,6 +714,7 @@ public static List<String> getStringAsWords(String text) {
}

public static boolean containsIgnoreCase(String text, String searchString) {
// No direct Guava equivalent existing - see https://stackoverflow.com/q/16560635/873282
return StringUtils.containsIgnoreCase(text, searchString);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref;
package org.jabref.architecture;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jabref.architecture;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.junit.ArchUnitRunner;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.runner.RunWith;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;

@RunWith(ArchUnitRunner.class)
@AnalyzeClasses(packages = "org.jabref")
public class MainArchitectureTestsWithArchUnit {

@ArchTest
public static final ArchRule doNotUseApacheCommonsLang3 =
noClasses().that().areNotAnnotatedWith(ApacheCommonsLang3Allowed.class)
.should().accessClassesThat().resideInAPackage("org.apache.commons.lang3");

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.jabref;
package org.jabref.architecture;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -17,7 +17,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.jabref.MainArchitectureTests.CLASS_ORG_JABREF_GLOBALS;
import static org.jabref.architecture.MainArchitectureTests.CLASS_ORG_JABREF_GLOBALS;

@RunWith(Parameterized.class)
public class TestArchitectureTests {
Expand Down

0 comments on commit cd5a87c

Please sign in to comment.