All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.DBCollectionObjectFactory Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * 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 com.mongodb.annotations.Immutable;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// @deprecated Usage of this API is not supported in AEM as a Cloud Service.
@Immutable
@SuppressWarnings("deprecation")
@Deprecated(since = "2021-05-27")
final class DBCollectionObjectFactory implements DBObjectFactory {

    private final Map, Class> pathToClassMap;

    private final ReflectionDBObject.JavaWrapper wrapper;

    DBCollectionObjectFactory() {
        this(Collections., Class>emptyMap(), null);
    }

    private DBCollectionObjectFactory(final Map, Class> pathToClassMap, final ReflectionDBObject.JavaWrapper wrapper) {
        this.pathToClassMap = pathToClassMap;
        this.wrapper = wrapper;
    }

    @Override
    public DBObject getInstance() {
        return getInstance(Collections.emptyList());
    }

    @Override
    public DBObject getInstance(final List path) {
        Class aClass = getClassForPath(path);
        try {
            return aClass.getDeclaredConstructor().newInstance();
        } catch (InstantiationException e) {
            throw createInternalException(aClass, e);
        } catch (IllegalAccessException e) {
            throw createInternalException(aClass, e);
        } catch (NoSuchMethodException e) {
            throw createInternalException(aClass, e);
        } catch (InvocationTargetException e) {
            throw createInternalException(aClass, e.getTargetException());
        }
    }

    public DBCollectionObjectFactory update(final Class aClass) {
        return new DBCollectionObjectFactory(updatePathToClassMap(aClass, Collections.emptyList()), isReflectionDBObject(aClass) ? ReflectionDBObject.getWrapper(aClass) : wrapper);
    }

    public DBCollectionObjectFactory update(final Class aClass, final List path) {
        return new DBCollectionObjectFactory(updatePathToClassMap(aClass, path), wrapper);
    }

    private Map, Class> updatePathToClassMap(final Class aClass, final List path) {
        Map, Class> map = new HashMap, Class>(pathToClassMap);
        if (aClass != null) {
            map.put(path, aClass);
        } else {
            map.remove(path);
        }
        return map;
    }

    Class getClassForPath(final List path) {
        if (pathToClassMap.containsKey(path)) {
            return pathToClassMap.get(path);
        } else {
            Class aClass = (wrapper != null) ? wrapper.getInternalClass(path) : null;
            return aClass != null ? aClass : BasicDBObject.class;
        }
    }

    private boolean isReflectionDBObject(final Class aClass) {
        return aClass != null && ReflectionDBObject.class.isAssignableFrom(aClass);
    }

    private MongoInternalException createInternalException(final Class aClass, final Throwable e) {
        throw new MongoInternalException("Can't instantiate class " + aClass, e);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy