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-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 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  
 package org.kuali.rice.core.api.util.collect;
 17  
 
 18  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 19  
 
 20  
 import java.lang.reflect.Field;
 21  
 import java.lang.reflect.Modifier;
 22  
 import java.util.Collection;
 23  
 import java.util.Collections;
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 import java.util.Set;
 27  
 
 28  
 /**
 29  
  * This class is a map for Constant properties.
 30  
  * 
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 32  
  */
 33  
 //FIXME: make class threadsafe
 34  0
 public final class ConstantsMap implements Map<String, Object> {
 35  
     
 36  
         private Map<String, Object> map;
 37  
 
 38  
     public void setConstantClass(Class<?> constantClass) {
 39  0
         final Map<String, Object> m = new HashMap<String, Object>();
 40  
 
 41  0
             publishFields(m, constantClass);
 42  0
             map = Collections.unmodifiableMap(m);
 43  0
         }
 44  
 
 45  
     /**
 46  
      * Publishes all of the static, final, non-private fields of the given Class as entries in the given HashMap instance
 47  
      * 
 48  
      * @param constantMap
 49  
      * @param c
 50  
      */
 51  
     private void publishFields(Map<String, Object> constantMap, Class<?> c) {
 52  0
         final Field[] fields = c.getDeclaredFields();
 53  0
         for (final Field field : fields) {
 54  0
             final int modifier = field.getModifiers();
 55  
 
 56  
             // publish values of static, final, non-private members
 57  0
             if (Modifier.isStatic(modifier) && Modifier.isFinal(modifier) && !Modifier.isPrivate(modifier)) {
 58  
                 try {
 59  0
                     final String fieldName = field.getName();
 60  
 
 61  0
                     constantMap.put(fieldName, field.get(null));
 62  0
                 } catch (IllegalAccessException e) {
 63  0
                         throw new ConstantExporterException(e);
 64  0
                 }
 65  
             }
 66  
         }
 67  
 
 68  
         // publish values of appropriate fields of member classes
 69  0
         publishMemberClassFields(constantMap, c);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Publishes all of the static, final, non-private fields of the non-anonymous member classes of the given Class as entries in
 74  
      * the given HashMap instance
 75  
      * 
 76  
      * @param constantMap
 77  
      * @param c
 78  
      */
 79  
     private void publishMemberClassFields(Map<String, Object> constantMap, Class<?> c) {
 80  0
         final Class<?>[] memberClasses = c.getClasses();
 81  
 
 82  0
         for (final Class<?> memberClass : memberClasses) {
 83  0
             if (!memberClass.isAnonymousClass()) {
 84  0
                 final String memberPrefix = memberClass.getSimpleName();
 85  
 
 86  0
                 final Map<String, Object> subclassMap = new HashMap<String, Object>();
 87  0
                 publishFields(subclassMap, memberClass);
 88  0
                 constantMap.put(memberPrefix, Collections.unmodifiableMap(subclassMap));
 89  
             }
 90  
         }
 91  0
     }
 92  
         
 93  
     //delegate methods
 94  
     
 95  
         @Override
 96  
         public int size() {
 97  0
                 return this.map.size();
 98  
         }
 99  
 
 100  
         @Override
 101  
         public boolean isEmpty() {
 102  0
                 return this.map.isEmpty();
 103  
         }
 104  
 
 105  
         @Override
 106  
         public boolean containsKey(Object key) {
 107  0
                 return this.map.containsKey(key);
 108  
         }
 109  
 
 110  
         @Override
 111  
         public boolean containsValue(Object value) {
 112  0
                 return this.map.containsValue(value);
 113  
         }
 114  
 
 115  
         @Override
 116  
         public Object get(Object key) {
 117  0
                 return this.map.get(key);
 118  
         }
 119  
 
 120  
         @Override
 121  
         public Object put(String key, Object value) {
 122  0
                 return this.map.put(key, value);
 123  
         }
 124  
 
 125  
         @Override
 126  
         public Object remove(Object key) {
 127  0
                 return this.map.remove(key);
 128  
         }
 129  
 
 130  
         @Override
 131  
         public void putAll(Map<? extends String, ? extends Object> m) {
 132  0
                 this.map.putAll(m);
 133  0
         }
 134  
 
 135  
         @Override
 136  
         public void clear() {
 137  0
                 this.map.clear();
 138  0
         }
 139  
 
 140  
         @Override
 141  
         public Set<String> keySet() {
 142  0
                 return this.map.keySet();
 143  
         }
 144  
 
 145  
         @Override
 146  
         public Collection<Object> values() {
 147  0
                 return this.map.values();
 148  
         }
 149  
 
 150  
         @Override
 151  
         public Set<java.util.Map.Entry<String, Object>> entrySet() {
 152  0
                 return this.map.entrySet();
 153  
         }
 154  
 
 155  
         @Override
 156  
         public boolean equals(Object o) {
 157  0
                 return this.map.equals(o);
 158  
         }
 159  
 
 160  
         @Override
 161  
         public int hashCode() {
 162  0
                 return this.map.hashCode();
 163  
         }
 164  
 
 165  0
         private static class ConstantExporterException extends RiceRuntimeException {
 166  
 
 167  
                 private ConstantExporterException(Throwable t) {
 168  0
                         super(t);
 169  0
                 }
 170  
         }
 171  
 }