]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | import org.apache.thrift.TException; | |
21 | ||
22 | // Generated code | |
23 | import tutorial.*; | |
24 | import shared.*; | |
25 | ||
26 | import java.util.HashMap; | |
27 | ||
28 | public class CalculatorHandler implements Calculator.Iface { | |
29 | ||
30 | private HashMap<Integer,SharedStruct> log; | |
31 | ||
32 | public CalculatorHandler() { | |
33 | log = new HashMap<Integer, SharedStruct>(); | |
34 | } | |
35 | ||
36 | public void ping() { | |
37 | System.out.println("ping()"); | |
38 | } | |
39 | ||
40 | public int add(int n1, int n2) { | |
41 | System.out.println("add(" + n1 + "," + n2 + ")"); | |
42 | return n1 + n2; | |
43 | } | |
44 | ||
45 | public int calculate(int logid, Work work) throws InvalidOperation { | |
46 | System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + "," + work.num2 + "})"); | |
47 | int val = 0; | |
48 | switch (work.op) { | |
49 | case ADD: | |
50 | val = work.num1 + work.num2; | |
51 | break; | |
52 | case SUBTRACT: | |
53 | val = work.num1 - work.num2; | |
54 | break; | |
55 | case MULTIPLY: | |
56 | val = work.num1 * work.num2; | |
57 | break; | |
58 | case DIVIDE: | |
59 | if (work.num2 == 0) { | |
60 | InvalidOperation io = new InvalidOperation(); | |
61 | io.whatOp = work.op.getValue(); | |
62 | io.why = "Cannot divide by 0"; | |
63 | throw io; | |
64 | } | |
65 | val = work.num1 / work.num2; | |
66 | break; | |
67 | default: | |
68 | InvalidOperation io = new InvalidOperation(); | |
69 | io.whatOp = work.op.getValue(); | |
70 | io.why = "Unknown operation"; | |
71 | throw io; | |
72 | } | |
73 | ||
74 | SharedStruct entry = new SharedStruct(); | |
75 | entry.key = logid; | |
76 | entry.value = Integer.toString(val); | |
77 | log.put(logid, entry); | |
78 | ||
79 | return val; | |
80 | } | |
81 | ||
82 | public SharedStruct getStruct(int key) { | |
83 | System.out.println("getStruct(" + key + ")"); | |
84 | return log.get(key); | |
85 | } | |
86 | ||
87 | public void zip() { | |
88 | System.out.println("zip()"); | |
89 | } | |
90 | ||
91 | } | |
92 |