1 /* 2 * Copyright 2008 Lukas Lang 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 org.castor.core.nature; 18 19 import java.util.HashMap; 20 import java.util.LinkedList; 21 import java.util.List; 22 import java.util.Map; 23 24 /** 25 * Adds property handle methods and a constructor including a health check. See 26 * constructor comments for more details. 27 * 28 * @author Lukas Lang 29 * @since 1.2.1 30 * 31 */ 32 public abstract class BaseNature implements Nature { 33 34 /** 35 * The PropertyHolder in focus. 36 */ 37 private PropertyHolder _holder = null; 38 39 /** 40 * Constructor taking a {@link PropertyHolder}. Must be called from 41 * subclasses. Does a health check on the given PropertyHolder, whether the 42 * extending Nature exists. 43 * 44 * @param holder 45 * a PropertyHolder. 46 */ 47 protected BaseNature(final PropertyHolder holder) { 48 if (holder == null) { 49 throw new NullPointerException("Holder must be set"); 50 } 51 // Health check 52 if (holder.hasNature(getId())) { 53 _holder = holder; 54 } else { 55 throw new IllegalStateException(getId() 56 + " Nature must be set before"); 57 } 58 } 59 60 /** 61 * Returns the property mapped to the key or null if not set before. 62 * 63 * @param key 64 * to look up. 65 * @return value or null if not found. 66 */ 67 protected final Object getProperty(final String key) { 68 return _holder.getProperty(addPrefix(key)); 69 } 70 71 /** 72 * Sets the property for a given. Key must NOT be null, but can be an empty 73 * String. 74 * 75 * @param property 76 * to set. 77 * @param key 78 * to insert. 79 */ 80 protected final void setProperty(final String key, final Object property) { 81 if (key != null) { 82 _holder.setProperty(addPrefix(key), property); 83 } 84 } 85 86 /** 87 * Generates a Key by adding a prefix. 88 * 89 * @param key 90 * to use. 91 * @return prefix + given key. 92 */ 93 private String addPrefix(final String key) { 94 StringBuffer buf = new StringBuffer(); 95 buf.append(getId()); 96 buf.append(key); 97 return buf.toString(); 98 } 99 100 /** 101 * Returns boolean value of the property or false if property value is null. 102 * Make sure, not to request a property, which does not have a boolean 103 * value! 104 * 105 * @param propertyName 106 * name of the property. 107 * @return false if null or false. 108 */ 109 protected final boolean getBooleanPropertyDefaultFalse( 110 final String propertyName) { 111 Boolean b = (Boolean) this.getProperty(propertyName); 112 if (b == null) { 113 return false; 114 } 115 return b.booleanValue(); 116 } 117 118 /** 119 * Returns the {@link PropertyHolder}. 120 * @return the holder 121 */ 122 protected final PropertyHolder getHolder() { 123 return _holder; 124 } 125 126 /** 127 * Returns value of the property as a List. If the property was not set 128 * before, a new List will be returned. Make sure, not to request a 129 * property, which is not a List! 130 * 131 * @param propertyName 132 * name of the property. 133 * @return A List. 134 */ 135 protected List getPropertyAsList(String property) { 136 List list = (List) getProperty(property); 137 if (list == null) { 138 list = new LinkedList(); 139 this.setProperty(property, list); 140 } 141 return list; 142 } 143 144 /** 145 * Returns value of the property as a List. If the property was not set 146 * before, a new List will be returned. Make sure, not to request a 147 * property, which is not a List! 148 * 149 * @param propertyName 150 * name of the property. 151 * @return A List. 152 */ 153 protected Map getPropertyAsMap(String property) { 154 Map map = (Map) getProperty(property); 155 if (map == null) { 156 map = new HashMap(); 157 this.setProperty(property, map); 158 } 159 return map; 160 } 161 162 }