<commit_before>/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
 *
 * 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 com.mongodb;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO This needs to be rethinked.
 */
public class TypeMapping {
    private static final List<String> EMPTY_PATH = Collections.emptyList();

    private final Map<List<String>, Class<? extends DBObject>> pathToClassMap;
    private ReflectionDBObject.JavaWrapper wrapper;

    public TypeMapping(final Class<? extends DBObject> topLevelClass) {
        this(topLevelClass, new HashMap<String, Class<? extends DBObject>>());
    }

    public TypeMapping(final Class<? extends DBObject> topLevelClass,
                       final Map<String, Class<? extends DBObject>> stringPathToClassMap) {
        this.pathToClassMap = createPathToClassMap(topLevelClass, stringPathToClassMap);

        updateWrapper(topLevelClass);
    }

    private void updateWrapper(final Class<? extends DBObject> topLevelClass) {
        if (ReflectionDBObject.class.isAssignableFrom(topLevelClass)) {
            wrapper = ReflectionDBObject.getWrapper(topLevelClass);
        } else {
            wrapper = null;
        }
    }

    public Class<? extends DBObject> getTopLevelClass() {
        return pathToClassMap.get(EMPTY_PATH);
    }

    public synchronized void setTopLevelClass(final Class<? extends DBObject> cls) {
        setInternalClass(EMPTY_PATH, cls);
        updateWrapper(cls);
    }

    public synchronized void setInternalClass(final List<String> path, final Class<? extends DBObject> cls) {
        pathToClassMap.put(path, cls);
    }

    public Class<? extends DBObject> getInternalClass(final List<String> path) {
        return pathToClassMap.get(path);
    }

    public Map<List<String>, Class<? extends DBObject>> getPathToClassMap() {
        return pathToClassMap;
    }

    public DBObject getNewInstance(final List<String> path) {
        final Class<? extends DBObject> cls = getClassForNewObject(path);
        try {
            return cls.newInstance();
        } catch (ReflectiveOperationException e) {
            throw new MongoInternalException("Can't create a new instance of class " + cls, e);
        }
    }<commit_msg>Fixed ClassNotFoundException for Java 6<commit_after>/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
 *
 * 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 com.mongodb;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO This needs to be rethinked.
 */
public class TypeMapping {
    private static final List<String> EMPTY_PATH = Collections.emptyList();

    private final Map<List<String>, Class<? extends DBObject>> pathToClassMap;
    private ReflectionDBObject.JavaWrapper wrapper;

    public TypeMapping(final Class<? extends DBObject> topLevelClass) {
        this(topLevelClass, new HashMap<String, Class<? extends DBObject>>());
    }

    public TypeMapping(final Class<? extends DBObject> topLevelClass,
                       final Map<String, Class<? extends DBObject>> stringPathToClassMap) {
        this.pathToClassMap = createPathToClassMap(topLevelClass, stringPathToClassMap);

        updateWrapper(topLevelClass);
    }

    private void updateWrapper(final Class<? extends DBObject> topLevelClass) {
        if (ReflectionDBObject.class.isAssignableFrom(topLevelClass)) {
            wrapper = ReflectionDBObject.getWrapper(topLevelClass);
        } else {
            wrapper = null;
        }
    }

    public Class<? extends DBObject> getTopLevelClass() {
        return pathToClassMap.get(EMPTY_PATH);
    }

    public synchronized void setTopLevelClass(final Class<? extends DBObject> cls) {
        setInternalClass(EMPTY_PATH, cls);
        updateWrapper(cls);
    }

    public synchronized void setInternalClass(final List<String> path, final Class<? extends DBObject> cls) {
        pathToClassMap.put(path, cls);
    }

    public Class<? extends DBObject> getInternalClass(final List<String> path) {
        return pathToClassMap.get(path);
    }

    public Map<List<String>, Class<? extends DBObject>> getPathToClassMap() {
        return pathToClassMap;
    }

    public DBObject getNewInstance(final List<String> path) {
        final Class<? extends DBObject> cls = getClassForNewObject(path);
        try {
            return cls.newInstance();
        } catch (InstantiationException e) {
            throw new MongoInternalException("can't create a new instance of class " + cls, e);
        } catch (IllegalAccessException e) {
            throw new MongoInternalException("can't create a new instance of class " + cls, e);
        }
    }
{'fraction_non_alphanumeric': 0.09132888660851719, 'fraction_numerical': 0.005643919958953309, 'mean_word_length': 3.3544303797468356, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '3220919', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1559, 'n_words': 500}