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

io.amient.affinity.core.config.CfgGroup Maven / Gradle / Ivy

/*
 * Copyright 2016-2018 Michal Harish, [email protected]
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 io.amient.affinity.core.config;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

public class CfgGroup> extends Cfg> implements CfgNested {

    private final Class cls;

    public CfgGroup(Class cls) {
        this.cls = cls;
    }

    public Class getGroupClass() {
        return cls;
    }

    @Override
    public CfgGroup doc(String description) {
        super.doc(description);
        return this;
    }

    @Override
    public CfgGroup apply(Config config) throws IllegalArgumentException {
        ConfigObject o = listPos > -1 ? config.getObjectList(relPath).get(listPos) : config.getObject(relPath);
        Map map = new HashMap<>();
        o.keySet().forEach((key) -> {
            try {
                G item = cls.newInstance();
                item.setRelPath(key);
                item.setPath(path(key));
                item.apply(config.getConfig(relPath));
                map.put(key, item);
            } catch (InstantiationException e) {
                throw new IllegalArgumentException(e);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException(e);
            }

        });
       setValue(map);
       return this;
    }

    @Override
    public String parameterInfo() {
        return "";
    }

    public G apply(String entry, G value) {
        value.setRelPath(entry);
        value.setPath(path(entry));
        return value;
    }

    public G apply(String entry) {
        Map group;
        try {
            group = apply();
        } catch (NoSuchElementException e) {
            setValue(new HashMap<>());
            group = apply();
        }
        if (group.containsKey(entry)) {
            return group.get(entry);
        } else {
            try {
                G template = cls.newInstance();
                template.setRelPath(entry);
                template.setPath(path(entry));
                group.put(entry, template);
                return template;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy