Skip to content

Commit

Permalink
Merge pull request #38 from salesforce/fix-serialization
Browse files Browse the repository at this point in the history
Fix serialization
  • Loading branch information
yoikawa authored Jun 2, 2023
2 parents ec95248 + c4624ef commit f9117ca
Show file tree
Hide file tree
Showing 27 changed files with 392 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected AbstractLanguageDeclension(HumanLanguage language) {

@Override
public final HumanLanguage getLanguage() {
return this.language;
return this.language;
}

@Override
Expand Down Expand Up @@ -72,7 +72,7 @@ public Article createArticle(String name, LanguageArticle articleType) {

@Override
public boolean hasEndsWith() {
return false;
return false;
}

@Override
Expand All @@ -95,7 +95,7 @@ public boolean hasPlural() {

@Override
public Set<LanguageNumber> getAllowedNumbers() {
return hasPlural() ? LanguageNumber.PLURAL_SET : LanguageNumber.SINGULAR_SET;
return hasPlural() ? LanguageNumber.PLURAL_SET : LanguageNumber.SINGULAR_SET;
}


Expand Down Expand Up @@ -144,7 +144,7 @@ public EnumSet<LanguageGender> getRequiredGenders() {
return null; // Default
}

final static EnumSet<LanguageCase> NOMINATIVE_SET = EnumSet.of(LanguageCase.NOMINATIVE);
static final EnumSet<LanguageCase> NOMINATIVE_SET = EnumSet.of(LanguageCase.NOMINATIVE);
@Override
public EnumSet<LanguageCase> getRequiredCases() {
return NOMINATIVE_SET; // Nominative is usually required
Expand All @@ -155,13 +155,13 @@ public EnumSet<LanguageCase> getAllowedCases() {
return getRequiredCases();
}

final static EnumSet<LanguageStartsWith> CONSONANT_SET = EnumSet.of(LanguageStartsWith.CONSONANT);
static final EnumSet<LanguageStartsWith> CONSONANT_SET = EnumSet.of(LanguageStartsWith.CONSONANT);
@Override
public EnumSet<LanguageStartsWith> getRequiredStartsWith() {
return CONSONANT_SET; // Only generally care about consonant.
}

final static EnumSet<LanguagePossessive> POSSESSIVE_NONE_SET = EnumSet.of(LanguagePossessive.NONE);
static final EnumSet<LanguagePossessive> POSSESSIVE_NONE_SET = EnumSet.of(LanguagePossessive.NONE);
@Override
public EnumSet<LanguagePossessive> getRequiredPossessive() {
return POSSESSIVE_NONE_SET; // Doesn't matter much.
Expand Down Expand Up @@ -316,7 +316,7 @@ public NounForm getNounForm(NounForm nf) {

@Override
public int getMaxDistanceForModifiers() {
return isInflected() ? 0 : 5;
return isInflected() ? 0 : 5;
}

// Convenience method for retrieving an equivalent AdjectiveForm from this declension
Expand Down Expand Up @@ -399,9 +399,8 @@ public AdjectiveForm getApproximateAdjectiveForm(LanguageStartsWith startsWith,
if (baseForm == null && number != LanguageNumber.SINGULAR) {
baseForm = getAdjectiveForm(startsWithToTry, genderToTry, LanguageNumber.SINGULAR, caseToTry, articleToTry, possessiveToTry);
}
// OK, you asked for something that wasn't supported.
// OK, you asked for something that wasn't supported. simply fallback to the first one in the supported form list
if (baseForm == null) {
assert false : "Programmer error, you asked for an illegal adjective form";
baseForm = getAdjectiveForms().iterator().next();
}
return baseForm;
Expand Down Expand Up @@ -453,7 +452,7 @@ public String formLowercaseNounForm(String s, NounForm form) {

@Override
public final LanguagePluralRules getPluralRules() {
return LanguageProviderFactory.get().getPluralRules(getLanguage());
return LanguageProviderFactory.get().getPluralRules(getLanguage());
}

/**
Expand All @@ -462,7 +461,7 @@ public final LanguagePluralRules getPluralRules() {
* Often times, complex declension have simple forms of one thing or another.
* This is the "simple" form types for which there is only one possible declension
*/
public static enum SimpleModifierForm implements AdjectiveForm, ArticleForm {
public enum SimpleModifierForm implements AdjectiveForm, ArticleForm {
SINGULAR
;
@Override public LanguageArticle getArticle() { return LanguageArticle.ZERO;}
Expand Down Expand Up @@ -547,12 +546,13 @@ public LanguageStartsWith getStartsWith() {
* Simple noun form with singular and plurals
* @author stamm
*/
public static enum PluralNounForm implements NounForm {
public enum PluralNounForm implements NounForm {
SINGULAR(LanguageNumber.SINGULAR),
PLURAL(LanguageNumber.PLURAL),
;

private final LanguageNumber number;

private PluralNounForm(LanguageNumber number) {
this.number = number;
}
Expand Down Expand Up @@ -609,10 +609,6 @@ public SimplePluralNounWithGender(LanguageDeclension declension, String name, St
super(declension, name, pluralAlias, type, entityName, LanguageStartsWith.CONSONANT, gender, access, isStandardField, isCopiedFromDefault);
}

@Override
public void makeSkinny() {
}

@Override
public Map<? extends NounForm, String> getAllDefinedValues() {
// TODO: All the values, or just the interesting ones? The world may never know.
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/force/i18n/grammar/Adjective.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/*
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

Expand All @@ -21,11 +21,9 @@
* @author yoikawa,stamm
*/
public abstract class Adjective extends NounModifier {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(Adjective.class.getName());
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(Adjective.class.getName());

private final LanguagePosition position;

/**
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/force/i18n/grammar/ArticledDeclension.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* @author stamm
*/
public abstract class ArticledDeclension extends AbstractLanguageDeclension {
public ArticledDeclension(HumanLanguage language) {
super(language);
}
protected ArticledDeclension(HumanLanguage language) {
super(language);
}

@Override
@Override
public final boolean hasArticle() {
return true;
}
Expand Down Expand Up @@ -215,10 +215,6 @@ public Noun clone() {
return noun;
}

@Override
public void makeSkinny() {
}

@Override
protected Object readResolve() {
super.readResolve();
Expand Down
65 changes: 50 additions & 15 deletions src/main/java/com/force/i18n/grammar/GrammaticalTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import static com.force.i18n.commons.util.settings.IniFileUtil.intern;

import java.io.*;
import java.util.Objects;
import java.util.*;

import com.force.i18n.*;
import com.force.i18n.HumanLanguage;
import com.force.i18n.LanguageProviderFactory;
import com.force.i18n.grammar.impl.LanguageDeclensionFactory;
import com.google.common.collect.ImmutableSortedMap;

/**
* Represents a grammatical term; generally one that is declined based on a noun form or other
Expand All @@ -25,7 +27,7 @@
* @author stamm
*/
public abstract class GrammaticalTerm implements Serializable, Comparable<GrammaticalTerm> {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

private String name; // non-final. see readObject()
private transient LanguageDeclension declension;
Expand All @@ -34,11 +36,13 @@ public enum TermType {
Noun('n'),
Adjective('a'),
Article('d');
private final char id;
TermType(char id) {
this.id = id;
}
public char getCharId() {return this.id;}

private final char id;

TermType(char id) {
this.id = id;
}
public char getCharId() {return this.id;}
}

protected GrammaticalTerm(LanguageDeclension declension, String name) {
Expand Down Expand Up @@ -84,12 +88,12 @@ public LanguageDeclension getDeclension() {
}

@Override
public int compareTo(GrammaticalTerm o) {
TermType thisType = getTermType();
TermType oType = o.getTermType();
int typeComp = thisType.compareTo(oType);
return typeComp == 0 ? getName().compareTo(o.getName()) : typeComp;
}
public int compareTo(GrammaticalTerm o) {
TermType thisType = getTermType();
TermType oType = o.getTermType();
int typeComp = thisType.compareTo(oType);
return typeComp == 0 ? getName().compareTo(o.getName()) : typeComp;
}

@Override
public boolean equals(Object o) {
Expand All @@ -106,7 +110,7 @@ public int hashCode() {
return Objects.hash(this.declension.getLanguage().ordinal(), getTermType(), this.name);
}

public abstract void toJson(Appendable appendable) throws IOException;
public abstract void toJson(Appendable appendable) throws IOException;

private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
Expand All @@ -119,4 +123,35 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
HumanLanguage ul = LanguageProviderFactory.get().getProvider().getLanguage((String)in.readObject());
this.declension = LanguageDeclensionFactory.get().getDeclension(ul);
}

/**
* Provides clients the capability of indicating when members of Noun's can be converted to space efficient data
* structures.
*/
public void makeSkinny() {
// the default implementation does nothing
}

/**
* Utility method used to convert static {@link Map}'s concrete type to a {@link ImmutableSortedMap}.
* {@link ImmutableSortedMap} have a 8 byte overhead per element and are useful for reducing the per element
* overhead, that is traditionally high on most {@code Map} implementations.
*
* @param <T>
* the type of the grammatical form for this term
* @param map
* the map to make skinny
* @return A {@link ImmutableSortedMap} created from a {@link Map} of {@link GrammaticalForm}'s (key) to
* {@link String}'s (value).
*/
protected <T extends GrammaticalForm> Map<T, String> makeSkinny(Map<T, String> map) {
return ImmutableSortedMap.copyOf(map, new KeyComparator<T>());
}

private static class KeyComparator<T extends GrammaticalForm> implements Comparator<T>, Serializable {
@Override
public int compare(T o1, T o2) {
return o1.getKey().compareTo(o2.getKey());
}
}
}
Loading

0 comments on commit f9117ca

Please sign in to comment.