77
88public class TextResult {
99
10+ public static class Value {
11+
12+ private final String translation ;
13+ private final List <String > translations ;
14+ private final List <TextBlock > translationBlocks ;
15+
16+ public Value (String translation ) {
17+ this .translation = translation ;
18+ this .translations = null ;
19+ this .translationBlocks = null ;
20+ }
21+
22+ public Value (String [] translation ) {
23+ this .translation = null ;
24+ this .translations = Collections .unmodifiableList (Arrays .asList (translation ));
25+ this .translationBlocks = null ;
26+ }
27+
28+ public Value (TextBlock [] translation ) {
29+ this .translation = null ;
30+ this .translations = null ;
31+ this .translationBlocks = Collections .unmodifiableList (Arrays .asList (translation ));
32+ }
33+
34+ public String getTranslation () {
35+ if (translation != null )
36+ return translation ;
37+
38+ if (translations != null && !translations .isEmpty ()) {
39+ if (translations .size () != 1 )
40+ throw new IllegalStateException ("Cannot get translation from multiple elements (" + translations .size () + ")" );
41+ return translations .get (0 );
42+ }
43+
44+ if (translationBlocks != null && !translationBlocks .isEmpty ()) {
45+ if (translationBlocks .size () != 1 )
46+ throw new IllegalStateException ("Cannot get translation from multiple elements (" + translationBlocks .size () + ")" );
47+ return translationBlocks .get (0 ).getText ();
48+ }
49+
50+ return null ;
51+ }
52+
53+ public List <String > getTranslations () {
54+ if (translations != null )
55+ return translations ;
56+
57+ if (translation != null )
58+ return Collections .singletonList (translation );
59+
60+ if (translationBlocks != null && !translationBlocks .isEmpty ()) {
61+ List <String > result = new ArrayList <>(translationBlocks .size ());
62+ for (TextBlock block : translationBlocks )
63+ result .add (block .getText ());
64+
65+ return result ;
66+ }
67+
68+ return null ;
69+ }
70+
71+ public List <TextBlock > getTranslationBlocks () {
72+ if (translationBlocks != null )
73+ return translationBlocks ;
74+
75+ if (translation != null )
76+ return Collections .singletonList (new TextBlock (translation ));
77+
78+ if (translations != null && !translations .isEmpty ()) {
79+ List <TextBlock > result = new ArrayList <>(translations .size ());
80+ for (String translation : translations )
81+ result .add (new TextBlock (translation ));
82+
83+ return result ;
84+ }
85+
86+ return null ;
87+ }
88+
89+ @ Override
90+ public String toString () {
91+ if (translation != null ) return translation ;
92+ if (translations != null ) return translations .toString ();
93+ if (translationBlocks != null ) return translationBlocks .toString ();
94+ return null ;
95+ }
96+ }
97+
1098 private final String contentType ;
1199 private final String sourceLanguage ;
12100 private final List <String > adaptedTo ;
13101
14- private final String translation ;
15- private final List <String > translations ;
16- private final List <TextBlock > translationBlocks ;
102+ private final Value translation ;
17103
18104 public TextResult (String contentType , String sourceLanguage , String translation , String [] adaptedTo ) {
19105 this .contentType = contentType ;
20106 this .sourceLanguage = sourceLanguage ;
21107 this .adaptedTo = adaptedTo == null ? null : Collections .unmodifiableList (Arrays .asList (adaptedTo ));
22108
23- this .translation = translation ;
24- this .translations = null ;
25- this .translationBlocks = null ;
109+ this .translation = new Value (translation );
26110 }
27111
28112 public TextResult (String contentType , String sourceLanguage , String [] translation , String [] adaptedTo ) {
29113 this .contentType = contentType ;
30114 this .sourceLanguage = sourceLanguage ;
31115 this .adaptedTo = adaptedTo == null ? null : Collections .unmodifiableList (Arrays .asList (adaptedTo ));
32116
33- this .translation = null ;
34- this .translations = Collections .unmodifiableList (Arrays .asList (translation ));
35- this .translationBlocks = null ;
117+ this .translation = new Value (translation );
36118 }
37119
38120 public TextResult (String contentType , String sourceLanguage , TextBlock [] translation , String [] adaptedTo ) {
39121 this .contentType = contentType ;
40122 this .sourceLanguage = sourceLanguage ;
41123 this .adaptedTo = adaptedTo == null ? null : Collections .unmodifiableList (Arrays .asList (adaptedTo ));
42124
43- this .translation = null ;
44- this .translations = null ;
45- this .translationBlocks = Collections .unmodifiableList (Arrays .asList (translation ));
125+ this .translation = new Value (translation );
46126 }
47127
48128 public String getContentType () {
@@ -58,66 +138,20 @@ public List<String> getAdaptedTo() {
58138 }
59139
60140 public String getTranslation () {
61- if (translation != null )
62- return translation ;
63-
64- if (translations != null && !translations .isEmpty ()) {
65- if (translations .size () != 1 )
66- throw new IllegalStateException ("Cannot get translation from multiple elements (" + translations .size () + ")" );
67- return translations .get (0 );
68- }
69-
70- if (translationBlocks != null && !translationBlocks .isEmpty ()) {
71- if (translationBlocks .size () != 1 )
72- throw new IllegalStateException ("Cannot get translation from multiple elements (" + translationBlocks .size () + ")" );
73- return translationBlocks .get (0 ).getText ();
74- }
75-
76- return null ;
141+ return translation .getTranslation ();
77142 }
78143
79144 public List <String > getTranslations () {
80- if (translations != null )
81- return translations ;
82-
83- if (translation != null )
84- return Collections .singletonList (translation );
85-
86- if (translationBlocks != null && !translationBlocks .isEmpty ()) {
87- List <String > result = new ArrayList <>(translationBlocks .size ());
88- for (TextBlock block : translationBlocks )
89- result .add (block .getText ());
90-
91- return result ;
92- }
93-
94- return null ;
145+ return translation .getTranslations ();
95146 }
96147
97148 public List <TextBlock > getTranslationBlocks () {
98- if (translationBlocks != null )
99- return translationBlocks ;
100-
101- if (translation != null )
102- return Collections .singletonList (new TextBlock (translation ));
103-
104- if (translations != null && !translations .isEmpty ()) {
105- List <TextBlock > result = new ArrayList <>(translations .size ());
106- for (String translation : translations )
107- result .add (new TextBlock (translation ));
108-
109- return result ;
110- }
111-
112- return null ;
149+ return translation .getTranslationBlocks ();
113150 }
114151
115152 @ Override
116153 public String toString () {
117- if (translation != null ) return translation ;
118- if (translations != null ) return translations .toString ();
119- if (translationBlocks != null ) return translationBlocks .toString ();
120- return null ;
154+ return this .translation .toString ();
121155 }
122156
123157}
0 commit comments