KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > EvaluationResult


1 package org.jacorb.notification.filter;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23 import java.lang.reflect.Field JavaDoc;
24
25 import org.omg.CORBA.Any JavaDoc;
26 import org.omg.CORBA.TCKind JavaDoc;
27 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
28 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
29
30 /**
31  * @author Alphonse Bendt
32  * @version $Id: EvaluationResult.java,v 1.8 2005/05/01 21:51:46 alphonse.bendt Exp $
33  */

34
35 public class EvaluationResult
36 {
37     public static final EvaluationResult BOOL_TRUE;
38
39     public static final EvaluationResult BOOL_FALSE;
40
41     ////////////////////////////////////////
42

43     static
44     {
45         EvaluationResult _r = new EvaluationResult();
46         _r.setBool(true);
47         BOOL_TRUE = wrapImmutable(_r);
48
49         _r = new EvaluationResult();
50         _r.setBool(false);
51         BOOL_FALSE = wrapImmutable(_r);
52     }
53
54     ////////////////////////////////////////
55

56     private int typeCode_;
57
58     private Object JavaDoc value_;
59
60     private Any JavaDoc any_;
61
62     ////////////////////////////////////////
63

64     protected Object JavaDoc getValue()
65     {
66         return value_;
67     }
68
69     private Object JavaDoc setValue(Object JavaDoc value)
70     {
71         Object JavaDoc _old = value_;
72
73         value_ = value;
74
75         return _old;
76     }
77
78     public boolean isLongLong()
79     {
80         return typeCode_ == TCKind._tk_longlong;
81     }
82
83     public boolean isDouble()
84     {
85         return typeCode_ == TCKind._tk_double;
86     }
87
88     public boolean isFloat()
89     {
90         return typeCode_ == TCKind._tk_float;
91     }
92
93     public boolean isLong()
94     {
95         return typeCode_ == TCKind._tk_long;
96     }
97
98     public boolean isString()
99     {
100         return typeCode_ == TCKind._tk_string;
101     }
102
103     public void setString(String JavaDoc s)
104     {
105         setValue(s);
106         typeCode_ = TCKind._tk_string;
107     }
108
109     public void setFloat(float f)
110     {
111         setFloat(new Double JavaDoc(f));
112     }
113
114     public void setFloat(double d)
115     {
116         setFloat(new Double JavaDoc(d));
117     }
118
119     public void setFloat(Double JavaDoc d)
120     {
121         setValue(d);
122         typeCode_ = TCKind._tk_float;
123     }
124
125     public void setLongLong(long l)
126     {
127         setLongLong(new Double JavaDoc(l));
128     }
129
130     public void setLongLong(Double JavaDoc d)
131     {
132         setValue(d);
133         typeCode_ = TCKind._tk_longlong;
134     }
135
136     public void setLong(int l)
137     {
138         setLong(new Double JavaDoc(l));
139     }
140
141     public void setLong(Double JavaDoc d)
142     {
143         setValue(d);
144         typeCode_ = TCKind._tk_long;
145     }
146
147     public void setDouble(Double JavaDoc d)
148     {
149         setValue(d);
150         typeCode_ = TCKind._tk_double;
151     }
152
153     public void setDouble(double d)
154     {
155         setDouble(new Double JavaDoc(d));
156     }
157
158     public String JavaDoc getString() throws DynamicTypeException
159     {
160         try
161         {
162             return (String JavaDoc) getValue();
163         } catch (ClassCastException JavaDoc c)
164         {
165             throw newDynamicTypeException("String");
166         }
167     }
168
169     private DynamicTypeException newDynamicTypeException(String JavaDoc type)
170     {
171         return new DynamicTypeException("could not convert value: " + getValue() + " to " + type);
172     }
173
174     private static DynamicTypeException newDynamicTypeException(String JavaDoc operand,
175             EvaluationResult left, EvaluationResult right)
176     {
177         return new DynamicTypeException("failed to " + operand + " incompatible operands " + left
178                 + " and " + right);
179     }
180
181     public long getLongLong() throws DynamicTypeException
182     {
183         try
184         {
185             return ((Double JavaDoc) getValue()).longValue();
186         } catch (ClassCastException JavaDoc e)
187         {
188             // ignored. will retry
189
}
190
191         try
192         {
193             return ((Boolean JavaDoc) getValue()).booleanValue() ? 1l : 0;
194         } catch (ClassCastException JavaDoc e)
195         {
196             // ignored. will retry
197
}
198
199         try
200         {
201             String JavaDoc _s = (String JavaDoc) getValue();
202
203             if (_s.length() == 1)
204             {
205                 return _s.charAt(0);
206             }
207         } catch (ClassCastException JavaDoc e)
208         {
209             // ignored. will throw error
210
}
211
212         throw newDynamicTypeException("LongLong");
213     }
214
215     public int getLong() throws DynamicTypeException
216     {
217         if (getValue() != null)
218         {
219             try
220             {
221                 return ((Double JavaDoc) getValue()).intValue();
222             } catch (ClassCastException JavaDoc e)
223             {
224                 // ignored. will retry
225
}
226
227             try
228             {
229                 return ((Boolean JavaDoc) getValue()).booleanValue() ? 1 : 0;
230             } catch (ClassCastException JavaDoc e)
231             {
232                 // ignored. will retry
233
}
234
235             try
236             {
237                 String JavaDoc _s = (String JavaDoc) getValue();
238
239                 if (_s.length() == 1)
240                 {
241                     return _s.charAt(0);
242                 }
243             } catch (ClassCastException JavaDoc e)
244             {
245                 // ignored. will throw error
246
}
247
248         }
249         else
250         {
251             return any_.extract_long();
252         }
253
254         throw newDynamicTypeException("Long");
255     }
256
257     public double getDouble() throws DynamicTypeException
258     {
259         try
260         {
261             return ((Double JavaDoc) getValue()).doubleValue();
262         } catch (ClassCastException JavaDoc e)
263         {
264             // ignored. will retry.
265
}
266
267         try
268         {
269             return ((Boolean JavaDoc) getValue()).booleanValue() ? 1d : 0;
270         } catch (ClassCastException JavaDoc e)
271         {
272             // ignored. will retry.
273
}
274
275         try
276         {
277             String JavaDoc _s = (String JavaDoc) getValue();
278
279             if (_s.length() == 1)
280             {
281                 return _s.charAt(0);
282             }
283         } catch (ClassCastException JavaDoc e)
284         {
285             // ignored. will throw error
286
}
287
288         throw newDynamicTypeException("Double");
289     }
290
291     public float getFloat() throws DynamicTypeException
292     {
293         try
294         {
295             return ((Double JavaDoc) getValue()).floatValue();
296         } catch (ClassCastException JavaDoc c)
297         {
298             // ignored. will retry.
299
}
300
301         try
302         {
303             return ((Boolean JavaDoc) getValue()).booleanValue() ? 1f : 0;
304         } catch (ClassCastException JavaDoc c2)
305         {
306             // ignored. will retry.
307
}
308
309         try
310         {
311             String JavaDoc _s = (String JavaDoc) getValue();
312
313             if (_s.length() == 1)
314             {
315                 return _s.charAt(0);
316             }
317         } catch (ClassCastException JavaDoc c3)
318         {
319             // ignored. will throw error
320
}
321
322         throw newDynamicTypeException("Float");
323     }
324
325     public boolean getBool() throws DynamicTypeException
326     {
327         try
328         {
329             return ((Boolean JavaDoc) getValue()).booleanValue();
330         } catch (ClassCastException JavaDoc c)
331         {
332             // ignored. will throw error
333
}
334
335         throw newDynamicTypeException("Boolean");
336     }
337
338     public void setBool(boolean b)
339     {
340         if (b)
341         {
342             setValue(Boolean.TRUE);
343         }
344         else
345         {
346             setValue(Boolean.FALSE);
347         }
348
349         typeCode_ = TCKind._tk_boolean;
350     }
351
352     public Any JavaDoc getAny()
353     {
354         return any_;
355     }
356
357     public void addAny(Any JavaDoc any)
358     {
359         any_ = any;
360     }
361
362     private static String JavaDoc typeCodeToName(int x)
363     {
364         try
365         {
366             Field JavaDoc[] _fields = TCKind JavaDoc.class.getDeclaredFields();
367
368             return _fields[x].getName();
369         } catch (Exception JavaDoc e)
370         {
371             return "unknown: " + x;
372         }
373     }
374
375     public String JavaDoc toString()
376     {
377         StringBuffer JavaDoc _buffer = new StringBuffer JavaDoc("{");
378
379         _buffer.append(getValue());
380         _buffer.append(";TC=");
381         _buffer.append(typeCodeToName(typeCode_));
382         _buffer.append(";any=");
383         _buffer.append(any_);
384         _buffer.append("}");
385
386         return _buffer.toString();
387     }
388
389     public boolean equals(Object JavaDoc o)
390     {
391         if (o instanceof EvaluationResult)
392         {
393             return (((EvaluationResult) o).getValue().equals(getValue()));
394         }
395
396         return super.equals(o);
397     }
398
399     public int hashCode()
400     {
401         return getValue().hashCode();
402     }
403
404     public int compareTo(EvaluationResult other) throws DynamicTypeException, EvaluationException
405     {
406         int _ret = Integer.MAX_VALUE;
407
408         if (getValue() == null && any_ != null && other.getValue() instanceof String JavaDoc)
409         {
410             try
411             {
412                 String JavaDoc _l = any_.type().member_name(0);
413
414                 _ret = _l.compareTo(other.getString());
415             } catch (BadKind JavaDoc e)
416             {
417                 throw new EvaluationException(e);
418             } catch (Bounds JavaDoc e)
419             {
420                 throw new EvaluationException(e);
421             }
422
423         }
424         else if (isString() || other.isString())
425         {
426             _ret = getString().compareTo(other.getString());
427
428             if (_ret < -1)
429             {
430                 _ret = -1;
431             }
432             else if (_ret > 1)
433             {
434                 _ret = 1;
435             }
436         }
437         else if (isFloat() || other.isFloat())
438         {
439             float _l = getFloat();
440             float _r = other.getFloat();
441
442             if (_l < _r)
443             {
444                 _ret = -1;
445             }
446             else if (_l == _r)
447             {
448                 _ret = 0;
449             }
450             else
451             {
452                 _ret = 1;
453             }
454         }
455         else
456         {
457             int _l = getLong();
458             int _r = other.getLong();
459
460             if (_l < _r)
461             {
462                 _ret = -1;
463             }
464             else if (_l == _r)
465             {
466                 _ret = 0;
467             }
468             else
469             {
470                 _ret = 1;
471             }
472         }
473
474         if (_ret == Integer.MAX_VALUE)
475         {
476             throw newDynamicTypeException("compare", this, other);
477         }
478
479         return _ret;
480     }
481
482     public static EvaluationResult wrapImmutable(EvaluationResult e)
483     {
484         return new ImmutableEvaluationResultWrapper(e);
485     }
486
487     public static EvaluationResult plus(EvaluationResult left, EvaluationResult right)
488             throws DynamicTypeException
489     {
490         final EvaluationResult _res = new EvaluationResult();
491
492         if (left.isDouble() || right.isDouble())
493         {
494             _res.setDouble(left.getDouble() + right.getDouble());
495         }
496         else if (left.isFloat() || right.isFloat())
497         {
498             _res.setFloat(left.getDouble() + right.getDouble());
499         }
500         else if (left.isLongLong() || right.isLongLong())
501         {
502             _res.setLongLong(left.getLongLong() + right.getLongLong());
503         }
504         else if (left.isLong() || right.isLong())
505         {
506             _res.setLong(left.getLong() + right.getLong());
507         }
508         else
509         {
510             throw newDynamicTypeException("add", left, right);
511         }
512
513         return _res;
514     }
515
516     public static EvaluationResult minus(EvaluationResult left, EvaluationResult right)
517             throws DynamicTypeException
518     {
519         final EvaluationResult _res = new EvaluationResult();
520
521         if (left.isDouble() ||
522
523         right.isDouble())
524         {
525             _res.setDouble(left.getDouble() - right.getDouble());
526         }
527         else if (left.isFloat() || right.isFloat())
528         {
529             _res.setFloat(left.getDouble() - right.getDouble());
530         }
531         else if (left.isLongLong() || right.isLongLong())
532         {
533             _res.setLongLong(left.getLongLong() - right.getLongLong());
534         }
535         else if (left.isLong() || right.isLong())
536         {
537             _res.setLong(left.getLong() - right.getLong());
538         }
539         else
540         {
541             throw newDynamicTypeException("subtract", left, right);
542         }
543
544         return _res;
545     }
546
547     static public EvaluationResult unaryMinus(EvaluationResult r) throws DynamicTypeException
548     {
549         final EvaluationResult _ret = new EvaluationResult();
550
551         if (r.isFloat())
552         {
553             _ret.setFloat(-r.getFloat());
554         }
555         else
556         { // (r.isFloat()) {
557
_ret.setDouble(-r.getDouble());
558         }
559
560         return _ret;
561     }
562
563     static public EvaluationResult div(EvaluationResult left, EvaluationResult right)
564             throws DynamicTypeException
565     {
566         final EvaluationResult _res = new EvaluationResult();
567
568         if (left.isDouble() || right.isDouble())
569         {
570             _res.setDouble(left.getDouble() / right.getDouble());
571         }
572         else if (left.isFloat() || right.isFloat())
573         {
574             _res.setFloat(left.getDouble() / right.getDouble());
575         }
576         else if (left.isLongLong() || right.isLongLong())
577         {
578             _res.setLongLong(left.getLongLong() / right.getLongLong());
579         }
580         else if (left.isLong() || right.isLong())
581         {
582             _res.setLong(left.getLong() / right.getLong());
583         }
584         else
585         {
586             throw newDynamicTypeException("divide", left, right);
587         }
588
589         return _res;
590
591     }
592
593     static public EvaluationResult mult(EvaluationResult left, EvaluationResult right)
594             throws DynamicTypeException
595     {
596         final EvaluationResult _res = new EvaluationResult();
597
598         if (left.isDouble() || right.isDouble())
599         {
600             _res.setDouble(left.getDouble() * right.getDouble());
601         }
602         else if (left.isFloat() || right.isFloat())
603         {
604             _res.setFloat(left.getDouble() * right.getDouble());
605         }
606         else if (left.isLongLong() || right.isLongLong())
607         {
608             _res.setLongLong(left.getLongLong() * right.getLongLong());
609         }
610         else if (left.isLong() || right.isLong())
611         {
612             _res.setLong(left.getLong() * right.getLong());
613         }
614         else
615         {
616             throw newDynamicTypeException("multiply", left, right);
617         }
618
619         return _res;
620     }
621
622     public static EvaluationResult fromAny(Any JavaDoc any)
623     {
624         if (any == null)
625         {
626             return null;
627         }
628
629         final EvaluationResult result;
630
631         switch (any.type().kind().value()) {
632         case TCKind._tk_any:
633             result = fromAny(any.extract_any());
634
635             break;
636         default:
637             result = new EvaluationResult();
638
639             extractIntoEvaluationResult(result, any);
640         }
641         return result;
642     }
643
644     private static void extractIntoEvaluationResult(EvaluationResult er, Any JavaDoc any)
645     {
646         switch (any.type().kind().value()) {
647         case TCKind._tk_boolean:
648             er.setBool(any.extract_boolean());
649             break;
650         case TCKind._tk_string:
651             er.setString(any.extract_string());
652             break;
653         case TCKind._tk_long:
654             er.setLong(any.extract_long());
655             break;
656         case TCKind._tk_short:
657             er.setLong(any.extract_short());
658             break;
659         case TCKind._tk_ulonglong:
660             er.setLongLong(any.extract_ulonglong());
661             break;
662         default:
663             er.addAny(any);
664             break;
665         }
666     }
667 }
668
669 class ImmutableEvaluationResultWrapper extends EvaluationResult
670 {
671     private final EvaluationResult delegate_;
672
673     ////////////////////////////////////////
674

675     ImmutableEvaluationResultWrapper(EvaluationResult delegate)
676     {
677         delegate_ = delegate;
678     }
679
680     ////////////////////////////////////////
681

682     public Object JavaDoc getValue()
683     {
684         return delegate_.getValue();
685     }
686
687     public float getFloat() throws DynamicTypeException
688     {
689         return delegate_.getFloat();
690     }
691
692     public boolean equals(Object JavaDoc object)
693     {
694         return delegate_.equals(object);
695     }
696
697     public int hashCode()
698     {
699         return delegate_.hashCode();
700     }
701
702     public String JavaDoc toString()
703     {
704         return delegate_.toString();
705     }
706
707     public String JavaDoc getString() throws DynamicTypeException
708     {
709         return delegate_.getString();
710     }
711
712     public boolean isString()
713     {
714         return delegate_.isString();
715     }
716
717     public boolean isLong()
718     {
719         return delegate_.isLong();
720     }
721
722     public boolean isFloat()
723     {
724         return delegate_.isFloat();
725     }
726
727     public boolean isDouble()
728     {
729         return delegate_.isDouble();
730     }
731
732     public boolean getBool() throws DynamicTypeException
733     {
734         return delegate_.getBool();
735     }
736
737     public Any JavaDoc getAny()
738     {
739         return delegate_.getAny();
740     }
741
742     public void setString(String JavaDoc s)
743     {
744         unsupported();
745     }
746
747     public void setFloat(float f)
748     {
749         unsupported();
750     }
751
752     public void setFloat(Double JavaDoc d)
753     {
754         unsupported();
755     }
756
757     public void setInt(int i)
758     {
759         unsupported();
760     }
761
762     public void setInt(Double JavaDoc i)
763     {
764         unsupported();
765     }
766
767     public void setBool(boolean b)
768     {
769         unsupported();
770     }
771
772     public void addAny(Any JavaDoc a)
773     {
774         unsupported();
775     }
776
777     private static void unsupported()
778     {
779         throw new UnsupportedOperationException JavaDoc();
780     }
781 }
782
783 class DynamicTypeException extends EvaluationException
784 {
785     private static final long serialVersionUID = 1L;
786
787     public DynamicTypeException()
788     {
789         super();
790     }
791
792     public DynamicTypeException(String JavaDoc msg)
793     {
794         super(msg);
795     }
796 }
Popular Tags