<commit_before>package fr.openwide.core.wicket.more.rendering;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.text.WordUtils;
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.Session;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IComponentAssignedModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Joiner.MapJoiner;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import fr.openwide.core.commons.util.functional.Functions2;
import fr.openwide.core.commons.util.functional.SerializableFunction;
import fr.openwide.core.commons.util.rendering.IRenderer;
import fr.openwide.core.wicket.more.util.IDatePattern;
import fr.openwide.core.wicket.more.util.model.Models;

/**
 * A one-way wicket converter: converts an object to a String.
 * <p>This class implements {@link IRenderer}, which is not tied to Wicket.
 */
public abstract class Renderer<T> implements IConverter<T>, IRenderer<T> {

	private static final long serialVersionUID = 4900817523619252790L;

	@Override
	public abstract String render(T value, Locale locale);

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 */
	protected static String getString(String key, Locale locale) {
		return getString(key, locale, null);
	}

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 */
	protected static String getString(String key, Locale locale, IModel<?> model) {
		return Localizer.get().getString(key, null, model, locale, null, (IModel<String>) null);
	}

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 * <p>Returns Optional.absent() when the key is not found.
	 */
	protected static Optional<String> getStringOptional(String key, Locale locale) {
		return getStringOptional(key, locale, null);
	}

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 * <p>Returns Optional.absent() when the key is not found.
	 */
	protected static Optional<String> getStringOptional(String key, Locale locale, IModel<?> model) {
		String defaultValue = new String(); // NOSONAR
		String result = Localizer.get().getString(key, null, model, locale, null, defaultValue);
		if (result == defaultValue) { // NOSONAR
			return Optional.absent();
		} else {
			return Optional.of(result);
		}
	}

	/**
	 * @deprecated Implemented to satisfy the {@link IConverter} interface; do not use this method.
	 * @throws UnsupportedOperationException Always.
	 */
	@Override
	@Deprecated
	public final T convertToObject(String value, Locale locale) throws ConversionException {
		throw new UnsupportedOperationException("Renderers do not implement convertToObject");
	}

	/**
	 * @deprecated Implemented to satisfy the {@link IConverter} interface; use {@link #render(Object, Locale)} instead.
	 */
	@Override
	@Deprecated
	public final String convertToString(T value, Locale locale) {
		return render(value, locale);
	}

	public Renderer<T> nullsAsNull() {
		return withDefault(Predicates.notNull(), NULL_RENDERER);
	}
	
	private static final Renderer<Object> NULL_RENDERER = new ConstantRenderer<Object>(null) {
		private static final long serialVersionUID = 5711969182760960576L;
		private Object readResolve() {
			return NULL_RENDERER;
		}
	};

	public Renderer<T> nullsAsBlank() {
		return withDefault(Predicates.notNull(), BLANK_RENDERER);
	}
	
	private static final Renderer<Object> BLANK_RENDERER = new ConstantRenderer<Object>("") {
		private static final long serialVersionUID = 5711969182760960576L;
		private Object readResolve() {
			return BLANK_RENDERER;
		}
	};
	
	public Renderer<T> nullsAs(T defaultValue) {
		return withDefault(Predicates.notNull(), defaultValue);
	}
	
	public Renderer<T> nullsAsConstant(String defaultRendering) {
		return withDefault(Predicates.notNull(), constant(defaultRendering));
	}
	
	public Renderer<T> withDefault(Predicate<? super T> validValuePredicate, T defaultValue) {
		return onResultOf(Functions2.defaultValue(validValuePredicate, Functions.constant(defaultValue)));
	}
	
