Example usage for org.apache.commons.lang3 ObjectUtils cloneIfPossible

List of usage examples for org.apache.commons.lang3 ObjectUtils cloneIfPossible

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ObjectUtils cloneIfPossible.

Prototype

public static <T> T cloneIfPossible(final T obj) 

Source Link

Document

Clone an object if possible.

This method is similar to #clone(Object) , but will return the provided instance as the return value instead of null if the instance is not cloneable.

Usage

From source file:com.devicehive.model.websockets.InsertNotification.java

public Date getTimestamp() {
    return ObjectUtils.cloneIfPossible(this.timestamp);
}

From source file:com.devicehive.dao.riak.model.RiakDeviceEquipment.java

public Date getTimestamp() {
    return ObjectUtils.cloneIfPossible(timestamp);
}

From source file:com.devicehive.dao.riak.model.RiakDeviceEquipment.java

public void setTimestamp(Date timestamp) {
    this.timestamp = ObjectUtils.cloneIfPossible(timestamp);
}

From source file:com.devicehive.vo.ApiInfoVO.java

public Date getServerTimestamp() {
    return ObjectUtils.cloneIfPossible(serverTimestamp);
}

From source file:com.devicehive.vo.ApiInfoVO.java

public void setServerTimestamp(Date serverTimestamp) {
    this.serverTimestamp = ObjectUtils.cloneIfPossible(serverTimestamp);
}

From source file:com.devicehive.dao.riak.model.RiakUser.java

public Date getLastLogin() {
    return ObjectUtils.cloneIfPossible(lastLogin);
}

From source file:com.devicehive.dao.riak.model.RiakUser.java

public void setLastLogin(Date lastLogin) {
    this.lastLogin = ObjectUtils.cloneIfPossible(lastLogin);
}

From source file:org.jspresso.framework.model.entity.CarbonEntityCloneFactory.java

/**
 * Carbon copies all scalar properties.//from   www.  j a  v  a2  s .com
 *
 * @param componentToClone
 *          the source.
 * @param clonedComponent
 *          the copy.
 * @param entityFactory
 *          the entity factory to use.
 */
public static void carbonCopyComponent(IComponent componentToClone, IComponent clonedComponent,
        IEntityFactory entityFactory) {
    if (componentToClone == clonedComponent) {
        return;
    }
    IComponentDescriptor<?> componentDescriptor = entityFactory
            .getComponentDescriptor(componentToClone.getComponentContract());

    for (IPropertyDescriptor propertyDescriptor : componentDescriptor.getPropertyDescriptors()) {
        if (!(propertyDescriptor instanceof IRelationshipEndPropertyDescriptor)
                && /* propertyDescriptor.isModifiable() */!(propertyDescriptor.isComputed()
                        && propertyDescriptor.getPersistenceFormula() == null)) {
            String propertyName = propertyDescriptor.getName();
            clonedComponent.straightSetProperty(propertyName,
                    ObjectUtils.cloneIfPossible(componentToClone.straightGetProperty(propertyName)));
        }
    }
}

From source file:org.jspresso.framework.model.entity.SmartEntityCloneFactory.java

