
de.alpharogroup.io.SerializedObjectExtensions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcommons-lang Show documentation
Show all versions of jcommons-lang Show documentation
Project that holds different usefull utility classes.
/**
* The MIT License
*
* Copyright (C) 2007 Asterios Raptis
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package de.alpharogroup.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.beanutils.BeanUtils;
import de.alpharogroup.file.FileConstants;
/**
* Helper-class for make exact copys from serialized objects.
*
* @version 1.0
* @author Asterios Raptis
*/
public final class SerializedObjectExtensions
{
/** The LOGGER. */
protected static final Logger LOGGER = Logger.getLogger(SerializedObjectExtensions.class.getName());
/**
* Copys the given Object and returns the copy from the object or null if the object can't be
* serialized.
*
* @param
* the generic type of the given object
* @param orig
* The object to copy.
* @return Returns a copy from the original object.
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ClassNotFoundException
* is thrown when a class is not found in the classloader or no definition for the
* class with the specified name could be found.
*/
@SuppressWarnings("unchecked")
public static T copySerializedObject(final T orig) throws IOException,
ClassNotFoundException
{
T object = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try
{
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(orig);
objectOutputStream.flush();
objectOutputStream.close();
final ByteArrayInputStream bis = new ByteArrayInputStream(
byteArrayOutputStream.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bis);
object = (T)ois.readObject();
}
finally
{
StreamExtensions.closeOutputStream(byteArrayOutputStream);
StreamExtensions.closeOutputStream(objectOutputStream);
}
return object;
}
/**
* Gets the changed data.
*
* @param sourceOjbect
* the source ojbect
* @param objectToCompare
* the object to compare
* @return the changed data
* @throws IllegalAccessException
* the illegal access exception
* @throws InvocationTargetException
* the invocation target exception
* @throws NoSuchMethodException
* the no such method exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getChangedData(final Object sourceOjbect,
final Object objectToCompare) throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException
{
final Map beanDescription = BeanUtils.describe(sourceOjbect);
beanDescription.remove("class");
final Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
clonedBeanDescription.remove("class");
final List changedData = new ArrayList<>();
for (final Object key : beanDescription.keySet())
{
final BeanComparator comparator = new BeanComparator(key.toString());
if (comparator.compare(sourceOjbect, objectToCompare) != 0)
{
final Object sourceAttribute = beanDescription.get(key);
final Object changedAttribute = clonedBeanDescription.get(key);
changedData.add(new SerializedChangedAttributeResult(key, sourceAttribute,
changedAttribute));
}
}
return changedData;
}
/**
* Compares the given two objects and gets the changed data.
*
* @param sourceOjbect
* the source ojbect
* @param objectToCompare
* the object to compare
* @return the changed data
* @throws IllegalAccessException
* the illegal access exception
* @throws InvocationTargetException
* the invocation target exception
* @throws NoSuchMethodException
* the no such method exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy