<commit_before>// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
//
// 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.apache.tapestry5.internal.transform;

import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.internal.InternalComponentResources;
import org.apache.tapestry5.internal.services.ComponentClassCache;
import org.apache.tapestry5.ioc.services.FieldValueConduit;
import org.apache.tapestry5.ioc.services.PerthreadManager;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.runtime.PageLifecycleAdapter;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.ComponentClassTransformWorker;
import org.apache.tapestry5.services.ComponentValueProvider;
import org.apache.tapestry5.services.TransformField;

/**
 * Converts fields with the {@link org.apache.tapestry5.annotations.Persist} annotation into persistent fields.
 */
public class PersistWorker implements ComponentClassTransformWorker
{
    class PersistentFieldConduit implements FieldValueConduit
    {
        private final InternalComponentResources resources;

        private final String name;

        private final Object defaultValue;

        private final String key;

        public PersistentFieldConduit(InternalComponentResources resources, String name, Object defaultValue)
        {
            this.resources = resources;
            this.name = name;
            this.defaultValue = defaultValue;

            this.key = String.format("PersistWorker:%s/%s", resources.getCompleteId(), name);

            resources.addPageLifecycleListener(new PageLifecycleAdapter()
            {
                @Override
                public void restoreStateBeforePageAttach()
                {
                    restoreStateAtPageAttach();
                }
            });
        }

        private PerThreadState getState()
        {
            PerThreadState state = (PerThreadState) perThreadManager.get(key);

            if (state == null)
            {
                state = new PerThreadState(defaultValue);
                perThreadManager.put(key, state);
            }

            return state;
        }

        public Object get()
        {
            return getState().value;
        }

        public void set(Object newValue)
        {
            resources.persistFieldChange(name, newValue);

            getState().value = newValue;
        }

        private void restoreStateAtPageAttach()
        {
            if (resources.hasFieldChange(name))
                getState().value = resources.getFieldChange(name);
        }
    }

    private final ComponentClassCache classCache;

    private final PerthreadManager perThreadManager;

    public PersistWorker(ComponentClassCache classCache, PerthreadManager perThreadManager)
    {
        this.classCache = classCache;
        this.perThreadManager = perThreadManager;
    }

    public void transform(ClassTransformation transformation, MutableComponentModel model)
    {
        List<TransformField> fieldsWithAnnotation = transformation.matchFieldsWithAnnotation(Persist.class);

        for (TransformField field : fieldsWithAnnotation)
        {
            makeFieldPersistent(field, model);
        }
    }

    private void makeFieldPersistent(TransformField field, MutableComponentModel model)
    {
        Persist annotation = field.getAnnotation(Persist.class);

        field.claim(annotation);

        final String logicalFieldName = model.setFieldPersistenceStrategy(field.getName(), annotation.value());

        final Object defaultValue = determineDefaultValueFromFieldType(field);

        ComponentValueProvider<FieldValueConduit> provider = new ComponentValueProvider<FieldValueConduit>()
        {
            public FieldValueConduit get(ComponentResources resources)
            {
                return new PersistentFieldConduit((InternalComponentResources) resources, logicalFieldName,
                        defaultValue);
            }
        };

        field.replaceAccess(provider);
    }

    private Object determineDefaultValueFromFieldType(TransformField field)
    {
        return classCache.defaultValueForType(field.getType());
    }
}
<commit_msg>TAP5-1197: Make use of PerThreadValue inside PersistWorker<commit_after>// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
//
// 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.apache.tapestry5.internal.transform;

import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.internal.InternalComponentResources;
import org.apache.tapestry5.internal.services.ComponentClassCache;
import org.apache.tapestry5.ioc.services.FieldValueConduit;
import org.apache.tapestry5.ioc.services.PerThreadValue;
import org.apache.tapestry5.ioc.services.PerthreadManager;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.runtime.PageLifecycleAdapter;
import org.apache.tapestry5.services.ClassTransformation;
import org.apache.tapestry5.services.ComponentClassTransformWorker;
import org.apache.tapestry5.services.ComponentValueProvider;
import org.apache.tapestry5.services.TransformField;

/**
 * Converts fields with the {@link org.apache.tapestry5.annotations.Persist} annotation into persistent fields.
 */
public class PersistWorker implements ComponentClassTransformWorker
{
    class PersistentFieldConduit implements FieldValueConduit
    {
        private final InternalComponentResources resources;

        private final String name;

        private final PerThreadValue<Object> fieldValue;

        private final Object defaultValue;

        public PersistentFieldConduit(InternalComponentResources resources, String name,
                PerThreadValue<Object> fieldValue, Object defaultValue)
        {
            this.resources = resources;
            this.name = name;
            this.fieldValue = fieldValue;
            this.defaultValue = defaultValue;

            resources.addPageLifecycleListener(new PageLifecycleAdapter()
            {
                @Override
                public void restoreStateBeforePageAttach()
                {
                    restoreStateAtPageAttach();
                }
            });
        }

        public Object get()
        {
            if (!fieldValue.exists())
                return defaultValue;

            return fieldValue.get();
        }

        public void set(Object newValue)
        {
            resources.persistFieldChange(name, newValue);

            fieldValue.set(newValue);
        }

        private void restoreStateAtPageAttach()
        {
            if (resources.hasFieldChange(name))
                fieldValue.set(resources.getFieldChange(name));
        }
    }

    private final ComponentClassCache classCache;

    private final PerthreadManager perThreadManager;

    public PersistWorker(ComponentClassCache classCache, PerthreadManager perThreadManager)
    {
        this.classCache = classCache;
        this.perThreadManager = perThreadManager;
    }

    public void transform(ClassTransformation transformation, MutableComponentModel model)
    {
        List<TransformField> fieldsWithAnnotation = transformation.matchFieldsWithAnnotation(Persist.class);

        for (TransformField field : fieldsWithAnnotation)
        {
            makeFieldPersistent(field, model);
        }
    }

    private void makeFieldPersistent(TransformField field, MutableComponentModel model)
    {
        Persist annotation = field.getAnnotation(Persist.class);

        field.claim(annotation);

        final String logicalFieldName = model.setFieldPersistenceStrategy(field.getName(), annotation.value());

        final Object defaultValue = determineDefaultValueFromFieldType(field);

        ComponentValueProvider<FieldValueConduit> provider = new ComponentValueProvider<FieldValueConduit>()
        {
            public FieldValueConduit get(ComponentResources resources)
            {
                String key = String.format("PersistWorker:%s/%s", resources.getCompleteId(), logicalFieldName);

                return new PersistentFieldConduit((InternalComponentResources) resources, logicalFieldName,
                        perThreadManager.createValue(key), defaultValue);
            }
        };

        field.replaceAccess(provider);
    }

    private Object determineDefaultValueFromFieldType(TransformField field)
    {
        return classCache.defaultValueForType(field.getType());
    }
}

{'fraction_non_alphanumeric': 0.07477885208804773, 'fraction_numerical': 0.008434478502365768, 'mean_word_length': 2.9396272285251217, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7154856', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2303, 'n_words': 569}