private <E extends IComponent> void handleRelationships(E componentToClone, E clonedComponent,
        IEntityFactory entityFactory) {/*from   ww w.  j a  v a  2s .c  o m*/
    IComponentDescriptor<?> componentDescriptor = entityFactory
            .getComponentDescriptor(componentToClone.getComponentContract());

    Map<Object, ICollectionPropertyDescriptor<?>> collRelToUpdate = new HashMap<>();
    for (Map.Entry<String, Object> propertyEntry : componentToClone.straightGetProperties().entrySet()) {
        if (propertyEntry.getValue() != null
                && !(IEntity.ID.equals(propertyEntry.getKey()) || IEntity.VERSION.equals(propertyEntry.getKey())
                        || componentDescriptor.getUnclonedProperties().contains(propertyEntry.getKey()))) {
            IPropertyDescriptor propertyDescriptor = componentDescriptor
                    .getPropertyDescriptor(propertyEntry.getKey());
            if (propertyDescriptor instanceof IRelationshipEndPropertyDescriptor) {
                if (propertyEntry.getValue() instanceof IComponent
                        && !(propertyEntry.getValue() instanceof IEntity)) {
                    clonedComponent.straightSetProperty(propertyEntry.getKey(),
                            cloneComponent((IComponent) propertyEntry.getValue(), entityFactory));
                } else {
                    IRelationshipEndPropertyDescriptor reverseDescriptor = ((IRelationshipEndPropertyDescriptor) propertyDescriptor)
                            .getReverseRelationEnd();
                    if (propertyDescriptor instanceof IReferencePropertyDescriptor<?>) {
                        if (!(reverseDescriptor instanceof IReferencePropertyDescriptor<?>)) {
                            if (((IRelationshipEndPropertyDescriptor) propertyDescriptor).isComposition()) {
                                clonedComponent.straightSetProperty(propertyEntry.getKey(),
                                        cloneEntity((IEntity) propertyEntry.getValue(), entityFactory));
                            } else {
                                clonedComponent.straightSetProperty(propertyEntry.getKey(),
                                        propertyEntry.getValue());
                                if (reverseDescriptor instanceof ICollectionPropertyDescriptor<?>) {
                                    if (isInitialized(propertyEntry.getValue())) {
                                        collRelToUpdate.put(propertyEntry.getValue(),
                                                (ICollectionPropertyDescriptor<?>) reverseDescriptor);
                                    }
                                }
                            }
                        }
                    } else if (propertyDescriptor instanceof ICollectionPropertyDescriptor<?>) {
                        if (reverseDescriptor instanceof ICollectionPropertyDescriptor<?>) {
                            // We must force initialization of the collection. So do a get.
                            try {
                                entityFactory.getAccessorFactory()
                                        .createPropertyAccessor(propertyEntry.getKey(),
                                                componentToClone.getComponentContract())
                                        .getValue(componentToClone);
                            } catch (IllegalAccessException | NoSuchMethodException ex) {
                                throw new EntityException(ex);
                            } catch (InvocationTargetException ex) {
                                if (ex.getCause() instanceof RuntimeException) {
                                    throw (RuntimeException) ex.getCause();
                                }
                                throw new EntityException(ex.getCause());
                            }
                            for (Object reverseCollectionElement : (Collection<?>) propertyEntry.getValue()) {
                                if (isInitialized(reverseCollectionElement)) {
                                    collRelToUpdate.put(reverseCollectionElement,
                                            (ICollectionPropertyDescriptor<?>) reverseDescriptor);
                                }
                            }
                        }
                    }
                }
            } else {
                clonedComponent.straightSetProperty(propertyEntry.getKey(),
                        ObjectUtils.cloneIfPossible(propertyEntry.getValue()));
            }
        }
    }
    for (Map.Entry<Object, ICollectionPropertyDescriptor<?>> collectionEntry : collRelToUpdate.entrySet()) {
        ICollectionPropertyDescriptor<?> collectionDescriptor = collectionEntry.getValue();
        Class<?> masterContract = null;
        if (collectionDescriptor.getReverseRelationEnd() instanceof IReferencePropertyDescriptor<?>) {
            masterContract = ((IReferencePropertyDescriptor<?>) collectionDescriptor.getReverseRelationEnd())
                    .getReferencedDescriptor().getComponentContract();
        } else if (collectionDescriptor.getReverseRelationEnd() instanceof ICollectionPropertyDescriptor<?>) {
            masterContract = ((ICollectionPropertyDescriptor<?>) collectionDescriptor.getReverseRelationEnd())
                    .getReferencedDescriptor().getElementDescriptor().getComponentContract();
        }
        ICollectionAccessor collectionAccessor = entityFactory.getAccessorFactory()
                .createCollectionPropertyAccessor(collectionDescriptor.getName(), masterContract,
                        clonedComponent.getComponentContract());
        if (collectionAccessor instanceof IModelDescriptorAware) {
            ((IModelDescriptorAware) collectionAccessor).setModelDescriptor(collectionDescriptor);
        }
        try {
            Collection<?> existingCollection = collectionAccessor.getValue(collectionEntry.getKey());
            if (!existingCollection.contains(clonedComponent)) {
                // it could already be there through lifecycle handlers / property
                // processors.
                collectionAccessor.addToValue(collectionEntry.getKey(), clonedComponent);
            }
        } catch (IllegalAccessException | NoSuchMethodException ex) {
            throw new EntityException(ex);
        } catch (InvocationTargetException ex) {
            if (ex.getCause() instanceof RuntimeException) {
                throw (RuntimeException) ex.getCause();
            }
            throw new EntityException(ex.getCause());
        }
    }
}

From source file:org.xwiki.extension.distribution.internal.job.DistributionJobStatus.java

public DistributionJobStatus(JobStatus status, ObservationManager observationManager,
        LoggerManager loggerManager) {/*from   w  ww  .ja  va  2  s .  c om*/
    super(status.getJobType(), ObjectUtils.cloneIfPossible((DistributionRequest) status.getRequest()), null,
            observationManager, loggerManager);

    if (status instanceof DistributionJobStatus) {
        DistributionJobStatus distributionJobStatus = (DistributionJobStatus) status;

        this.previousDistributionExtension = distributionJobStatus.previousDistributionExtension;
        this.previousDistributionExtensionUi = distributionJobStatus.previousDistributionExtensionUi;
        this.distributionExtension = distributionJobStatus.distributionExtension;
        this.distributionExtensionUi = distributionJobStatus.distributionExtensionUi;
        this.stepList = distributionJobStatus.stepList != null ? new ArrayList<>(distributionJobStatus.stepList)
                : new ArrayList<>();
    }
}