Skip to content

Commit

Permalink
Resolving a bunch of small details.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Jun 16, 2024
1 parent 0ff6428 commit 65965ef
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 47 deletions.
15 changes: 8 additions & 7 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
module org.meeuw.i18n.languages {
exports org.meeuw.i18n.languages;
exports org.meeuw.i18n.languages.jaxb;

exports org.meeuw.i18n.languages.validation;

// deprecated language validator of regions depends on ours.
exports org.meeuw.i18n.languages.validation.impl to org.meeuw.i18n.regions;

requires static jakarta.validation;

requires static java.logging;
requires static org.checkerframework.checker.qual;

// xml binding (optional annotation)
requires static jakarta.xml.bind;

// jackson json binding (optional annotation)
requires static com.fasterxml.jackson.databind;

requires static java.compiler;

// but if used, these are needed too
opens org.meeuw.i18n.languages to com.fasterxml.jackson.databind;

// open to validator implementation
opens org.meeuw.i18n.languages.validation.impl;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/meeuw/i18n/languages/ISO_639_1_Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package org.meeuw.i18n.languages;

import java.util.List;
import javax.annotation.processing.Generated;


/**
Expand All @@ -10,6 +11,7 @@
* This enum is automatically generated by class org.meeuw.i18n.languages.test.GenerateEnumsTest from all ISO-639-3 languages which have a part1 code.
*
*/
@Generated("org.meeuw.i18n.languages.test.GenerateEnumsTest")
public enum ISO_639_1_Code
implements LanguageCode
{
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/meeuw/i18n/languages/ISO_639_3_Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Normally it makes sense to just use {@link LanguageCode}.
*/
@SuppressWarnings("DataFlowIssue")
public class ISO_639_3_Code implements LanguageCode {

final static Logger LOGGER = Logger.getLogger(LanguageCode.class.getName());
Expand All @@ -36,7 +37,7 @@ public class ISO_639_3_Code implements LanguageCode {
static {
Map<String, List<NameRecord>> namesMap = new HashMap<>();
try (InputStream inputStream = ISO_639_3_Code.class.getResourceAsStream(DIR + "iso-639-3_Name_Index.tab");
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
) {
String line = inputStreamReader.readLine();
while (line != null) {
Expand All @@ -51,7 +52,7 @@ public class ISO_639_3_Code implements LanguageCode {

Map<String, ISO_639_3_Code> temp = new HashMap<>();
try (InputStream inputStream = ISO_639_3_Code.class.getResourceAsStream(DIR + "iso-639-3.tab");
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
) {
inputStreamReader.readLine(); // skipheader;
String line = inputStreamReader.readLine();
Expand All @@ -60,9 +61,9 @@ public class ISO_639_3_Code implements LanguageCode {
List<NameRecord> names = namesMap.get(split[0]);
ISO_639_3_Code found = new ISO_639_3_Code(
split[0],
split[1].length() > 0 ? split[1] : null,
split[2].length() > 0 ? split[2] : null,
split[3].length() > 0 ? split[3] : null,
!split[1].isEmpty() ? split[1] : null,
!split[2].isEmpty() ? split[2] : null,
!split[3].isEmpty() ? split[3] : null,
Scope.valueOf(split[4]),
Type.valueOf(split[5]),
split[6],
Expand All @@ -80,7 +81,7 @@ public class ISO_639_3_Code implements LanguageCode {
Map<LanguageCode, List<LanguageCode>> tempIndividual = new HashMap<>();
Map<LanguageCode, List<LanguageCode>> tempMacro = new HashMap<>();
try (InputStream inputStream = ISO_639_3_Code.class.getResourceAsStream(DIR + "iso-639-3-macrolanguages.tab");
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
) {
inputStreamReader.readLine(); // skipheader;
String line = inputStreamReader.readLine();
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/meeuw/i18n/languages/ISO_639_Code.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.meeuw.i18n.languages;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.Serializable;
import java.util.Locale;

import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.meeuw.i18n.languages.jaxb.LanguageCodeAdapter;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* A code in the ISO-639 standard for {@link LanguageCode languages} and {@link LanguageFamilyCode language families}.
*/
@XmlJavaTypeAdapter(LanguageCodeAdapter.class)
public interface ISO_639_Code extends Serializable {


/**
* The code associated with this language or language family.
*
Expand All @@ -19,31 +24,31 @@ public interface ISO_639_Code extends Serializable {
*/
@JsonValue
String code();


@Override
String toString();

Scope scope();

Type languageType();

default String refName() {
return nameRecord(Locale.US).print();
}

default NameRecord nameRecord(Locale locale) {
return new NameRecord(toString());
}

default NameRecord nameRecord() {
return new NameRecord(toString());
}

@JsonCreator
static ISO_639_Code fromCode(String code) {
return ISO_639.iso639(code);
}


}
3 changes: 2 additions & 1 deletion src/main/java/org/meeuw/i18n/languages/LanguageCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public interface LanguageCode extends ISO_639_Code {
static void setFallbacks(final Map<String, LanguageCode> exemptions) {
ISO_639.setFallbacks(new AbstractMap<>() {
@Override
@NonNull
public Set<Entry<String, ISO_639_Code>> entrySet() {
return exemptions.entrySet()
.stream()
Expand Down Expand Up @@ -150,7 +151,7 @@ static void resetFallBacks() {
static Optional<LanguageCode> get(String code, boolean matchRetired) {
Optional<LanguageCode> optional;
if (code.length() == 2) {
optional = getByPart1(code);
optional = ISO_639.getByPart1(code);
} else {
Optional<LanguageCode> byPart3 = ISO_639.getByPart3(code, matchRetired);
if (byPart3.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* The main goal of this class is to be used in {@link ISO_639#getByPart3(String)}, which will return the unretired language code if possible.
*
*/
@SuppressWarnings("DataFlowIssue")
public class RetiredLanguageCode implements Serializable, LanguageCode {

static final Map<String, RetiredLanguageCode> KNOWN;
Expand Down Expand Up @@ -104,7 +105,7 @@ public String part1() {
public static Optional<RetiredLanguageCode> getByCode(String code) {
return Optional.ofNullable(KNOWN.get(code));
}

public RetirementReason retReason() {
return retReason;
}
Expand Down Expand Up @@ -140,7 +141,7 @@ public String toString() {
if (changeTo != null) {
try {
joiner.add("changeTo='" + changeTo().code() + "'");
} catch (RetirementException e) {
} catch (RetirementException ignored) {

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.meeuw.i18n.languages.validation.impl;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.*;
import java.util.logging.Logger;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import org.checkerframework.checker.nullness.qual.*;
import org.meeuw.i18n.languages.*;
import org.meeuw.i18n.languages.validation.Language;
Expand All @@ -20,7 +22,7 @@ public class LanguageValidator implements ConstraintValidator<Language, Object>

public static final String[] LEGACY = {"jw"}; // javanese?


@MonotonicNonNull
Language annotation;

Expand All @@ -44,11 +46,11 @@ public static boolean isValid(LanguageValidationInfo annotation, @Nullable Objec
if (language instanceof Locale) {
return isValid(annotation, ((Locale) language).getLanguage());
}

if (language instanceof CharSequence) {
return isValid(annotation, ((CharSequence) language));
}

if (language instanceof Iterable) {
boolean valid = true;
for (Object o : (Collection<?>) language) {
Expand All @@ -68,10 +70,10 @@ public static boolean isValid(LanguageValidationInfo annotation, @Nullable CharS
String splitter = annotation.forXml() ? "-" : "[_-]";
String[] components = value.split(splitter, 3);

if (!annotation.mayContainCountry() && components.length > 1 && components[1].length() > 0) {
if (!annotation.mayContainCountry() && components.length > 1 && !components[1].isEmpty()) {
return false;
}
if (!annotation.mayContainVariant() && components.length > 2 && components[2].length() > 0) {
if (!annotation.mayContainVariant() && components.length > 2 && !components[2].isEmpty()) {
return false;
}

Expand All @@ -85,7 +87,7 @@ public static boolean isValid(LanguageValidationInfo annotation, @Nullable CharS
if (annotation.forXml()) {
value = Locale.forLanguageTag(value).getLanguage();
}

Optional<? extends ISO_639_Code> languageCode = getLanguage(annotation, value);
if (languageCode.isPresent()) {
ISO_639_Code lc = languageCode.get();
Expand All @@ -103,11 +105,11 @@ public static boolean isValid(LanguageValidationInfo annotation, @Nullable CharS


}

private static Optional<? extends ISO_639_Code> getLanguage(LanguageValidationInfo annotation, String value) {
Optional<LanguageCode> iso3 = LanguageCode.getByPart1(value);


Optional<LanguageCode> iso3 = ISO_639.getByPart1(value);
if (iso3.isPresent()) {
return iso3;
}
Expand All @@ -122,7 +124,7 @@ private static Optional<? extends ISO_639_Code> getLanguage(LanguageValidationIn
if (isoPart2B.isPresent()) {
return isoPart2B;
}

Optional<LanguageCode> isoPart2T = ISO_639.getByPart2T(value);
if (isoPart2T.isPresent()) {
return isoPart2T;
Expand All @@ -134,7 +136,7 @@ private static Optional<? extends ISO_639_Code> getLanguage(LanguageValidationIn
return isoPart5;
}
}

if (annotation.lenient()) {
String displayLanguage = new Locale(value).getDisplayLanguage();
if (!value.equals(displayLanguage)) { // last fall back is iso code itself.
Expand All @@ -145,5 +147,5 @@ private static Optional<? extends ISO_639_Code> getLanguage(LanguageValidationIn
}
return Optional.empty();
}

}
5 changes: 3 additions & 2 deletions src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
requires org.assertj.core;
requires org.junit.jupiter.params;
requires jakarta.validation;


requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires com.sun.codemodel;
requires java.compiler;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.processing.Generated;

import org.junit.jupiter.api.Test;
import org.meeuw.i18n.languages.*;

Expand All @@ -28,6 +30,7 @@ public void create639_1() throws JClassAlreadyExistsException, IOException {
.append("An enum with all ISO-639-1 language codes.\n<p>\n")
.append("This enum is automatically generated by " + GenerateEnumsTest.class + " from all ISO-639-3 languages which have a part1 code.");

iso639_1.annotate(Generated.class).param("value", GenerateEnumsTest.class.getName());
JFieldVar languageCode = iso639_1.field(JMod.PRIVATE, ISO_639_3_Code.class, "languageCode");

JMethod constructor = iso639_1.constructor(JMod.NONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;
import org.meeuw.i18n.languages.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@SuppressWarnings("OptionalGetWithoutIsPresent")
class LanguageCodeTest {

Expand All @@ -18,7 +20,7 @@ public void example() {
Optional<LanguageCode> optional = ISO_639.getByPart3("nld");
LanguageCode languageCode = LanguageCode.languageCode("nl");

// show it 'inverted' name
// show its 'inverted' name
System.out.println(languageCode.nameRecord(Locale.US).inverted());

// get a language family
Expand Down

0 comments on commit 65965ef

Please sign in to comment.