saberian | 7b5b77b | 2012-06-04 11:19:43 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 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 android.bordeaux.services; |
| 18 | |
| 19 | |
| 20 | import android.bordeaux.services.StringString; |
| 21 | import android.content.Context; |
| 22 | import android.util.Log; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.ArrayList; |
| 26 | |
| 27 | class AggregatorManager extends IAggregatorManager.Stub { |
| 28 | private final String TAG = "AggregatorMnager"; |
| 29 | private static HashMap<String, Aggregator> sFeatureMap; |
| 30 | private static AggregatorManager mManager = null; |
| 31 | |
| 32 | private AggregatorManager() { |
| 33 | sFeatureMap = new HashMap<String, Aggregator>(); |
| 34 | } |
| 35 | |
| 36 | public static AggregatorManager getInstance() { |
| 37 | if (mManager == null ) |
| 38 | mManager = new AggregatorManager(); |
| 39 | return mManager; |
| 40 | } |
| 41 | |
| 42 | public String[] getListOfFeatures() { |
| 43 | String[] s = new String[sFeatureMap.size()]; |
| 44 | int i = 0; |
| 45 | for (Map.Entry<String, Aggregator> x : sFeatureMap.entrySet()) { |
| 46 | s[i] = x.getKey(); |
| 47 | i++; |
| 48 | } |
| 49 | return s; |
| 50 | } |
| 51 | |
| 52 | public void registerAggregator(Aggregator agg, AggregatorManager m) { |
| 53 | agg.setManager(m); |
| 54 | String[] fl = agg.getListOfFeatures(); |
| 55 | for ( int i = 0; i< fl.length; i ++) |
| 56 | sFeatureMap.put(fl[i], agg); |
| 57 | } |
| 58 | |
| 59 | public ArrayList<StringString> getData(String dataName) { |
| 60 | return getList(getDataMap(dataName)); |
| 61 | } |
| 62 | |
| 63 | public Map<String, String> getDataMap(String dataName) { |
| 64 | if (sFeatureMap.get(dataName) != null) |
| 65 | return sFeatureMap.get(dataName).getFeatureValue(dataName); |
| 66 | else |
| 67 | Log.e(TAG, "There is no feature called " + dataName); |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | private ArrayList<StringString> getList(final Map<String, String> sample) { |
| 72 | ArrayList<StringString> StringString_sample = new ArrayList<StringString>(); |
| 73 | for (Map.Entry<String, String> x : sample.entrySet()) { |
| 74 | StringString v = new StringString(); |
| 75 | v.key = x.getKey(); |
| 76 | v.value = x.getValue(); |
| 77 | StringString_sample.add(v); |
| 78 | } |
| 79 | return StringString_sample; |
| 80 | } |
| 81 | } |