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

com.github.t3t5u.common.database.AbstractConverter Maven / Gradle / Ivy

The newest version!
package com.github.t3t5u.common.database;

import java.util.List;

import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public abstract class AbstractConverter implements Converter {
	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractConverter.class);

	@Override
	public final T toModel(final E entity) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toModel: " + entity);
		}
		if (!isAcceptable(entity)) {
			return null;
		}
		final T result = toModelAccepted(entity);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toModel: " + result);
		}
		return result;
	}

	protected boolean isAcceptable(final E entity) {
		return entity != null;
	}

	protected abstract T toModelAccepted(E entity);

	@Override
	@Deprecated
	public final T toModel(final E entity, final T model) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toModel: " + entity + ", " + model);
		}
		if (!isAcceptable(entity, model)) {
			return null;
		}
		final T result = toModelAccepted(entity, model);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toModel: " + result);
		}
		return result;
	}

	@Deprecated
	protected final boolean isAcceptable(final E entity, final T model) {
		return (entity != null) && (model != null);
	}

	@Deprecated
	protected final T toModelAccepted(final E entity, final T model) {
		return model;
	}

	@Override
	public final List toModels(final List entities) {
		if (entities == null) {
			return null;
		}
		return Lists.transform(entities, new Function() {
			@Override
			@Nullable
			public T apply(@Nullable final E input) {
				return toModel(input);
			}
		});
	}

	@Override
	public final E toEntity(final T model) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toEntity: " + model);
		}
		if (!isAcceptable(model)) {
			return null;
		}
		final E result = toEntityAccepted(model);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toEntity: " + result);
		}
		return result;
	}

	protected boolean isAcceptable(final T model) {
		return model != null;
	}

	protected abstract E toEntityAccepted(T model);

	@Override
	public final E toEntity(final T model, final E entity) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toEntity: " + model + ", " + entity);
		}
		if (!isAcceptable(model, entity)) {
			return null;
		}
		final E result = toEntityAccepted(model, entity);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("toEntity: " + result);
		}
		return result;
	}

	protected boolean isAcceptable(final T model, final E entity) {
		return (model != null) && (entity != null);
	}

	protected E toEntityAccepted(final T model, final E entity) {
		return entity;
	}

	@Override
	public final List toEntities(final List models) {
		if (models == null) {
			return null;
		}
		return Lists.transform(models, new Function() {
			@Override
			@Nullable
			public E apply(@Nullable final T input) {
				return toEntity(input);
			}
		});
	}

	protected static  E toEntityForced(final Converter converter, final T model, final E entity) {
		final E result = converter.toEntity(model, entity);
		return result != null ? result : converter.toEntity(model);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy