<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.composite;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * JAVADOC
 */
public final class UsesInstance
{
    public static final UsesInstance NO_USES = new UsesInstance();

    private final Set<Object> uses = new HashSet<Object>();

    public void use( Object... objects )
    {
        uses.addAll( Arrays.asList( objects ) );
    }

    public Object useForType( Class<?> type )
    {
        for( Object use : uses )
        {
            if( use instanceof CompositeBuilderInstance )
            {
                CompositeBuilderInstance builder = (CompositeBuilderInstance) use;
                if( type.isAssignableFrom( builder.compositeType() ) )
                {
                    return builder.newInstance();
                }
            }

            if( type.isInstance( use ) )
            {
                return use;
            }
        }

        return null;
    }
}<commit_msg>Fixed QI-199 (@Uses does instance reuse now, to allow for easy graph creation)<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.composite;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.qi4j.runtime.object.ObjectBuilderInstance;

/**
 * JAVADOC
 */
public final class UsesInstance
{
    public static final UsesInstance NO_USES = new UsesInstance();

    private final Set<Object> uses = new HashSet<Object>();

    public void use( Object... objects )
    {
        uses.addAll( Arrays.asList( objects ) );
    }

    public Object useForType( Class<?> type )
    {
        for( Object use : uses )
        {
            if( use instanceof CompositeBuilderInstance )
            {
                CompositeBuilderInstance builder = (CompositeBuilderInstance) use;
                if( type.isAssignableFrom( builder.compositeType() ) )
                {
                    return builder.newInstance();
                }
            } else if( use instanceof ObjectBuilderInstance )
            {
                ObjectBuilderInstance builder = (ObjectBuilderInstance) use;
                if( type.isAssignableFrom( builder.objectType() ) )
                {
                    return builder.newInstance();
                }
            }

            if( type.isInstance( use ) )
            {
                return use;
            }
        }

        return null;
    }
}
{'fraction_non_alphanumeric': 0.08601553829078801, 'fraction_numerical': 0.006104328523862375, 'mean_word_length': 2.165057067603161, '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': '1890381', 'n_tokens_mistral': 954, 'n_tokens_neox': 912, 'n_words': 326}