Skip to content

Commit

Permalink
Lombok is great, but usage was too seldom, to have it in the project.
Browse files Browse the repository at this point in the history
Small refactoring and renaming
  • Loading branch information
Gmugra committed Dec 13, 2017
1 parent 3eec8f3 commit 54d38d0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.cactusthorn</groupId>
<artifactId>localization</artifactId>
<version>0.7</version>
<version>0.8</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -51,12 +51,12 @@
</plugins>
</build>
<dependencies>
<dependency>
<!--dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
</dependency-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import java.util.Locale;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import net.cactusthorn.localization.core.LocalizationKey;
import net.cactusthorn.localization.core.LocalizationKeys;

@Slf4j
public class LoggingLocalization extends BasicLocalization {

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoggingLocalization.class);

public LoggingLocalization(Map<Locale, LocalizationKeys> translations, String systemId, Path l10nDirectory, Charset charset) {
super(translations, systemId, l10nDirectory, charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import lombok.extern.slf4j.Slf4j;
import net.cactusthorn.localization.core.LocalizationKeys;

@Slf4j
//must be final, because start the thread in the constructor
public final class WatchLoggingLocalization extends LoggingLocalization implements Runnable {

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WatchLoggingLocalization.class);

private final WatchService watchService = FileSystems.getDefault().newWatchService();

private final Thread thread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
import org.apache.commons.text.translate.EntityArrays;
import org.apache.commons.text.translate.LookupTranslator;

import lombok.ToString;
import net.cactusthorn.localization.LocalizationException;
import net.cactusthorn.localization.Parameter;
import net.cactusthorn.localization.formats.Formats;

@ToString
public class LocalizationKey {

public static final String PS = "{{";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LocalizationKeys {

private Sys sys;
private Formats formats;
private Map<String,LocalizationKey> translations = new HashMap<>();
private Map<String,LocalizationKey> $keys = new HashMap<>();

public LocalizationKeys(String systemId, String languageTag, Map<String,String> properties) throws LocalizationException, ScriptException {

Expand All @@ -47,33 +47,33 @@ public void combineWith(LocalizationKeys keys ) {
sys.combineWith(keys.sys);
formats.combineWith(keys.formats);

keys.translations.entrySet().forEach(e -> { if (translations.containsKey(e.getKey())) { translations.get(e.getKey()).combineWith(e.getValue()); } } );
keys.translations.entrySet().forEach(e -> translations.putIfAbsent(e.getKey(), e.getValue() ) );
keys.$keys.entrySet().forEach(e -> { if ($keys.containsKey(e.getKey())) { $keys.get(e.getKey()).combineWith(e.getValue()); } } );
keys.$keys.entrySet().forEach(e -> $keys.putIfAbsent(e.getKey(), e.getValue() ) );
}

void addDefault(String key, String value, boolean escapeHtml) {
if (!translations.containsKey(key ) ) {
translations.put(key, new LocalizationKey(key));
if (!$keys.containsKey(key ) ) {
$keys.put(key, new LocalizationKey(key));
}
translations.get(key).setDefault(value, escapeHtml );
$keys.get(key).setDefault(value, escapeHtml );
}

void addPluralSpecial(String key, int special, String value, boolean escapeHtml) {
if (!translations.containsKey(key ) ) {
translations.put(key, new LocalizationKey(key));
if (!$keys.containsKey(key ) ) {
$keys.put(key, new LocalizationKey(key));
}
translations.get(key).addPluralSpecial(special, value, escapeHtml );
$keys.get(key).addPluralSpecial(special, value, escapeHtml );
}

void addPluralSpecial(String key, String special, String value, boolean escapeHtml) {
addPluralSpecial(key, Integer.parseInt(special), value, escapeHtml );
}

void addPlural(String key, int plural, String value, boolean escapeHtml) {
if (!translations.containsKey(key ) ) {
translations.put(key, new LocalizationKey(key));
if (!$keys.containsKey(key ) ) {
$keys.put(key, new LocalizationKey(key));
}
translations.get(key).addPlural(plural, value, escapeHtml );
$keys.get(key).addPlural(plural, value, escapeHtml );
}

public void addPlural(String key, String plural, String value, boolean escapeHtml) {
Expand All @@ -97,15 +97,15 @@ public String get(String key, boolean withFormatting, Parameter<?>... parameters
}

public String get(String key, boolean withFormatting, final Map<String, ?> params) {
LocalizationKey translation = translations.get(key);
LocalizationKey translation = $keys.get(key);
if (translation == null ) {
throw new LocalizationKeyException(sys.locale(), "unavailable key: " + key );
}
return translation.get(sys, withFormatting ? formats : null, params );
}

public String getDefault(String key) {
LocalizationKey translation = translations.get(key);
LocalizationKey translation = $keys.get(key);
if (translation == null ) {
throw new LocalizationKeyException(sys.locale(), "unavailable key: " + key );
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/net/cactusthorn/localization/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
import javax.script.ScriptException;
import javax.script.SimpleBindings;

import lombok.ToString;
import net.cactusthorn.localization.LocalizationException;

import javax.script.Compilable;
import javax.script.Bindings;

@ToString(exclude="pluralScript")
class Sys {

private CompiledScript pluralScript;
Expand Down Expand Up @@ -110,4 +108,15 @@ String languageTag() {
boolean escapeHtml() {
return escapeHtml;
}

@Override
public String toString() {
return "Sys(id=" + id
+ ", locale=" + locale
+ ", nplurals=" + nplurals
+ ", pluralExpression=" + pluralExpression
+ ", escapeHtml=" + escapeHtml + ")";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import java.util.Locale;
import java.util.Map;

import lombok.ToString;
import net.cactusthorn.localization.LocalizationFormatException;

@ToString
public class Formats {

private Locale locale;
Expand Down

0 comments on commit 54d38d0

Please sign in to comment.