/machinelearning/5.0.x/drools-core/src/main/java/org/drools/process/core/datatype/impl/InstanceDataTypeFactory.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 55 lines · 27 code · 8 blank · 20 comment · 2 complexity · da76b6d31ae5d5507605b76beadd126f MD5 · raw file

  1. package org.drools.process.core.datatype.impl;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
  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. import org.drools.process.core.datatype.DataType;
  18. import org.drools.process.core.datatype.DataTypeFactory;
  19. /**
  20. * A data type factory that always returns the same instance of a given class.
  21. *
  22. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  23. */
  24. public class InstanceDataTypeFactory implements DataTypeFactory {
  25. private static final long serialVersionUID = 400L;
  26. private Class<?> dataTypeClass;
  27. private DataType instance;
  28. public InstanceDataTypeFactory(final Class<?> dataTypeClass) {
  29. this.dataTypeClass = dataTypeClass;
  30. }
  31. public DataType createDataType() {
  32. if (this.instance == null) {
  33. try {
  34. this.instance = (DataType) this.dataTypeClass.newInstance();
  35. } catch (final IllegalAccessException e) {
  36. throw new RuntimeException(
  37. "Could not create data type for class "
  38. + this.dataTypeClass, e);
  39. } catch (final InstantiationException e) {
  40. throw new RuntimeException(
  41. "Could not create data type for class "
  42. + this.dataTypeClass, e);
  43. }
  44. }
  45. return this.instance;
  46. }
  47. }