| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JsonObjectToMapMorpher |
|
| 0.0;0 |
| 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 under the License. | |
| 17 | */ | |
| 18 | package org.apache.shindig.social.core.util; | |
| 19 | ||
| 20 | import com.google.common.collect.Maps; | |
| 21 | ||
| 22 | import java.util.HashMap; | |
| 23 | import java.util.Map; | |
| 24 | import java.util.Map.Entry; | |
| 25 | ||
| 26 | import net.sf.ezmorph.Morpher; | |
| 27 | import net.sf.ezmorph.ObjectMorpher; | |
| 28 | import net.sf.json.JSONObject; | |
| 29 | ||
| 30 | /** | |
| 31 | * A morpher that converts objects into maps | |
| 32 | */ | |
| 33 | 4 | public class JsonObjectToMapMorpher implements Morpher, ObjectMorpher { |
| 34 | ||
| 35 | /** | |
| 36 | * @return the class that the morper will morph to | |
| 37 | */ | |
| 38 | public Class<?> morphsTo() { | |
| 39 | 3 | return Map.class; |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * @param clazz the class being checked | |
| 44 | * @return true if this morpher supports the class | |
| 45 | */ | |
| 46 | @SuppressWarnings("unchecked") | |
| 47 | public boolean supports(Class clazz) { | |
| 48 | 3 | return (JSONObject.class.equals(clazz)); |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * @param the bean to be morphed | |
| 53 | * @return the morphed bean (a map) | |
| 54 | */ | |
| 55 | public Object morph(Object bean) { | |
| 56 | 2 | Map<Object, Object> result = Maps.newHashMap(); |
| 57 | 2 | JSONObject jsonObject = (JSONObject) bean; |
| 58 | 1 | for (Object entry : jsonObject.entrySet()) { |
| 59 | 2 | result.put(((Entry)entry).getKey(), ((Entry)entry).getValue()); |
| 60 | 2 | } |
| 61 | 1 | return result; |
| 62 | } | |
| 63 | ||
| 64 | } |