24
24
25
25
package org .restlet .ext .gwt ;
26
26
27
+ import static org .restlet .data .MediaType .APPLICATION_JAVA_OBJECT_GWT ;
28
+
27
29
import java .io .IOException ;
28
30
import java .io .Serializable ;
29
31
import java .util .List ;
47
49
public class GwtConverter extends ConverterHelper {
48
50
49
51
/** GWT variant. */
50
- private static final VariantInfo VARIANT_GWT = new VariantInfo (
51
- MediaType .APPLICATION_JAVA_OBJECT_GWT );
52
+ private static final VariantInfo VARIANT_GWT = new VariantInfo (APPLICATION_JAVA_OBJECT_GWT );
52
53
53
54
@ Override
54
55
public List <Class <?>> getObjectClasses (Variant source ) {
@@ -77,92 +78,81 @@ public List<VariantInfo> getVariants(Class<?> source) {
77
78
78
79
@ Override
79
80
public float score (Object source , Variant target , Resource resource ) {
80
- float result = -1.0F ;
81
81
82
82
if (source instanceof Serializable || source instanceof IsSerializable ) {
83
83
if (target == null ) {
84
- result = 0.5F ;
85
- } else if (MediaType .APPLICATION_JAVA_OBJECT_GWT .equals (target
86
- .getMediaType ())) {
87
- result = 1.0F ;
88
- } else if (MediaType .APPLICATION_JAVA_OBJECT_GWT
89
- .isCompatible (target .getMediaType ())) {
90
- result = 0.6F ;
91
- } else {
92
- result = 0.5F ;
84
+ return 0.5F ;
85
+ }
86
+ if (APPLICATION_JAVA_OBJECT_GWT .equals (target .getMediaType ())) {
87
+ return 1.0F ;
88
+ }
89
+ if (APPLICATION_JAVA_OBJECT_GWT .isCompatible (target .getMediaType ())) {
90
+ return 0.6F ;
93
91
}
92
+ return 0.5F ;
94
93
}
95
94
96
- return result ;
95
+ return - 1.0F ;
97
96
}
98
97
99
98
@ Override
100
- public <T > float score (Representation source , Class <T > target ,
101
- Resource resource ) {
102
- float result = -1.0F ;
103
-
99
+ public <T > float score (Representation source , Class <T > target , Resource resource ) {
104
100
if (source instanceof ObjectRepresentation <?>) {
105
- result = 1.0F ;
106
- } else if ((target != null )
107
- && ObjectRepresentation .class .isAssignableFrom (target )) {
108
- result = 1.0F ;
109
- } else if ((target != null )
110
- && (Serializable .class .isAssignableFrom (target ) || IsSerializable .class
111
- .isAssignableFrom (target ))) {
112
- if (MediaType .APPLICATION_JAVA_OBJECT_GWT .equals (source
113
- .getMediaType ())) {
114
- result = 1.0F ;
115
- } else if (MediaType .APPLICATION_JAVA_OBJECT_GWT
116
- .isCompatible (source .getMediaType ())) {
117
- result = 0.6F ;
101
+ return 1.0F ;
102
+ }
103
+
104
+ if (target != null ) {
105
+ if (ObjectRepresentation .class .isAssignableFrom (target )) {
106
+ return 1.0F ;
107
+ }
108
+ if (Serializable .class .isAssignableFrom (target )
109
+ || IsSerializable .class .isAssignableFrom (target )) {
110
+ if (APPLICATION_JAVA_OBJECT_GWT .equals (source .getMediaType ())) {
111
+ return 1.0F ;
112
+ }
113
+ if (APPLICATION_JAVA_OBJECT_GWT .isCompatible (source .getMediaType ())) {
114
+ return 0.6F ;
115
+ }
118
116
}
119
117
}
120
118
121
- return result ;
119
+ return - 1.0F ;
122
120
}
123
121
124
122
@ SuppressWarnings ("unchecked" )
125
123
@ Override
126
- public <T > T toObject (Representation source , Class <T > target ,
127
- Resource resource ) throws IOException {
128
- ObjectRepresentation <?> gwtSource = null ;
129
- if (source instanceof ObjectRepresentation <?>) {
130
- gwtSource = (ObjectRepresentation <?>) source ;
131
- } else {
132
- gwtSource = new ObjectRepresentation <T >(source .getText (), target );
133
- }
124
+ public <T > T toObject (Representation source , Class <T > target , Resource resource ) throws IOException {
125
+ ObjectRepresentation <?> gwtSource = (source instanceof ObjectRepresentation <?>) ?
126
+ (ObjectRepresentation <?>) source :
127
+ new ObjectRepresentation <T >(source .getText (), target );
134
128
135
- T result = null ;
136
- if (gwtSource != null ) {
137
- if (target == null ) {
138
- result = (T ) gwtSource .getObject ();
139
- } else if (ObjectRepresentation .class .isAssignableFrom (target )) {
140
- result = (T ) gwtSource ;
141
- } else if (Serializable .class .isAssignableFrom (target )
142
- || IsSerializable .class .isAssignableFrom (target )) {
143
- result = (T ) gwtSource .getObject ();
144
- }
129
+ if (target == null ) {
130
+ return (T ) gwtSource .getObject ();
131
+ }
132
+ if (ObjectRepresentation .class .isAssignableFrom (target )) {
133
+ return (T ) gwtSource ;
134
+ }
135
+ if (Serializable .class .isAssignableFrom (target )
136
+ || IsSerializable .class .isAssignableFrom (target )) {
137
+ return (T ) gwtSource .getObject ();
145
138
}
146
139
147
- return result ;
140
+ return null ;
148
141
}
149
142
150
143
@ Override
151
- public Representation toRepresentation (Object source , Variant target ,
152
- Resource resource ) {
153
- Representation result = null ;
154
-
144
+ public Representation toRepresentation (Object source , Variant target , Resource resource ) {
155
145
if (source instanceof Serializable ) {
156
- result = new ObjectRepresentation <Serializable >(
157
- ( Serializable ) source );
158
- } else if (source instanceof IsSerializable ) {
159
- result = new ObjectRepresentation <IsSerializable >(
160
- ( IsSerializable ) source );
161
- } else if (source instanceof Representation ) {
162
- result = (Representation ) source ;
146
+ return new ObjectRepresentation <Serializable >(( Serializable ) source );
147
+ }
148
+ if (source instanceof IsSerializable ) {
149
+ return new ObjectRepresentation <IsSerializable >(( IsSerializable ) source );
150
+ }
151
+ if (source instanceof Representation ) {
152
+ return (Representation ) source ;
163
153
}
164
154
165
- return result ;
155
+ return null ;
166
156
}
167
157
168
158
@ Override
@@ -171,8 +161,7 @@ public <T> void updatePreferences(List<Preference<MediaType>> preferences,
171
161
if (Serializable .class .isAssignableFrom (entity )
172
162
|| IsSerializable .class .isAssignableFrom (entity )
173
163
|| ObjectRepresentation .class .isAssignableFrom (entity )) {
174
- updatePreferences (preferences ,
175
- MediaType .APPLICATION_JAVA_OBJECT_GWT , 1.0F );
164
+ updatePreferences (preferences , APPLICATION_JAVA_OBJECT_GWT , 1.0F );
176
165
}
177
166
}
178
167
0 commit comments