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

coffeepot.bean.wr.mapper.ObjectMapperFactory Maven / Gradle / Ivy

Go to download

Coffeepot-bean-wr is a simple Java framework for marshalling Java beans to flat file and unmarshal flat file to Java Beans

There is a newer version: 2.0.0-rc.5
Show newest version
/*
 * Copyright 2013 - Jeandeson O. Merelis
 */
package coffeepot.bean.wr.mapper;

/*
 * #%L
 * coffeepot-bean-wr
 * %%
 * Copyright (C) 2013 Jeandeson O. Merelis
 * %%
 * 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.
 * #L%
 */
import coffeepot.bean.wr.typeHandler.TypeHandlerFactory;
import coffeepot.bean.wr.typeHandler.TypeHandlerFactoryImpl;
import coffeepot.bean.wr.types.FormatType;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Jeandeson O. Merelis
 */
public final class ObjectMapperFactory {

    private final Set noResolved = new HashSet<>();
    private final Map mappers = new HashMap<>();
    private final Map idsMap = new HashMap<>();
    private final FormatType formatType;
    private TypeHandlerFactory handlerFactory = new TypeHandlerFactoryImpl();

    public ObjectMapperFactory(FormatType formatType) {
        this.formatType = formatType;
    }

    public ObjectMapper create(Class clazz) throws UnresolvedObjectMapperException, NoSuchFieldException, Exception {
        return create(clazz, (String) null, (Callback) null);
    }

    public ObjectMapper create(Class clazz, String recordGroupId, Callback callback) throws UnresolvedObjectMapperException, NoSuchFieldException, Exception {
        ObjectMapper objectMapper = mappers.get(clazz);
        if (objectMapper != null) {
            return objectMapper;
        }

        if (callback != null) {
            RecordModel rm = callback.call(clazz);
            if (rm != null) {
                objectMapper = new ObjectMapper(clazz, recordGroupId, this, rm);
            }
        }
        if (objectMapper == null) {
            objectMapper = new ObjectMapper(clazz, recordGroupId, this);
        }

        mappers.put(clazz, objectMapper);
        noResolved.remove(clazz);
        while (!noResolved.isEmpty()) {
            Class next = noResolved.iterator().next();
            createHelper(next, recordGroupId, callback);
        }
        return objectMapper;
    }

    public ObjectMapper create(Class clazz, RecordModel record) throws UnresolvedObjectMapperException, NoSuchFieldException, Exception {
        return create(clazz, record, (String) null, (Callback) null);
    }

    public ObjectMapper create(Class clazz, RecordModel record, String recordGroupId, Callback callback) throws UnresolvedObjectMapperException, NoSuchFieldException, Exception {
        mappers.remove(clazz);

        ObjectMapper objectMapper = null;
        if (callback != null) {
            RecordModel rm = callback.call(clazz);
            if (rm != null) {
                objectMapper = new ObjectMapper(clazz, recordGroupId, this, rm);
            }
        }
        if (objectMapper == null) {
            objectMapper = new ObjectMapper(clazz, recordGroupId, this);
        }

        mappers.put(clazz, objectMapper);
        noResolved.remove(clazz);
        while (!noResolved.isEmpty()) {
            Class next = noResolved.iterator().next();
            createHelper(next, recordGroupId, callback);
        }
        return objectMapper;

    }

    private void createHelper(Class clazz, String recordGroupId, Callback callback) throws UnresolvedObjectMapperException, NoSuchFieldException, Exception {
        ObjectMapper objectMapper = mappers.get(clazz);
        if (objectMapper != null) {
            noResolved.remove(clazz);
            return;
        }
        objectMapper = null;
        if (callback != null) {
            RecordModel rm = callback.call(clazz);
            if (rm != null) {
                objectMapper = new ObjectMapper(clazz, recordGroupId, this, rm);
            }
        }
        if (objectMapper == null) {
            objectMapper = new ObjectMapper(clazz, recordGroupId, this);
        }

        mappers.put(clazz, objectMapper);
        noResolved.remove(clazz);
    }

    public Set getNoResolved() {
        return noResolved;
    }

    public Map getMappers() {
        return mappers;
    }

    public TypeHandlerFactory getHandlerFactory() {
        return handlerFactory;
    }

    public void setHandlerFactory(TypeHandlerFactory handlerFactory) {
        this.handlerFactory = handlerFactory;
    }

    public FormatType getFormatType() {
        return formatType;
    }

    public Map getIdsMap() {
        return idsMap;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy