KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > Calculator


1 package org.apache.slide.projector.processor;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.slide.projector.Context;
6 import org.apache.slide.projector.Processor;
7 import org.apache.slide.projector.Result;
8 import org.apache.slide.projector.descriptor.NumberValueDescriptor;
9 import org.apache.slide.projector.descriptor.ParameterDescriptor;
10 import org.apache.slide.projector.descriptor.ResultDescriptor;
11 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
12 import org.apache.slide.projector.descriptor.StateDescriptor;
13 import org.apache.slide.projector.i18n.LocalizedMessage;
14 import org.apache.slide.projector.i18n.ParameterMessage;
15 import org.apache.slide.projector.value.NumberValue;
16
17 public class Calculator implements Processor {
18     private final static String JavaDoc FIRST_NUMBER = "firstNumber";
19     private final static String JavaDoc SECOND_NUMBER = "secondNumber";
20     private final static String JavaDoc CALCULATED_RESULT = "calculatedResult";
21
22     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[]{new ParameterDescriptor(
23             FIRST_NUMBER, new ParameterMessage(
24                     "calculator/parameter/firstNumber"),
25             new NumberValueDescriptor(new Integer JavaDoc(0), new Integer JavaDoc(5)))};
26     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(
27             new StateDescriptor[]{new StateDescriptor(StateDescriptor.OK,
28                     new LocalizedMessage("calculator/state/ok")),},
29             new ResultEntryDescriptor[]{new ResultEntryDescriptor(
30                     CALCULATED_RESULT, new LocalizedMessage(
31                             "calculator/result/calculatedResult"),
32                     NumberValue.CONTENT_TYPE, true)});
33
34     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
35         int firstInt = ((NumberValue) parameter.get(FIRST_NUMBER))
36                 .getNumber().intValue();
37         int secondInt = ((NumberValue) parameter.get(SECOND_NUMBER))
38                 .getNumber().intValue();
39         int calculatedResult = firstInt + secondInt;
40         return new Result(StateDescriptor.OK, CALCULATED_RESULT,
41                 new NumberValue(new Integer JavaDoc(calculatedResult)));
42     }
43
44     public ParameterDescriptor[] getParameterDescriptors() {
45         return parameterDescriptors;
46     }
47
48     public ResultDescriptor getResultDescriptor() {
49         return resultDescriptor;
50     }
51 }
Popular Tags