All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kapil.framework.lang.ObjectFactory Maven / Gradle / Ivy

Go to download

This is a set of utilities and classes that I have found useful over the years. In my career spanning over a decade, I have time and again written the same code or some part of the code over and over again. I never found the time to collate the details in a reusable library. This project will be a collection of such files. The work that I have been doing is more than 5 years old, however the project has been conceived in 2011.

There is a newer version: 1.2.1
Show newest version
/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 ******************************************************************************/
package com.kapil.framework.lang;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;

import com.kapil.framework.exception.impl.ObjectInitializationException;
import com.kapil.framework.logger.ILogger;
import com.kapil.framework.logger.LogFactory;

/**
 * 

* Utility class used to create objects of other classes. *

*/ @SuppressWarnings("rawtypes") public final class ObjectFactory { private static final ILogger LOGGER = LogFactory.getInstance().getLogger(ObjectFactory.class); /** *

* Made private to prevent from instances getting created *

* . */ private ObjectFactory() { } /** *

* Creates an instance of the class passed in the parameter. *

* * @param className - a {@link String} containing name of the class for which the instance has to be created * * @return initialized {@link Object} */ public static Object createInstance(final String className) { Object clazz; try { clazz = Class.forName(className).newInstance(); LOGGER.debug("Instace of class with the name [" + className + "] created successfully."); return clazz; } catch (InstantiationException iex) { throw new ObjectInitializationException(ObjectInitializationException.CLASS_NOT_FOUND, new String[] { className }, iex); } catch (IllegalAccessException iaex) { throw new ObjectInitializationException(ObjectInitializationException.ILLEGAL_ACCESS, new String[] { className }, iaex); } catch (ClassNotFoundException cnfex) { throw new ObjectInitializationException(ObjectInitializationException.CLASS_NOT_FOUND, new String[] { className }, cnfex); } } public static Object createInstance(final Class clazz) throws ObjectInitializationException { return createInstance(clazz.getCanonicalName()); } public static Object createInstance(final String className, final String data) throws ObjectInitializationException { Object obj = null; if (StringUtils.equalsIgnoreCase(data, "null")) { obj = null; } else if (StringUtils.equalsIgnoreCase(className, "long")) { obj = new Long(data); } else if (StringUtils.equalsIgnoreCase(className, "string")) { obj = new String(data); } else if (StringUtils.equalsIgnoreCase(className, "integer")) { obj = new Integer(data); } else if (StringUtils.equalsIgnoreCase(className, "boolean")) { if (data.equals(Boolean.TRUE)) { obj = Boolean.TRUE; } else { obj = Boolean.FALSE; } } else if (StringUtils.equalsIgnoreCase(className, "Date")) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, Integer.parseInt(data.substring(0, data.indexOf("-")))); c.set(Calendar.MONTH, Integer.parseInt(data.substring(data.indexOf("-") + 1, data.lastIndexOf("-"))) - 1); c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(data.substring(data.lastIndexOf("-") + 1, data.length()))); obj = c.getTime(); } return obj; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy