Coverage Report - org.kuali.rice.core.api.util.collect.ConstantsMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ConstantsMap
0%
0/41
0%
0/12
1.444
ConstantsMap$1
N/A
N/A
1.444
ConstantsMap$ConstantExporterException
0%
0/3
N/A
1.444
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.api.util.collect;
 18  
 
 19  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 20  
 
 21  
 import java.lang.reflect.Field;
 22  
 import java.lang.reflect.Modifier;
 23  
 import java.util.Collection;
 24  
 import java.util.Collections;
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 
 29  
 /**
 30  
  * This class is a map for Constant properties.
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  */
 34  
 //FIXME: make class threadsafe
 35  0
 public final class ConstantsMap implements Map<String, Object> {
 36  
     
 37  
         private Map<String, Object> map;
 38  
 
 39  
     public void setConstantClass(Class<?> constantClass) {
 40  0
         final Map<String, Object> m = new HashMap<String, Object>();
 41  
 
 42  0
             publishFields(m, constantClass);
 43  0
             map = Collections.unmodifiableMap(m);
 44  0
         }
 45  
 
 46  
     /**
 47  
      * Publishes all of the static, final, non-private fields of the given Class as entries in the given HashMap instance
 48  
      * 
 49  
      * @param constantMap
 50  
      * @param c
 51  
      */
 52  
     private void publishFields(Map<String, Object> constantMap, Class<?> c) {
 53  0
         final Field[] fields = c.getDeclaredFields();
 54  0
         for (final Field field : fields) {
 55  0
             final int modifier = field.getModifiers();
 56  
 
 57  
             // publish values of static, final, non-private members
 58  0
             if (Modifier.isStatic(modifier) && Modifier.isFinal(modifier) && !Modifier.isPrivate(modifier)) {
 59  
                 try {
 60  0
                     final String fieldName = field.getName();
 61  
 
 62  0
                     constantMap.put(fieldName, field.get(null));
 63  0
                 } catch (IllegalAccessException e) {
 64  0
                         throw new ConstantExporterException(e);
 65  0
                 }
 66  
             }
 67  
         }
 68  
 
 69  
         // publish values of appropriate fields of member classes
 70  0
         publishMemberClassFields(constantMap, c);
 71  0
     }
 72  
 
 73  
     /**
 74  
      * Publishes all of the static, final, non-private fields of the non-anonymous member classes of the given Class as entries in
 75  
      * the given HashMap instance
 76  
      * 
 77  
      * @param constantMap
 78  
      * @param c
 79  
      */
 80  
     private void publishMemberClassFields(Map<String, Object> constantMap, Class<?> c) {
 81  0
         final Class<?>[] memberClasses = c.getClasses();
 82  
 
 83  0
         for (final Class<?> memberClass : memberClasses) {
 84  0
             if (!memberClass.isAnonymousClass()) {
 85  0
                 final String memberPrefix = memberClass.getSimpleName();
 86  
 
 87  0
                 final Map<String, Object> subclassMap = new HashMap<String, Object>();
 88  0
                 publishFields(subclassMap, memberClass);
 89  0
                 constantMap.put(memberPrefix, Collections.unmodifiableMap(subclassMap));
 90  
             }
 91  
         }
 92  0
     }
 93  
         
 94  
     //delegate methods
 95  
     
 96  
         @Override
 97  
         public int size() {
 98  0
                 return this.map.size();
 99  
         }
 100  
 
 101  
         @Override
 102  
         public boolean isEmpty() {
 103  0
                 return this.map.isEmpty();
 104  
         }
 105  
 
 106  
         @Override
 107  
         public boolean containsKey(Object key) {
 108  0
                 return this.map.containsKey(key);
 109  
         }
 110  
 
 111  
         @Override
 112  
         public boolean containsValue(Object value) {
 113  0
                 return this.map.containsValue(value);
 114  
         }
 115  
 
 116  
         @Override
 117  
         public Object get(Object key) {
 118  0
                 return this.map.get(key);
 119  
         }
 120  
 
 121  
         @Override
 122  
         public Object put(String key, Object value) {
 123  0
                 return this.map.put(key, value);
 124  
         }
 125  
 
 126  
         @Override
 127  
         public Object remove(Object key) {
 128  0
                 return this.map.remove(key);
 129  
         }
 130  
 
 131  
         @Override
 132  
         public void putAll(Map<? extends String, ? extends Object> m) {
 133  0
                 this.map.putAll(m);
 134  0
         }
 135  
 
 136  
         @Override
 137  
         public void clear() {
 138  0
                 this.map.clear();
 139  0
         }
 140  
 
 141  
         @Override
 142  
         public Set<String> keySet() {
 143  0
                 return this.map.keySet();
 144  
         }
 145  
 
 146  
         @Override
 147  
         public Collection<Object> values() {
 148  0
                 return this.map.values();
 149  
         }
 150  
 
 151  
         @Override
 152  
         public Set<java.util.Map.Entry<String, Object>> entrySet() {
 153  0
                 return this.map.entrySet();
 154  
         }
 155  
 
 156  
         @Override
 157  
         public boolean equals(Object o) {
 158  0
                 return this.map.equals(o);
 159  
         }
 160  
 
 161  
         @Override
 162  
         public int hashCode() {
 163  0
                 return this.map.hashCode();
 164  
         }
 165  
 
 166  0
         private static class ConstantExporterException extends RiceRuntimeException {
 167  
 
 168  
                 private ConstantExporterException(Throwable t) {
 169  0
                         super(t);
 170  0
                 }
 171  
         }
 172  
 }