| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.org.usurper.setup; |
| 20 | |
|
| 21 | |
import java.util.HashMap; |
| 22 | |
import java.util.Map; |
| 23 | |
import java.util.Set; |
| 24 | |
|
| 25 | |
import org.org.usurper.handlers.IHandler; |
| 26 | |
import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler; |
| 27 | |
import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler; |
| 28 | |
import org.org.usurper.handlers.basic.ArrayHandler; |
| 29 | |
import org.org.usurper.handlers.basic.EnumHandler; |
| 30 | |
import org.org.usurper.model.IHandledEntity; |
| 31 | |
import org.org.usurper.model.ITargetDefinition; |
| 32 | |
import org.org.usurper.model.PropertyTypeDefinition; |
| 33 | |
import org.org.usurper.model.SpecificPropertyDefinition; |
| 34 | |
import org.org.usurper.setup.constants.OnMissingHandlers; |
| 35 | |
import org.org.usurper.setup.constants.PropertyWritingMechanism; |
| 36 | |
import org.org.usurper.setup.constants.UsurperGeneratorConstants; |
| 37 | |
import org.org.usurper.utils.UsurperGeneratorSetupUtils; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public class UsurperGeneratorSetup implements IUsurperGeneratorSetup { |
| 56 | |
|
| 57 | |
Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap; |
| 58 | |
Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> specificPropertyHandlersMap; |
| 59 | |
private ArrayHandler arrayHandler; |
| 60 | |
private EnumHandler enumHandler; |
| 61 | |
private OnMissingHandlers onMissingHandlers; |
| 62 | |
private PropertyWritingMechanism propertyWritingMechanism; |
| 63 | |
private ICountCallback countCallback; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 352 | public UsurperGeneratorSetup() { |
| 74 | 352 | this.propertyTypeHandlersMap = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>(10); |
| 75 | 352 | this.specificPropertyHandlersMap = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>(); |
| 76 | 352 | this.arrayHandler = new ArrayHandler(); |
| 77 | 352 | this.enumHandler = new EnumHandler(); |
| 78 | 352 | this.onMissingHandlers = OnMissingHandlers.FAIL; |
| 79 | 352 | this.propertyWritingMechanism = PropertyWritingMechanism.USE_SETTERS; |
| 80 | 352 | this.registerPropertyTypeHandlers(UsurperGeneratorConstants.DEFAULT_PROPERTY_HANDLERS); |
| 81 | 352 | this.countCallback = new ICountCallback() { |
| 82 | |
|
| 83 | 352 | public Integer determineCount(IHandledEntity handledEntity) { |
| 84 | 26 | return UsurperGeneratorConstants.DEFAULT_ENTRIES_COUNT; |
| 85 | |
} |
| 86 | |
|
| 87 | |
}; |
| 88 | 352 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | 3 | public UsurperGeneratorSetup(IUsurperGeneratorSetup immutableSetup) { |
| 96 | 3 | this.propertyTypeHandlersMap = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>(immutableSetup.getPropertyTypeHandlersMap()); |
| 97 | 3 | this.specificPropertyHandlersMap = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>(immutableSetup.getSpecificPropertyHandlersMap()); |
| 98 | 3 | this.arrayHandler = immutableSetup.getArrayHandler(); |
| 99 | 3 | this.enumHandler = immutableSetup.getEnumHandler(); |
| 100 | 3 | this.onMissingHandlers = immutableSetup.getOnMissingHandlers(); |
| 101 | 3 | this.propertyWritingMechanism = immutableSetup.getPropertyWritingMechanism(); |
| 102 | 3 | this.countCallback = immutableSetup.getCountCallback(); |
| 103 | 3 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> getPropertyTypeHandlersMap() { |
| 109 | 21 | return propertyTypeHandlersMap; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public void setPropertyTypeHandlersMap(Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap) { |
| 118 | 0 | this.propertyTypeHandlersMap = propertyTypeHandlersMap; |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> getSpecificPropertyHandlersMap() { |
| 125 | 21 | return specificPropertyHandlersMap; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public void registerPropertyTypeHandler(AbstractPropertyTypeHandler typeHandler) { |
| 136 | 4938 | for (PropertyTypeDefinition handledType : typeHandler.getHandledTypes()) { |
| 137 | 8122 | propertyTypeHandlersMap.put(handledType, typeHandler); |
| 138 | |
} |
| 139 | 4938 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public void registerPropertyTypeHandlers(Set<AbstractPropertyTypeHandler> typeHandlers) { |
| 149 | 353 | for (AbstractPropertyTypeHandler typeHandler : typeHandlers) { |
| 150 | 4929 | registerPropertyTypeHandler(typeHandler); |
| 151 | |
} |
| 152 | 353 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public boolean hasPropertyTypeHandler(PropertyTypeDefinition type) { |
| 158 | 0 | return this.propertyTypeHandlersMap.containsKey(type); |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public AbstractPropertyTypeHandler getPropertyTypeHandler(PropertyTypeDefinition handledType) { |
| 166 | 0 | return this.propertyTypeHandlersMap.get(handledType); |
| 167 | |
} |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public void registerSpecificPropertyHandler(AbstractSpecificPropertyHandler propertyHandler) { |
| 177 | 4 | this.specificPropertyHandlersMap.put(propertyHandler.getTargetProperty(), propertyHandler); |
| 178 | 4 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public Map<ITargetDefinition, IHandler> getAllHandlers() { |
| 185 | 0 | Map<ITargetDefinition, IHandler> result = new HashMap<ITargetDefinition, IHandler>(); |
| 186 | 0 | result.putAll(specificPropertyHandlersMap); |
| 187 | 0 | result.putAll(propertyTypeHandlersMap); |
| 188 | 0 | return result; |
| 189 | |
} |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public ImmutableUsurperGeneratorSetup getImmutable() { |
| 197 | 356 | return new ImmutableUsurperGeneratorSetup(propertyTypeHandlersMap, specificPropertyHandlersMap, arrayHandler, enumHandler, onMissingHandlers, propertyWritingMechanism, countCallback); |
| 198 | |
} |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public ArrayHandler getArrayHandler() { |
| 204 | 9 | return arrayHandler; |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
public void setArrayHandler(ArrayHandler arrayHandler) { |
| 213 | 6 | this.arrayHandler = arrayHandler; |
| 214 | 6 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public EnumHandler getEnumHandler() { |
| 220 | 9 | return enumHandler; |
| 221 | |
} |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
public void setEnumHandler(EnumHandler enumHandler) { |
| 229 | 0 | this.enumHandler = enumHandler; |
| 230 | 0 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public OnMissingHandlers getOnMissingHandlers() { |
| 236 | 9 | return onMissingHandlers; |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void setOnMissingHandlers(String onMissingHandlers) { |
| 245 | 0 | this.onMissingHandlers = OnMissingHandlers.valueOf(onMissingHandlers); |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
public void onMissingHandlers(OnMissingHandlers onMissingHandlers) { |
| 254 | 4 | this.onMissingHandlers = onMissingHandlers; |
| 255 | 4 | } |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
public PropertyWritingMechanism getPropertyWritingMechanism() { |
| 261 | 9 | return propertyWritingMechanism; |
| 262 | |
} |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public void setPropertyWritingMechanism(String propertyWritingMechanism) { |
| 270 | 0 | this.propertyWritingMechanism = PropertyWritingMechanism.valueOf(propertyWritingMechanism); |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
public void usePropertyWritingMechanism(PropertyWritingMechanism propertyWritingMechanism) { |
| 279 | 3 | this.propertyWritingMechanism = propertyWritingMechanism; |
| 280 | 3 | } |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
public ICountCallback getCountCallback() { |
| 286 | 9 | return countCallback; |
| 287 | |
} |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public void setCountCallback(ICountCallback countCallback) { |
| 295 | 3 | this.countCallback = countCallback; |
| 296 | 3 | } |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public boolean hasSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition) { |
| 302 | 0 | return this.specificPropertyHandlersMap.containsKey(specificPropertyDefinition); |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
public AbstractSpecificPropertyHandler getSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition) { |
| 309 | 0 | return this.specificPropertyHandlersMap.get(specificPropertyDefinition); |
| 310 | |
} |
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
public void setAllHandlers(Map<ITargetDefinition, IHandler> handlers) { |
| 318 | 306 | for (ITargetDefinition key : handlers.keySet()) { |
| 319 | 7905 | IHandler handler = handlers.get(key); |
| 320 | 7905 | if (key instanceof SpecificPropertyDefinition) { |
| 321 | 6 | if (handler instanceof AbstractSpecificPropertyHandler) { |
| 322 | 6 | specificPropertyHandlersMap.put((SpecificPropertyDefinition) key, (AbstractSpecificPropertyHandler) handler); |
| 323 | |
} else { |
| 324 | 0 | throw new ClassCastException("With SpecificPropertyDefinition key :<" + key + "> a " + AbstractSpecificPropertyHandler.class.getName() + " implementation should be defined"); |
| 325 | |
} |
| 326 | 7899 | } else if (key instanceof PropertyTypeDefinition) { |
| 327 | 7899 | if (handler instanceof AbstractPropertyTypeHandler) { |
| 328 | 7899 | propertyTypeHandlersMap.put((PropertyTypeDefinition) key, (AbstractPropertyTypeHandler) handler); |
| 329 | |
} else { |
| 330 | 0 | throw new ClassCastException("With PropertyTypeDefinition key :<" + key + "> a " + AbstractPropertyTypeHandler.class.getName() + " implementation should be defined"); |
| 331 | |
} |
| 332 | |
} else { |
| 333 | 0 | throw new ClassCastException("key should be a " + SpecificPropertyDefinition.class.getName() + " or a " + PropertyTypeDefinition.class.getName() + ", not a" + key.getClass().getName()); |
| 334 | |
} |
| 335 | 7905 | } |
| 336 | 306 | } |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
public String toStringRepresentation() { |
| 342 | 0 | return UsurperGeneratorSetupUtils.buildStringRepresentation(this); |
| 343 | |
} |
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
public String toString() { |
| 349 | 0 | return toStringRepresentation(); |
| 350 | |
} |
| 351 | |
} |