/*-
 * ~~~~~~licensing~~~~~~
 * management-core
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.platform.base.management.core;

import java.util.LinkedHashMap;
import java.util.Map;

import ru.entaxy.platform.base.support.CommonUtils;

public class Qualifier {

	protected String domain = ManagementCore.ENTAXY_JMX_DOMAIN;
	
	protected Qualifier parent;

	protected Map<String, String> attributes = new LinkedHashMap<>();
	
	public Qualifier() {
		super();
	}

	public Qualifier(String key, String value) {
		super();
		attributes.put(key, value);
	}
	
	public Qualifier(Qualifier parent) {
		this();
		this.parent = parent;
	}
	
	public Qualifier qualifier(String key, String value) {
		return (new Qualifier(this)).attribute(key, value);
	}

	public Qualifier attribute(String key, String value) {
		this.attributes.put(key, value);
		return this;
	}
	
	public String getDomain() {
		if (this.parent == null)
			return CommonUtils.getValid(this.domain, ManagementCore.ENTAXY_JMX_DOMAIN);
		else
			return parent.getDomain();
	}
	
	public String getAttributes() {
		String value = this.parent==null?"":this.parent.getAttributes();
		for (Map.Entry<String, String> entry: this.attributes.entrySet()) {
			if (!value.isEmpty()) {
				value += ",";
			}
			value += entry.getKey() + "=" + entry.getValue();
		}
		return value;
	}
	
	public String getValue() {
		String value = getAttributes();
		if (value.isEmpty())
			return getDomain();
		else
			return getDomain() + ":" + value;
	}
	
	public String getFullString() {
		return ManagementCore.JMX_OBJECTNAME + "=" + getValue();
	}
}
