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

org.bson.codecs.pojo.InstanceCreatorImpl 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 org.bson.codecs.pojo;

import org.bson.codecs.configuration.CodecConfigurationException;
import java.util.HashMap;
import java.util.Map;
import static java.lang.String.format;

// @deprecated Usage of this API is not supported in AEM as a Cloud Service.
@Deprecated(since = "2022-10-31")
final class InstanceCreatorImpl implements InstanceCreator {

    private final CreatorExecutable creatorExecutable;

    private final Map, Object> cachedValues;

    private final Map properties;

    private final Object[] params;

    private T newInstance;

    InstanceCreatorImpl(final CreatorExecutable creatorExecutable) {
        this.creatorExecutable = creatorExecutable;
        if (creatorExecutable.getProperties().isEmpty()) {
            this.cachedValues = null;
            this.properties = null;
            this.params = null;
            this.newInstance = creatorExecutable.getInstance();
        } else {
            this.cachedValues = new HashMap, Object>();
            this.properties = new HashMap();
            for (int i = 0; i < creatorExecutable.getProperties().size(); i++) {
                if (creatorExecutable.getIdPropertyIndex() != null && creatorExecutable.getIdPropertyIndex() == i) {
                    this.properties.put(ClassModelBuilder.ID_PROPERTY_NAME, creatorExecutable.getIdPropertyIndex());
                } else {
                    this.properties.put(creatorExecutable.getProperties().get(i).value(), i);
                }
            }
            this.params = new Object[properties.size()];
        }
    }

    @Override
    public  void set(final S value, final PropertyModel propertyModel) {
        if (newInstance != null) {
            propertyModel.getPropertyAccessor().set(newInstance, value);
        } else {
            if (!properties.isEmpty()) {
                String propertyName = propertyModel.getWriteName();
                if (!properties.containsKey(propertyName)) {
                    // Support legacy BsonProperty settings where the property name was used instead of the write name.
                    propertyName = propertyModel.getName();
                }
                Integer index = properties.get(propertyName);
                if (index != null) {
                    params[index] = value;
                }
                properties.remove(propertyName);
            }
            if (properties.isEmpty()) {
                constructInstanceAndProcessCachedValues();
            } else {
                cachedValues.put(propertyModel, value);
            }
        }
    }

    @Override
    public T getInstance() {
        if (newInstance == null) {
            try {
                for (Map.Entry entry : properties.entrySet()) {
                    params[entry.getValue()] = null;
                }
                constructInstanceAndProcessCachedValues();
            } catch (CodecConfigurationException e) {
                throw new CodecConfigurationException(format("Could not construct new instance of: %s. " + "Missing the following properties: %s", creatorExecutable.getType().getSimpleName(), properties.keySet()), e);
            }
        }
        return newInstance;
    }

    private void constructInstanceAndProcessCachedValues() {
        try {
            newInstance = creatorExecutable.getInstance(params);
        } catch (Exception e) {
            throw new CodecConfigurationException(e.getMessage(), e);
        }
        for (Map.Entry, Object> entry : cachedValues.entrySet()) {
            setPropertyValue(entry.getKey(), entry.getValue());
        }
    }

    @SuppressWarnings("unchecked")
    private  void setPropertyValue(final PropertyModel propertyModel, final Object value) {
        set((S) value, propertyModel);
    }
}