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

org.appng.api.support.OptionImpl Maven / Gradle / Ivy

There is a newer version: 1.24.5
Show newest version
/*
 * Copyright 2011-2021 the original author or authors.
 *
 * 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.appng.api.support;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.Option;

/**
 * The default implementation for {@link Option}
 * 
 * @author Matthias Herlitzius
 * @author Matthias Müller
 */
public class OptionImpl implements Option {

	private static final Pattern INT_PATTERN = Pattern.compile("[+-]?[\\d]+");
	private final String name;
	private Map attributeMap = new HashMap<>();

	public OptionImpl(String name) {
		this.name = name;
	}

	public OptionImpl(String name, Map map) {
		this.name = name;
		addMap(map);
	}

	public String getName() {
		return name;
	}

	public OptionImpl addMap(Map map) {
		attributeMap.putAll(map);
		return this;
	}

	public OptionImpl addAttribute(String name, String value) {
		attributeMap.put(name, value);
		return this;
	}

	public boolean containsAttribute(String name) {
		return attributeMap.containsKey(name);
	}

	public String getAttribute(String name) {
		return getString(name);
	}

	public int getAttributeAsInteger(String name) {
		return Integer.valueOf(attributeMap.get(name));
	}

	public Set getAttributeNames() {
		return attributeMap.keySet();
	}

	public Map getAttributeMap() {
		return attributeMap;
	}

	public String getString(String name) {
		return attributeMap.get(name);
	}

	public Integer getInteger(String name) {
		String value = getString(name);
		return null == value ? null : INT_PATTERN.matcher(value).matches() ? Integer.valueOf(value) : null;
	}

	public Boolean getBoolean(String name) {
		return Boolean.valueOf(getString(name));
	}

	public > E getEnum(String name, Class type) {
		String value = StringUtils.upperCase(getString(name));
		return EnumUtils.isValidEnum(type, value) ? Enum.valueOf(type, value) : null;
	}

	public String toString() {
		StringBuffer result = new StringBuffer(getName() + " [ ");
		getAttributeNames().forEach(key -> result.append(key + "=\"" + getString(key) + "\" "));
		result.append("]");
		return result.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy