• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
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 
17 package com.android.calculator2;
18 
19 import android.content.Context;
20 
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 
25 public class CalculatorExpressionTokenizer {
26 
27     private final Map<String, String> mReplacementMap;
28 
CalculatorExpressionTokenizer(Context context)29     public CalculatorExpressionTokenizer(Context context) {
30         mReplacementMap = new HashMap<>();
31 
32         mReplacementMap.put(".", context.getString(R.string.dec_point));
33 
34         mReplacementMap.put("0", context.getString(R.string.digit_0));
35         mReplacementMap.put("1", context.getString(R.string.digit_1));
36         mReplacementMap.put("2", context.getString(R.string.digit_2));
37         mReplacementMap.put("3", context.getString(R.string.digit_3));
38         mReplacementMap.put("4", context.getString(R.string.digit_4));
39         mReplacementMap.put("5", context.getString(R.string.digit_5));
40         mReplacementMap.put("6", context.getString(R.string.digit_6));
41         mReplacementMap.put("7", context.getString(R.string.digit_7));
42         mReplacementMap.put("8", context.getString(R.string.digit_8));
43         mReplacementMap.put("9", context.getString(R.string.digit_9));
44 
45         mReplacementMap.put("/", context.getString(R.string.op_div));
46         mReplacementMap.put("*", context.getString(R.string.op_mul));
47         mReplacementMap.put("-", context.getString(R.string.op_sub));
48 
49         mReplacementMap.put("cos", context.getString(R.string.fun_cos));
50         mReplacementMap.put("ln", context.getString(R.string.fun_ln));
51         mReplacementMap.put("log", context.getString(R.string.fun_log));
52         mReplacementMap.put("sin", context.getString(R.string.fun_sin));
53         mReplacementMap.put("tan", context.getString(R.string.fun_tan));
54 
55         mReplacementMap.put("Infinity", context.getString(R.string.inf));
56     }
57 
getNormalizedExpression(String expr)58     public String getNormalizedExpression(String expr) {
59         for (Entry<String, String> replacementEntry : mReplacementMap.entrySet()) {
60             expr = expr.replace(replacementEntry.getValue(), replacementEntry.getKey());
61         }
62         return expr;
63     }
64 
getLocalizedExpression(String expr)65     public String getLocalizedExpression(String expr) {
66         for (Entry<String, String> replacementEntry : mReplacementMap.entrySet()) {
67             expr = expr.replace(replacementEntry.getKey(), replacementEntry.getValue());
68         }
69         return expr;
70     }
71 }
72