Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.aries.blueprint.di;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 

Version:
$Rev: 896324 $ $Date: 2010-01-06 01:05:04 -0500 (Wed, 06 Jan 2010) $
 
 public class MapRecipe extends AbstractRecipe {
 
     private final List<Recipe[]> entries;
     private final Class typeClass;
 
     public MapRecipe(String nameClass type) {
         super(name);
         if (type == nullthrow new NullPointerException("type is null");
         this. = type;
         this. = new ArrayList<Recipe[]>();
     }
 
     public List<RecipegetDependencies() {
         List<RecipenestedRecipes = new ArrayList<Recipe>(.size() * 2);
         for (Recipe[] entry : ) {
             nestedRecipes.add(entry[0]);
             if (entry[1] != null) {
                 nestedRecipes.add(entry[1]);
             }
         }
         return nestedRecipes;
     }
 
     protected Object internalCreate() throws ComponentDefinitionException {
         Class mapType = getMap();
 
         if (!ReflectionUtils.hasDefaultConstructor(mapType)) {
             throw new ComponentDefinitionException("Type does not have a default constructor " + mapType.getName());
         }
 
         Object o;
         try {
             o = mapType.newInstance();
         } catch (Exception e) {
             throw new ComponentDefinitionException("Error while creating set instance: " + mapType.getName());
         }
 
         Map instance;
         if (o instanceof Map) {
             instance = (Mapo;
         } else if (o instanceof Dictionary) {
             instance = new DummyDictionaryAsMap((Dictionaryo);
         } else {
             throw new ComponentDefinitionException("Specified map type does not implement the Map interface: " + mapType.getName());
         }
 
         // add map entries
         for (Recipe[] entry : ) {
             Object key = entry[0].create();
             Object value = entry[1] != null ? entry[1].create() : null;
             instance.put(keyvalue);
         }
         return instance;
     }
 
     public void put(Recipe keyRecipe value) {
         if (key == nullthrow new NullPointerException("key is null");
         .add(new Recipe[] { keyvalue});
     }
 
     public void putAll(Map<Recipe,Recipemap) {
         if (map == nullthrow new NullPointerException("map is null");
        for (Map.Entry<Recipe,Recipeentry : map.entrySet()) {
            Recipe key = entry.getKey();
            Recipe value = entry.getValue();
            put(keyvalue);
        }
    }
    public static Class getMap(Class type) {
        if (ReflectionUtils.hasDefaultConstructor(type)) {
            return type;
        } else if (SortedMap.class.isAssignableFrom(type)) {
            return TreeMap.class;
        } else if (ConcurrentMap.class.isAssignableFrom(type)) {
            return ConcurrentHashMap.class;
        } else {
            return LinkedHashMap.class;
        }
    }
    public static class DummyDictionaryAsMap extends AbstractMap {
        private final Dictionary dictionary;
        public DummyDictionaryAsMap(Dictionary dictionary) {
            this. = dictionary;
        }
        @Override
        public Object put(Object keyObject value) {
            return .put(keyvalue);
        }
        public Set entrySet() {
            throw new UnsupportedOperationException();
        }
    }
New to GrepCode? Check out our FAQ X