1 /* 2 * Copyright 2008 Lukas Lang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 * 14 */ 15 package org.castor.core.nature; 16 17 import java.util.HashMap; 18 import java.util.LinkedList; 19 import java.util.List; 20 import java.util.Map; 21 22 /** 23 * Adds property handle methods and a constructor including a health check. See constructor comments 24 * for more details. 25 * 26 * @author Lukas Lang 27 * @since 1.2.1 28 * 29 */ 30 public abstract class BaseNature implements Nature { 31 32 /** 33 * The PropertyHolder in focus. 34 */ 35 private PropertyHolder _holder = null; 36 37 /** 38 * Constructor taking a {@link PropertyHolder}. Must be called from subclasses. Does a health 39 * check on the given PropertyHolder, whether the extending Nature exists. 40 * 41 * @param holder a PropertyHolder. 42 */ 43 protected BaseNature(final PropertyHolder holder) { 44 if (holder == null) { 45 throw new NullPointerException("Holder must be set"); 46 } 47 // Health check 48 if (holder.hasNature(getId())) { 49 _holder = holder; 50 } else { 51 throw new IllegalStateException(getId() + " Nature must be set before"); 52 } 53 } 54 55 /** 56 * Returns the property mapped to the key or null if not set before. 57 * 58 * @param key to look up. 59 * @return value or null if not found. 60 */ 61 protected final Object getProperty(final String key) { 62 return _holder.getProperty(addPrefix(key)); 63 } 64 65 /** 66 * Sets the property for a given. Key must NOT be null, but can be an empty String. 67 * 68 * @param property to set. 69 * @param key to insert. 70 */ 71 protected final void setProperty(final String key, final Object property) { 72 if (key != null) { 73 _holder.setProperty(addPrefix(key), property); 74 } 75 } 76 77 /** 78 * Generates a Key by adding a prefix. 79 * 80 * @param key to use. 81 * @return prefix + given key. 82 */ 83 private String addPrefix(final String key) { 84 return new StringBuilder().append(getId()).append(key).toString(); 85 } 86 87 /** 88 * Returns boolean value of the property or false if property value is null. Make sure, not to 89 * request a property, which does not have a boolean value! 90 * 91 * @param propertyName name of the property. 92 * @return false if null or false. 93 */ 94 protected final boolean getBooleanPropertyDefaultFalse(final String propertyName) { 95 Boolean b = (Boolean) this.getProperty(propertyName); 96 if (b == null) { 97 return false; 98 } 99 return b.booleanValue(); 100 } 101 102 /** 103 * Returns the {@link PropertyHolder}. 104 * 105 * @return the holder 106 */ 107 protected final PropertyHolder getHolder() { 108 return _holder; 109 } 110 111 /** 112 * Returns value of the property as a List. If the property was not set before, a new List will be 113 * returned. Make sure, not to request a property, which is not a List! 114 * 115 * @param propertyName name of the property. 116 * @return A List. 117 */ 118 protected List getPropertyAsList(String property) { 119 List list = (List) getProperty(property); 120 if (list == null) { 121 list = new LinkedList(); 122 this.setProperty(property, list); 123 } 124 return list; 125 } 126 127 /** 128 * Returns value of the property as a List. If the property was not set before, a new List will be 129 * returned. Make sure, not to request a property, which is not a List! 130 * 131 * @param propertyName name of the property. 132 * @return A List. 133 */ 134 protected Map getPropertyAsMap(String property) { 135 Map map = (Map) getProperty(property); 136 if (map == null) { 137 map = new HashMap(); 138 this.setProperty(property, map); 139 } 140 return map; 141 } 142 143 }