1 | /** |
2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.deduced.viewer.web.shared; |
17 | |
18 | import java.io.Serializable; |
19 | |
20 | /** |
21 | * |
22 | * Serializer for generic values. Used to filter out which types GWT needs to |
23 | * support during serialization. |
24 | * |
25 | * @author Steve McDuff |
26 | * |
27 | */ |
28 | @SuppressWarnings("boxing") |
29 | public class Serializer |
30 | { |
31 | /** |
32 | * serialize String |
33 | * |
34 | * @param value the string value |
35 | * @return the serialized string |
36 | */ |
37 | public static StringSerializedValue serializeString( |
38 | String value) |
39 | { |
40 | StringSerializedValue retVal = new StringSerializedValue(); |
41 | retVal.setValue(value); |
42 | return retVal; |
43 | } |
44 | |
45 | /** |
46 | * serialize a value |
47 | * |
48 | * @param value the value to serialize |
49 | * @return the serialized value |
50 | */ |
51 | public static SerializedValue serialize( |
52 | Serializable value) |
53 | { |
54 | if (value == null) |
55 | { |
56 | StringSerializedValue retVal = new StringSerializedValue(); |
57 | return retVal; |
58 | } |
59 | else if (value.getClass() == String.class) |
60 | { |
61 | StringSerializedValue retVal = new StringSerializedValue(); |
62 | retVal.setValue((String) value); |
63 | return retVal; |
64 | } |
65 | else if (value.getClass() == Boolean.class) |
66 | { |
67 | if (Boolean.TRUE.equals(value)) |
68 | { |
69 | return BooleanSerializedValue.TRUE; |
70 | } |
71 | return BooleanSerializedValue.FALSE; |
72 | } |
73 | else if (value.getClass() == Byte.class) |
74 | { |
75 | ByteSerializedValue retVal = new ByteSerializedValue(); |
76 | retVal.setValue((Byte) value); |
77 | return retVal; |
78 | } |
79 | else if (value.getClass() == Short.class) |
80 | { |
81 | ShortSerializedValue retVal = new ShortSerializedValue(); |
82 | retVal.setValue((Short) value); |
83 | return retVal; |
84 | } |
85 | else if (value.getClass() == Integer.class) |
86 | { |
87 | IntegerSerializedValue retVal = new IntegerSerializedValue(); |
88 | retVal.setValue((Integer) value); |
89 | return retVal; |
90 | } |
91 | |
92 | return serializePart2(value); |
93 | } |
94 | |
95 | /** |
96 | * serialize Part : used to reduce the method complexity level |
97 | * |
98 | * @param value the value to serialize |
99 | * @return the serialized value |
100 | */ |
101 | private static SerializedValue serializePart2( |
102 | Serializable value) |
103 | { |
104 | if (value.getClass() == Long.class) |
105 | { |
106 | LongSerializedValue retVal = new LongSerializedValue(); |
107 | retVal.setValue((Long) value); |
108 | return retVal; |
109 | } |
110 | else if (value.getClass() == Float.class) |
111 | { |
112 | FloatSerializedValue retVal = new FloatSerializedValue(); |
113 | retVal.setValue((Float) value); |
114 | return retVal; |
115 | } |
116 | else if (value.getClass() == Double.class) |
117 | { |
118 | DoubleSerializedValue retVal = new DoubleSerializedValue(); |
119 | retVal.setValue((Double) value); |
120 | return retVal; |
121 | } |
122 | else if (value.getClass() == Character.class) |
123 | { |
124 | CharacterSerializedValue retVal = new CharacterSerializedValue(); |
125 | retVal.setValue((Character) value); |
126 | return retVal; |
127 | } |
128 | else if (value instanceof Model) |
129 | { |
130 | ModelSerializedValue retVal = new ModelSerializedValue(); |
131 | retVal.setValue((Model) value); |
132 | return retVal; |
133 | } |
134 | return null; |
135 | } |
136 | |
137 | /** |
138 | * |
139 | * Abstract Serialized Value |
140 | * |
141 | * @author Steve McDuff |
142 | * |
143 | */ |
144 | public abstract static class AbstractSerializedValue implements |
145 | SerializedValue, Serializable, Comparable<Object> |
146 | { |
147 | /** |
148 | * serialVersionUID |
149 | */ |
150 | private static final long serialVersionUID = 5766631445548524664L; |
151 | |
152 | /** |
153 | * (non-JSDoc) |
154 | * |
155 | * @see java.lang.Object#equals(java.lang.Object) |
156 | */ |
157 | @Override |
158 | public boolean equals( |
159 | Object obj) |
160 | { |
161 | if (obj == null) |
162 | { |
163 | return false; |
164 | } |
165 | |
166 | if (obj.getClass() != getClass()) |
167 | { |
168 | return false; |
169 | } |
170 | |
171 | SerializedValue other = (SerializedValue) obj; |
172 | |
173 | return Utilities.equals(getValue(), other.getValue()); |
174 | } |
175 | |
176 | /** |
177 | * (non-JSDoc) |
178 | * |
179 | * @see java.lang.Object#hashCode() |
180 | */ |
181 | @Override |
182 | public int hashCode() |
183 | { |
184 | Serializable value = getValue(); |
185 | if (value != null) |
186 | { |
187 | return value.hashCode(); |
188 | } |
189 | return 0; |
190 | } |
191 | |
192 | /** |
193 | * (non-JSDoc) |
194 | * |
195 | * @see java.lang.Comparable#compareTo(java.lang.Object) |
196 | */ |
197 | @SuppressWarnings("unchecked") |
198 | @Override |
199 | public int compareTo( |
200 | Object o) |
201 | { |
202 | SerializedValue other = (SerializedValue) o; |
203 | Comparable src = (Comparable) getValue(); |
204 | Comparable target = (Comparable) other.getValue(); |
205 | return src.compareTo(target); |
206 | } |
207 | } |
208 | |
209 | /** |
210 | * |
211 | * String Serialized Value |
212 | * |
213 | * @author Steve McDuff |
214 | * |
215 | */ |
216 | public static final class StringSerializedValue extends |
217 | AbstractSerializedValue |
218 | { |
219 | /** |
220 | * serialVersionUID |
221 | */ |
222 | private static final long serialVersionUID = -307275450342469724L; |
223 | /** |
224 | * value |
225 | */ |
226 | private String value; |
227 | |
228 | /** |
229 | * StringSerializedValue constructor |
230 | */ |
231 | public StringSerializedValue() |
232 | { |
233 | |
234 | } |
235 | |
236 | /** |
237 | * setValue |
238 | * |
239 | * @param setValue new value |
240 | */ |
241 | public void setValue( |
242 | String setValue) |
243 | { |
244 | value = setValue; |
245 | } |
246 | |
247 | /** |
248 | * (non-JSDoc) |
249 | * |
250 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
251 | */ |
252 | @Override |
253 | public String getValue() |
254 | { |
255 | return value; |
256 | } |
257 | } |
258 | |
259 | /** |
260 | * |
261 | * Boolean Serialized Value |
262 | * |
263 | * @author Steve McDuff |
264 | * |
265 | */ |
266 | public static final class BooleanSerializedValue extends |
267 | AbstractSerializedValue |
268 | { |
269 | /** |
270 | * serialVersionUID |
271 | */ |
272 | private static final long serialVersionUID = 8396414314342521367L; |
273 | /** |
274 | * value |
275 | */ |
276 | private boolean value; |
277 | |
278 | /** |
279 | * True value |
280 | */ |
281 | public static final BooleanSerializedValue TRUE = |
282 | new BooleanSerializedValue(); |
283 | |
284 | /** |
285 | * False value |
286 | */ |
287 | public static final BooleanSerializedValue FALSE = |
288 | new BooleanSerializedValue(); |
289 | |
290 | static |
291 | { |
292 | TRUE.setValue(Boolean.TRUE); |
293 | FALSE.setValue(Boolean.FALSE); |
294 | } |
295 | |
296 | /** |
297 | * BooleanSerializedValue constructor |
298 | */ |
299 | public BooleanSerializedValue() |
300 | { |
301 | |
302 | } |
303 | |
304 | /** |
305 | * setValue |
306 | * |
307 | * @param setValue new value |
308 | */ |
309 | public void setValue( |
310 | Boolean setValue) |
311 | { |
312 | value = setValue; |
313 | } |
314 | |
315 | /** |
316 | * (non-JSDoc) |
317 | * |
318 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
319 | */ |
320 | @Override |
321 | public Boolean getValue() |
322 | { |
323 | return value; |
324 | } |
325 | } |
326 | |
327 | /** |
328 | * |
329 | * Byte Serialized Value |
330 | * |
331 | * @author Steve McDuff |
332 | * |
333 | */ |
334 | public static final class ByteSerializedValue extends |
335 | AbstractSerializedValue |
336 | { |
337 | /** |
338 | * serialVersionUID |
339 | */ |
340 | private static final long serialVersionUID = 1065359836614829580L; |
341 | /** |
342 | * value |
343 | */ |
344 | private byte value; |
345 | |
346 | /** |
347 | * ByteSerializedValue constructor |
348 | */ |
349 | public ByteSerializedValue() |
350 | { |
351 | |
352 | } |
353 | |
354 | /** |
355 | * setValue |
356 | * |
357 | * @param setValue new value |
358 | */ |
359 | public void setValue( |
360 | Byte setValue) |
361 | { |
362 | value = setValue; |
363 | } |
364 | |
365 | /** |
366 | * (non-JSDoc) |
367 | * |
368 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
369 | */ |
370 | @Override |
371 | public Byte getValue() |
372 | { |
373 | return value; |
374 | } |
375 | } |
376 | |
377 | /** |
378 | * |
379 | * Short Serialized Value |
380 | * |
381 | * @author Steve McDuff |
382 | * |
383 | */ |
384 | public static final class ShortSerializedValue extends |
385 | AbstractSerializedValue |
386 | { |
387 | /** |
388 | * serialVersionUID |
389 | */ |
390 | private static final long serialVersionUID = -448451354344794612L; |
391 | /** |
392 | * value |
393 | */ |
394 | private short value; |
395 | |
396 | /** |
397 | * ShortSerializedValue constructor |
398 | */ |
399 | public ShortSerializedValue() |
400 | { |
401 | |
402 | } |
403 | |
404 | /** |
405 | * setValue |
406 | * |
407 | * @param setValue new value |
408 | */ |
409 | public void setValue( |
410 | Short setValue) |
411 | { |
412 | value = setValue; |
413 | } |
414 | |
415 | /** |
416 | * (non-JSDoc) |
417 | * |
418 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
419 | */ |
420 | @Override |
421 | public Short getValue() |
422 | { |
423 | return value; |
424 | } |
425 | } |
426 | |
427 | /** |
428 | * |
429 | * Integer Serialized Value |
430 | * |
431 | * @author Steve McDuff |
432 | * |
433 | */ |
434 | public static final class IntegerSerializedValue extends |
435 | AbstractSerializedValue |
436 | { |
437 | /** |
438 | * serialVersionUID |
439 | */ |
440 | private static final long serialVersionUID = 8877180268383686806L; |
441 | /** |
442 | * value |
443 | */ |
444 | private int value; |
445 | |
446 | /** |
447 | * IntegerSerializedValue constructor |
448 | */ |
449 | public IntegerSerializedValue() |
450 | { |
451 | |
452 | } |
453 | |
454 | /** |
455 | * setValue |
456 | * |
457 | * @param setValue new value |
458 | */ |
459 | public void setValue( |
460 | Integer setValue) |
461 | { |
462 | value = setValue; |
463 | } |
464 | |
465 | /** |
466 | * (non-JSDoc) |
467 | * |
468 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
469 | */ |
470 | @Override |
471 | public Integer getValue() |
472 | { |
473 | return value; |
474 | } |
475 | } |
476 | |
477 | /** |
478 | * |
479 | * Long Serialized Value |
480 | * |
481 | * @author Steve McDuff |
482 | * |
483 | */ |
484 | public static final class LongSerializedValue extends |
485 | AbstractSerializedValue |
486 | { |
487 | /** |
488 | * serialVersionUID |
489 | */ |
490 | private static final long serialVersionUID = 7187136526923758090L; |
491 | /** |
492 | * value |
493 | */ |
494 | private long value; |
495 | |
496 | /** |
497 | * LongSerializedValue constructor |
498 | */ |
499 | public LongSerializedValue() |
500 | { |
501 | |
502 | } |
503 | |
504 | /** |
505 | * setValue |
506 | * |
507 | * @param setValue new value |
508 | */ |
509 | public void setValue( |
510 | Long setValue) |
511 | { |
512 | value = setValue; |
513 | } |
514 | |
515 | /** |
516 | * (non-JSDoc) |
517 | * |
518 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
519 | */ |
520 | @Override |
521 | public Long getValue() |
522 | { |
523 | return value; |
524 | } |
525 | } |
526 | |
527 | /** |
528 | * |
529 | * Double Serialized Value |
530 | * |
531 | * @author Steve McDuff |
532 | * |
533 | */ |
534 | public static final class DoubleSerializedValue extends |
535 | AbstractSerializedValue |
536 | { |
537 | /** |
538 | * serialVersionUID |
539 | */ |
540 | private static final long serialVersionUID = 965935377492024825L; |
541 | /** |
542 | * value |
543 | */ |
544 | private double value; |
545 | |
546 | /** |
547 | * DoubleSerializedValue constructor |
548 | */ |
549 | public DoubleSerializedValue() |
550 | { |
551 | |
552 | } |
553 | |
554 | /** |
555 | * setValue |
556 | * |
557 | * @param setValue new value |
558 | */ |
559 | public void setValue( |
560 | Double setValue) |
561 | { |
562 | value = setValue; |
563 | } |
564 | |
565 | /** |
566 | * (non-JSDoc) |
567 | * |
568 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
569 | */ |
570 | @Override |
571 | public Double getValue() |
572 | { |
573 | return value; |
574 | } |
575 | } |
576 | |
577 | /** |
578 | * |
579 | * Float Serialized Value |
580 | * |
581 | * @author Steve McDuff |
582 | * |
583 | */ |
584 | public static final class FloatSerializedValue extends |
585 | AbstractSerializedValue |
586 | { |
587 | /** |
588 | * serialVersionUID |
589 | */ |
590 | private static final long serialVersionUID = 8173745975953768479L; |
591 | /** |
592 | * value |
593 | */ |
594 | private float value; |
595 | |
596 | /** |
597 | * FloatSerializedValue constructor |
598 | */ |
599 | public FloatSerializedValue() |
600 | { |
601 | |
602 | } |
603 | |
604 | /** |
605 | * setValue |
606 | * |
607 | * @param setValue new value |
608 | */ |
609 | public void setValue( |
610 | Float setValue) |
611 | { |
612 | value = setValue; |
613 | } |
614 | |
615 | /** |
616 | * (non-JSDoc) |
617 | * |
618 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
619 | */ |
620 | @Override |
621 | public Float getValue() |
622 | { |
623 | return value; |
624 | } |
625 | } |
626 | |
627 | /** |
628 | * |
629 | * Character Serialized Value |
630 | * |
631 | * @author Steve McDuff |
632 | * |
633 | */ |
634 | public static final class CharacterSerializedValue extends |
635 | AbstractSerializedValue |
636 | { |
637 | /** |
638 | * serialVersionUID |
639 | */ |
640 | private static final long serialVersionUID = -6137341634417088306L; |
641 | /** |
642 | * value |
643 | */ |
644 | private char value; |
645 | |
646 | /** |
647 | * CharacterSerializedValue constructor |
648 | */ |
649 | public CharacterSerializedValue() |
650 | { |
651 | |
652 | } |
653 | |
654 | /** |
655 | * setValue |
656 | * |
657 | * @param setValue new value |
658 | */ |
659 | public void setValue( |
660 | Character setValue) |
661 | { |
662 | value = setValue; |
663 | } |
664 | |
665 | /** |
666 | * (non-JSDoc) |
667 | * |
668 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
669 | */ |
670 | |
671 | @Override |
672 | public Character getValue() |
673 | { |
674 | return value; |
675 | } |
676 | } |
677 | |
678 | /** |
679 | * |
680 | * Model Serialized Value |
681 | * |
682 | * @author Steve McDuff |
683 | * |
684 | */ |
685 | public static final class ModelSerializedValue extends |
686 | AbstractSerializedValue |
687 | { |
688 | /** |
689 | * serialVersionUID |
690 | */ |
691 | private static final long serialVersionUID = 4047680732363295555L; |
692 | /** |
693 | * value |
694 | */ |
695 | private Model value; |
696 | |
697 | /** |
698 | * ModelSerializedValue constructor |
699 | */ |
700 | public ModelSerializedValue() |
701 | { |
702 | |
703 | } |
704 | |
705 | /** |
706 | * setValue |
707 | * |
708 | * @param setValue new value |
709 | */ |
710 | public void setValue( |
711 | Model setValue) |
712 | { |
713 | value = setValue; |
714 | } |
715 | |
716 | /** |
717 | * (non-JSDoc) |
718 | * |
719 | * @see org.deduced.viewer.web.shared.SerializedValue#getValue() |
720 | */ |
721 | @Override |
722 | public Model getValue() |
723 | { |
724 | return value; |
725 | } |
726 | } |
727 | |
728 | } |