Copyable.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * 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 org.genesys.blocks.model;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.genesys.blocks.annotations.NotCopyable;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
//import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldFilter;

import javax.persistence.Id;

/**
 * The Interface Copyable.
 *
 * @param <T> the generic type
 */
public interface Copyable<T> {

	/**
	 * Make a deep copy of the object, with all new instances. Implementations
	 * should use {@link #apply(Object)} to apply this values to the new instance.
	 * Default implementation returns null.
	 * <pre>
	 * &#64;Override
	 * public Budget copy() {
	 * 	final Budget copy = new Budget();
	 * 	copy.apply(this);
	 * 	return copy;
	 * }
	 * </pre>
	 *
	 * @return a deep copy
	 */
	default T copy() {
		return null;
	}

	/**
	 * Apply values from source to this object. It should not handle lists, sets and
	 * other complicated things.
	 *
	 * The default implementation is based on
	 * {@link ReflectionUtils#shallowCopyFieldState(Object, Object)} but it only
	 * uses accessible fields and ignores Collections.
	 *
	 * @param source the source
	 * @return the t
	 */
	default T apply(final T source) {
		if (source == null) {
			throw new IllegalArgumentException("Source for field copy cannot be null");
		}
		if (!source.getClass().equals(this.getClass())) {
			throw new IllegalArgumentException("Source class [" + source.getClass().getName() + "] must be same as target class [" + this.getClass().getName() + "]");
		}

		@SuppressWarnings("unchecked")
		final T dest = (T) this;

		ReflectionUtils.doWithFields(source.getClass(), field -> {
			if (field.getDeclaringClass().equals(dest.getClass()) || Modifier.isProtected(field.getModifiers()) || Modifier.isPublic(field.getModifiers()) || (field
				.getModifiers() == 0)) {
				ReflectionUtils.makeAccessible(field);
				// System.err.println(field + " is made accessible, modifiers = " +
				// field.getModifiers());
				final Object srcValue = field.get(source);
				field.set(dest, srcValue);
			} else {
				// System.err.println(field + " not accessible, modifiers = " +
				// field.getModifiers());
			}
		}, COPYABLE_FIELDS);

		return dest;
	}

	/**
	 * Pre-built FieldFilter that matches all non-static, non-final, non-Collection
	 * fields.
	 */
	public static final FieldFilter COPYABLE_FIELDS = field -> !(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())
	// or collection
			|| Collection.class.isAssignableFrom(field.getType())
			// or map
			|| Map.class.isAssignableFrom(field.getType())
			// or array
			|| field.getClass().isArray()
			// or has @Id
			|| field.isAnnotationPresent(Id.class)
			// or has @CreatedBy
			|| field.isAnnotationPresent(CreatedBy.class)
			// or has @LastModifiedBy
			|| field.isAnnotationPresent(LastModifiedBy.class)
			// or has @CreatedDate
			|| field.isAnnotationPresent(CreatedDate.class)
//			 or has @LastModifiedDate
//			|| field.isAnnotationPresent(LastModifiedDate.class)
			// or has @NotCopyable
			|| field.isAnnotationPresent(NotCopyable.class));

	/**
	 * Utility method that makes a deep copy of {@link Copyable} elements.
	 *
	 * @param <R> the generic type
	 * @param source the source
	 * @param action the action
	 * @return the list
	 */
	default <R extends Copyable<R>> List<R> copyList(final List<R> source, final Consumer<? super R> action) {
		if ((source == null) || (source.isEmpty())) {
			return new ArrayList<>();
		}
		return source.stream().map(item -> item.copy()).peek(action).collect(Collectors.toList());
	}
}