<commit_before>/*
 * Copyright (c) 2008, <NAME>. All Rights Reserved.
 *
 * 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.qi4j.runtime.value;

import java.util.Iterator;
import org.qi4j.api.common.ConstructionException;
import org.qi4j.api.common.QualifiedName;
import org.qi4j.api.property.Property;
import org.qi4j.api.property.StateHolder;
import org.qi4j.api.value.ValueBuilder;
import org.qi4j.api.value.ValueComposite;
import org.qi4j.runtime.composite.AbstractStateModel;
import org.qi4j.runtime.structure.ModuleInstance;

/**
 * Implementation of ValueBuilder
 */
public final class ValueBuilderInstance<T>
    implements ValueBuilder<T>
{
    private final ModuleInstance moduleInstance;
    private final ValueModel valueModel;

    // lazy initialized in accessor
    private ValueInstance prototypeInstance;

    // lazy initialized in accessor
    private StateHolder state;

    public ValueBuilderInstance( ModuleInstance moduleInstance, ValueModel valueModel )
    {
        this.moduleInstance = moduleInstance;

        this.valueModel = valueModel;
    }

    public ValueBuilder<T> withPrototype( T value )
    {
        ValueInstance valueInstance = ValueInstance.getValueInstance( (ValueComposite) value );
        StateHolder state = valueInstance.state();
        this.state = valueModel.newBuilderState( state );
        prototypeInstance = null;

        return this;
    }

    public ValueBuilder<T> withState( StateHolder state )
    {
        final StateHolder valueState = getState();
        state.visitProperties( new StateHolder.StateVisitor<RuntimeException>()
        {
            public void visitProperty( QualifiedName name, Object value )
            {
                Property<Object> property = valueState.getProperty( name );
                if( property != null )
                {
                    property.set( value );
                }
            }
        } );
        return this;
    }

    public T prototype()
    {
        // Instantiate given value type
        if( prototypeInstance == null )
        {
            prototypeInstance = valueModel.newValueInstance( moduleInstance, getState() );
        }

        return prototypeInstance.<T>proxy();
    }

    public <K> K prototypeFor( Class<K> mixinType )
    {
        // Instantiate given value type
        if( prototypeInstance == null )
        {
            prototypeInstance = valueModel.newValueInstance( moduleInstance, getState() );
        }

        return prototypeInstance.newProxy( mixinType );
    }

    public T newInstance()
        throws ConstructionException
    {
        StateHolder instanceState;
        if( state == null )
        {
            instanceState = valueModel.newInitialState();
        }
        else
        {
            instanceState = valueModel.newState( state );
        }

        valueModel.checkConstraints( instanceState );
        ValueInstance valueInstance = valueModel.newValueInstance( moduleInstance, instanceState );
        return valueInstance.<T>proxy();
    }

    public Iterator<T> iterator()
    {
        return new Iterator<T>()
        {
            public boolean hasNext()
            {
                return true;
            }

            public T next()
            {
                return newInstance();
            }

            public void remove()
            {
                throw new UnsupportedOperationException();
            }
        };
    }

    private StateHolder getState()
    {
        if( state == null )
        {
            state = valueModel.newBuilderState();
        }

        return state;
    }
}<commit_msg>Solving 'ignore' for setting properies in ValueBuilder that does not exist. Missed one file.<commit_after>/*
 * Copyright (c) 2008, <NAME>. All Rights Reserved.
 *
 * 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.qi4j.runtime.value;

import java.util.Iterator;
import org.qi4j.api.common.ConstructionException;
import org.qi4j.api.common.QualifiedName;
import org.qi4j.api.property.Property;
import org.qi4j.api.property.StateHolder;
import org.qi4j.api.value.ValueBuilder;
import org.qi4j.api.value.ValueComposite;
import org.qi4j.runtime.structure.ModuleInstance;

/**
 * Implementation of ValueBuilder
 */
public final class ValueBuilderInstance<T>
    implements ValueBuilder<T>
{
    private final ModuleInstance moduleInstance;
    private final ValueModel valueModel;

    // lazy initialized in accessor
    private ValueInstance prototypeInstance;

    // lazy initialized in accessor
    private StateHolder state;

    public ValueBuilderInstance( ModuleInstance moduleInstance, ValueModel valueModel )
    {
        this.moduleInstance = moduleInstance;

        this.valueModel = valueModel;
    }

    public ValueBuilder<T> withPrototype( T value )
    {
        ValueInstance valueInstance = ValueInstance.getValueInstance( (ValueComposite) value );
        StateHolder state = valueInstance.state();
        this.state = valueModel.newBuilderState( state );
        prototypeInstance = null;

        return this;
    }

    public ValueBuilder<T> withState( StateHolder state )
    {
        final StateHolder valueState = getState();
        state.visitProperties( new StateHolder.StateVisitor<RuntimeException>()
        {
            public void visitProperty( QualifiedName name, Object value )
            {
                Property<Object> property = valueState.getProperty( name );
                if( property != null )
                {
                    property.set( value );
                }
            }
        } );
        return this;
    }

    public T prototype()
    {
        // Instantiate given value type
        if( prototypeInstance == null )
        {
            prototypeInstance = valueModel.newValueInstance( moduleInstance, getState() );
        }

        return prototypeInstance.<T>proxy();
    }

    public <K> K prototypeFor( Class<K> mixinType )
    {
        // Instantiate given value type
        if( prototypeInstance == null )
        {
            prototypeInstance = valueModel.newValueInstance( moduleInstance, getState() );
        }

        return prototypeInstance.newProxy( mixinType );
    }

    public T newInstance()
        throws ConstructionException
    {
        StateHolder instanceState;
        if( state == null )
        {
            instanceState = valueModel.newInitialState();
        }
        else
        {
            instanceState = valueModel.newState( state );
        }

        valueModel.checkConstraints( instanceState );
        ValueInstance valueInstance = valueModel.newValueInstance( moduleInstance, instanceState );
        return valueInstance.<T>proxy();
    }

    public Iterator<T> iterator()
    {
        return new Iterator<T>()
        {
            public boolean hasNext()
            {
                return true;
            }

            public T next()
            {
                return newInstance();
            }

            public void remove()
            {
                throw new UnsupportedOperationException();
            }
        };
    }

    private StateHolder getState()
    {
        if( state == null )
        {
            state = valueModel.newBuilderState();
        }

        return state;
    }
}
{'fraction_non_alphanumeric': 0.08149753220175755, 'fraction_numerical': 0.0039725532683279165, 'mean_word_length': 2.2126836813611757, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '259610', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2024, 'n_words': 591}