<commit_before>package com.qoomon.hibernate.usertype.domainvalue;

import com.qoomon.domainvalue.type.DV;
import com.qoomon.hibernate.usertype.AbstractUserType;
import com.qoomon.hibernate.usertype.util.GenericTypeUtil;
import com.qoomon.hibernate.usertype.util.HibernateTypeUtil;
import org.hibernate.type.SingleColumnType;
import org.hibernate.usertype.UserType;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class DomainValueUserType<T extends DV<V>, V> extends AbstractUserType<T, V> {

    private static final long serialVersionUID = -4693695512787385672L;

    private final SingleColumnType<V> hibernateType;
    private Constructor<T> domainValueConstructor;


    public DomainValueUserType(final Class<T> domainValueType) {
        super(domainValueType);

        Class<V> valueType = getTypeArgument(domainValueType);
        try {
            this.domainValueConstructor = domainValueType.getDeclaredConstructor(valueType);
            this.domainValueConstructor.setAccessible(true);
        } catch (Exception ex) {
            throw new RuntimeException("missing constructor " + domainValueType.getSimpleName() + "("
                    + valueType.getSimpleName() + ")", ex);
        }

        this.hibernateType = HibernateTypeUtil.getType(valueType);
        if (this.hibernateType == null) {
            throw new RuntimeException("No mapping for value type " + valueType.getName());
        }
    }

    public static <T extends DV<V>, V> List<UserType> generate(final Collection<Class<T>> domainValueTypes) {
        final List<UserType> result = new LinkedList<UserType>();

        for (final Class<T> domainValueClass : domainValueTypes) {
            final UserType userType = new DomainValueUserType<T, V>(domainValueClass);
            result.add(userType);
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    protected  Class<V> getTypeArgument(final Class<T> domainValueType) {
        return (Class<V>) GenericTypeUtil.getTypeArguments(DV.class, domainValueType).get(0);
    }

    @Override
    public SingleColumnType<V> getHibernateType() {
        return hibernateType;
    }

    @Override
    protected T nullSafeWrap(final V value) {
        if (value == null) {
            return null;
        }
        try {
            // safe operation. was check on construction.
            return this.domainValueConstructor.newInstance(value);
        } catch (Exception ex) {
            throw new RuntimeException("unexpected",ex);
        }
    }

    @Override
    protected V nullSafeUnwrap(final T domainValue) {
        if (domainValue == null) {
            return null;
        }<commit_msg>Update DomainValueUserType.java<commit_after>package com.qoomon.hibernate.usertype.domainvalue;

import com.qoomon.domainvalue.type.DV;
import com.qoomon.hibernate.usertype.AbstractUserType;
import com.qoomon.hibernate.usertype.util.GenericTypeUtil;
import com.qoomon.hibernate.usertype.util.HibernateTypeUtil;
import org.hibernate.type.SingleColumnType;
import org.hibernate.usertype.UserType;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class DomainValueUserType<T extends DV<V>, V> extends AbstractUserType<T, V> {

    private static final long serialVersionUID = -4693695512787385672L;

    private final SingleColumnType<V> hibernateType;
    private Constructor<T> domainValueConstructor;


    public DomainValueUserType(final Class<T> domainValueType) {
        super(domainValueType);

        Class<V> valueType = getTypeArgument(domainValueType);
        try {
            this.domainValueConstructor = domainValueType.getDeclaredConstructor(valueType);
            this.domainValueConstructor.setAccessible(true);
        } catch (Exception ex) {
            throw new RuntimeException("missing constructor " + domainValueType.getSimpleName() + "("
                    + valueType.getSimpleName() + ")", ex);
        }

        this.hibernateType = HibernateTypeUtil.getType(valueType);
        if (this.hibernateType == null) {
            throw new RuntimeException("No mapping for value type " + valueType.getName());
        }
    }

    @SuppressWarnings("unchecked")
    protected  Class<V> getTypeArgument(final Class<T> domainValueType) {
        return (Class<V>) GenericTypeUtil.getTypeArguments(DV.class, domainValueType).get(0);
    }

    @Override
    public SingleColumnType<V> getHibernateType() {
        return hibernateType;
    }

    @Override
    protected T nullSafeWrap(final V value) {
        if (value == null) {
            return null;
        }
        try {
            // safe operation. was check on construction.
            return this.domainValueConstructor.newInstance(value);
        } catch (Exception ex) {
            throw new RuntimeException("unexpected",ex);
        }
    }

    @Override
    protected V nullSafeUnwrap(final T domainValue) {
        if (domainValue == null) {
            return null;
        }
{'fraction_non_alphanumeric': 0.0955585464333782, 'fraction_numerical': 0.007690828686791002, 'mean_word_length': 3.135135135135135, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '5455017', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1406, 'n_words': 298}