11package com .force .i18n .grammar .impl ;
22
3- import java .io .IOException ;
4- import java .io .ObjectInputStream ;
5- import java .io .ObjectOutputStream ;
6- import java .io .Serializable ;
7- import java .util .Collection ;
8- import java .util .HashMap ;
9- import java .util .HashSet ;
10- import java .util .Map ;
11- import java .util .Set ;
3+ import static com .force .i18n .commons .util .settings .IniFileUtil .intern ;
4+
5+ import java .io .*;
6+ import java .util .*;
127
138import com .force .i18n .HumanLanguage ;
149import com .force .i18n .commons .util .collection .MapSerializer ;
15- import com .force .i18n .grammar .GrammaticalTerm ;
16- import com .force .i18n .grammar .GrammaticalTermMap ;
17- import com .force .i18n .grammar .LanguageDictionary ;
18- import com .force .i18n .grammar .Noun ;
19- import com .force .i18n .grammar .RenamingProvider ;
20- import com .google .common .collect .ImmutableMap ;
21-
22- import static com .force .i18n .commons .util .settings .IniFileUtil .intern ;
10+ import com .force .i18n .grammar .*;
11+ import com .google .common .collect .ImmutableSortedMap ;
2312
2413/**
2514 * In-memory version of GrammaticalTermMap
2615 *
2716 * @author ytanida
2817 */
29- public class GrammaticalTermMapImpl <T extends GrammaticalTerm > implements GrammaticalTermMap <T >, Serializable {
18+ public class GrammaticalTermMapImpl <T extends GrammaticalTerm > implements GrammaticalTermMap <T > {
3019 private static final long serialVersionUID = 2099717329853215271L ;
3120
3221 protected transient Map <String , T > map ;
3322 private boolean isSkinny = false ;
3423
3524 public GrammaticalTermMapImpl () {
36- map = new HashMap <>();
25+ map = new HashMap <>();
3726 }
3827
3928 public GrammaticalTermMapImpl (Map <String , T > map , boolean isSkinny ) {
4029 this .isSkinny = isSkinny ;
41- if (isSkinny )
42- this .map = new ImmutableMap .Builder <String , T >().putAll (map ).build ();
43- else
30+ if (isSkinny ) {
31+ // this uses "natural ordering" built-in comparator that ends up calling GrammaticalTerm.compareTo()
32+ // maybe better to specify comparator for just comparing GrammaticalTerm.getName() here
33+ this .map = ImmutableSortedMap .copyOf (map );
34+ } else {
4435 this .map = map ;
45-
36+ }
4637 }
4738
4839 @ Override
4940 public boolean equals (Object obj ) {
50- if (obj == this )
51- return true ;
52- if (!(obj instanceof GrammaticalTermMapImpl ))
53- return false ;
41+ if (obj == this ) return true ;
42+ if (!(obj instanceof GrammaticalTermMapImpl )) return false ;
5443
5544 @ SuppressWarnings ("unchecked" )
5645 GrammaticalTermMapImpl <T > other = (GrammaticalTermMapImpl <T >)obj ;
@@ -73,14 +62,15 @@ public GrammaticalTermMap<T> makeSkinny() {
7362 }
7463
7564 @ Override
76- public void writeJson (Appendable out , RenamingProvider renamingProvider , LanguageDictionary dictionary , Collection <String > termsToInclude ) throws IOException {
65+ public void writeJson (Appendable out , RenamingProvider renamingProvider , LanguageDictionary dictionary ,
66+ Collection <String > termsToInclude ) throws IOException {
7767 Set <String > wrote = new HashSet <>();
7868 out .append ('{' );
7969 if (termsToInclude != null ) {
8070 boolean first = true ;
8171 for (String name : termsToInclude ) {
8272 GrammaticalTerm term = map .get (name );
83- if (term != null ) {
73+ if (term != null ) {
8474 if (term instanceof Noun ) term = dictionary .getNounOverride ((Noun )term );
8575 if (!first ) {
8676 out .append (',' );
@@ -97,7 +87,7 @@ public void writeJson(Appendable out, RenamingProvider renamingProvider, Languag
9787 }
9888 out .append ('}' );
9989 }
100-
90+
10191 private void writeJson (Appendable out , RenamingProvider renamingProvider , HumanLanguage lang ) throws IOException {
10292 boolean first = true ;
10393 for (GrammaticalTerm term : map .values ()) {
@@ -109,8 +99,9 @@ private void writeJson(Appendable out, RenamingProvider renamingProvider, HumanL
10999 writeJsonTerm (out , renamingProvider , term , lang );
110100 }
111101 }
112-
113- private void writeJsonTerm (Appendable out , RenamingProvider renamingProvider , GrammaticalTerm term , HumanLanguage lang ) throws IOException {
102+
103+ private void writeJsonTerm (Appendable out , RenamingProvider renamingProvider , GrammaticalTerm term ,
104+ HumanLanguage lang ) throws IOException {
114105 if (renamingProvider != null && term instanceof Noun && renamingProvider .useRenamedNouns ()) {
115106 Noun renamedNoun = renamingProvider .getRenamedNoun (lang , ((Noun )term ).getName ());
116107 if (renamedNoun != null ) term = renamedNoun ;
@@ -129,7 +120,6 @@ public T get(String name) {
129120 return map .get (name );
130121 }
131122
132-
133123 @ Override
134124 public boolean containsKey (String name ) {
135125 return map .containsKey (name );
@@ -147,26 +137,24 @@ public Collection<T> values() {
147137
148138 @ Override
149139 public void put (String k , T v ) {
150- if (isSkinny )
151- throw new IllegalStateException ("This map is not able to modify" );
152- map .put (k ,v );
140+ if (isSkinny ) throw new IllegalStateException ("This map is not able to modify" );
141+ map .put (k , v );
153142 }
154-
155- @ SuppressWarnings ("unchecked" )
156- @ Override
143+
144+ @ Override
157145 public void putAll (GrammaticalTermMap <T > other ) {
158- if (isSkinny )
159- throw new IllegalStateException ("This map is not able to modify" );
146+ if (isSkinny ) throw new IllegalStateException ("This map is not able to modify" );
160147 map .putAll (((GrammaticalTermMapImpl <T >)other ).map );
161148 }
162-
149+
163150 @ Override
164151 public boolean isEmpty () {
165152 return map .isEmpty ();
166153 }
167154
168155 /**
169156 * Override default serializer to avoid any duplicated in the serialized map.
157+ *
170158 * @param in
171159 * @throws IOException
172160 */
@@ -178,6 +166,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
178166
179167 /**
180168 * Override default serializer to avoid any duplicated in the serialized map.
169+ *
181170 * @param in
182171 * @throws IOException
183172 */
0 commit comments