	public Renderer<T> withDefault(Predicate<? super T> validValuePredicate, IRenderer<? super T> defaultRenderer) {
		return new DefaultingRenderer<>(validValuePredicate, this, defaultRenderer);<commit_msg>Added more Renderer.nullsAs methods (syntactic sugar)<commit_after>package fr.openwide.core.wicket.more.rendering;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.text.WordUtils;
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.Session;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IComponentAssignedModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Joiner.MapJoiner;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import fr.openwide.core.commons.util.functional.Functions2;
import fr.openwide.core.commons.util.functional.SerializableFunction;
import fr.openwide.core.commons.util.rendering.IRenderer;
import fr.openwide.core.wicket.more.util.IDatePattern;
import fr.openwide.core.wicket.more.util.model.Models;

/**
 * A one-way wicket converter: converts an object to a String.
 * <p>This class implements {@link IRenderer}, which is not tied to Wicket.
 */
public abstract class Renderer<T> implements IConverter<T>, IRenderer<T> {

	private static final long serialVersionUID = 4900817523619252790L;

	@Override
	public abstract String render(T value, Locale locale);

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 */
	protected static String getString(String key, Locale locale) {
		return getString(key, locale, null);
	}

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 */
	protected static String getString(String key, Locale locale, IModel<?> model) {
		return Localizer.get().getString(key, null, model, locale, null, (IModel<String>) null);
	}

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 * <p>Returns Optional.absent() when the key is not found.
	 */
	protected static Optional<String> getStringOptional(String key, Locale locale) {
		return getStringOptional(key, locale, null);
	}

	/**
	 * Utility method that can be used when a resource key value is needed in the render() implementation.
	 * <p>Returns Optional.absent() when the key is not found.
	 */
	protected static Optional<String> getStringOptional(String key, Locale locale, IModel<?> model) {
		String defaultValue = new String(); // NOSONAR
		String result = Localizer.get().getString(key, null, model, locale, null, defaultValue);
		if (result == defaultValue) { // NOSONAR
			return Optional.absent();
		} else {
			return Optional.of(result);
		}
	}

	/**
	 * @deprecated Implemented to satisfy the {@link IConverter} interface; do not use this method.
	 * @throws UnsupportedOperationException Always.
	 */
	@Override
	@Deprecated
	public final T convertToObject(String value, Locale locale) throws ConversionException {
		throw new UnsupportedOperationException("Renderers do not implement convertToObject");
	}

	/**
	 * @deprecated Implemented to satisfy the {@link IConverter} interface; use {@link #render(Object, Locale)} instead.
	 */
	@Override
	@Deprecated
	public final String convertToString(T value, Locale locale) {
		return render(value, locale);
	}

	public Renderer<T> nullsAsNull() {
		return withDefault(Predicates.notNull(), NULL_RENDERER);
	}
	
	private static final Renderer<Object> NULL_RENDERER = new ConstantRenderer<Object>(null) {
		private static final long serialVersionUID = 5711969182760960576L;
		private Object readResolve() {
			return NULL_RENDERER;
		}
	};

	public Renderer<T> nullsAsBlank() {
		return withDefault(Predicates.notNull(), BLANK_RENDERER);
	}
	
	private static final Renderer<Object> BLANK_RENDERER = new ConstantRenderer<Object>("") {
		private static final long serialVersionUID = 5711969182760960576L;
		private Object readResolve() {
			return BLANK_RENDERER;
		}
	};
	
	public Renderer<T> nullsAs(T defaultValue) {
		return withDefault(Predicates.notNull(), defaultValue);
	}
	
	public Renderer<T> nullsAs(IRenderer<? super T> defaultRenderer) {
		return withDefault(Predicates.notNull(), defaultRenderer);
	}
	
	public Renderer<T> nullsAsResourceKey(String resourceKey) {
		return withDefault(Predicates.notNull(), withResourceKey(resourceKey));
	}
	
	public Renderer<T> nullsAsConstant(String defaultRendering) {
		return withDefault(Predicates.notNull(), constant(defaultRendering));
	}
	
	public Renderer<T> withDefault(Predicate<? super T> validValuePredicate, T defaultValue) {
		return onResultOf(Functions2.defaultValue(validValuePredicate, Functions.constant(defaultValue)));
	}
	
	public Renderer<T> withDefault(Predicate<? super T> validValuePredicate, IRenderer<? super T> defaultRenderer) {
		return new DefaultingRenderer<>(validValuePredicate, this, defaultRenderer);
{'fraction_non_alphanumeric': 0.10445497630331753, 'fraction_numerical': 0.011374407582938388, 'mean_word_length': 5.29910447761194, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4051045', 'n_tokens_mistral': 3321, 'n_tokens_neox': 3100, 'n_words': 844}