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

com.cybermkd.route.core.Params Maven / Gradle / Ivy

There is a newer version: 1.0.1.3
Show newest version
package com.cybermkd.route.core;

import com.cybermkd.common.entity.CaseInsensitiveMap;
import com.cybermkd.common.entity.Entity;
import com.cybermkd.common.util.json.Jsoner;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * Created by ice on 15-1-26.
 */
public class Params {
    /**
     * params for validate
     */
    private Map params;

    public Params() {
        this.params = new CaseInsensitiveMap();
    }

    public Params(Entity entity) {
        this.params = entity.getAttrs();
    }

    public Params(CaseInsensitiveMap params) {
        this.params = params;
    }

    /**
     * Put key value pair to the params.
     */
    public Params set(String key, Object value) {
        params.put(key, value);
        return this;
    }

    public  T getObject(Class clazz) {
        return Jsoner.toObject(Jsoner.toJSON(params), clazz);
    }

    /**
     * Get param of any type.
     */
    public  T get(String name) {
        return (T) (params.get(name));
    }

    /**
     * Parse param to any type
     */
    public  T get(String attr, Class clazz) {
        Object value = params.get(attr);
        if (clazz.isAssignableFrom(value.getClass())) {
            return (T) value;
        } else {
            return Jsoner.toObject(Jsoner.toJSON(value), clazz);
        }
    }

    /**
     * Get param of any type. Returns defaultValue if null.
     */
    public  T get(String attr, Object defaultValue) {
        Object result = get(attr);
        return (T) (result != null ? result : defaultValue);
    }

    /**
     * Get param for clazz. Returns defaultValue if null.
     */
    public  T get(String attr, Class clazz, Object defaultValue) {
        Object result = get(attr, clazz);
        return (T) (result != null ? result : defaultValue);
    }

    /**
     * Return param name of this route.
     */
    public String[] getNames() {
        Set nameSet = params.keySet();
        return nameSet.toArray(new String[nameSet.size()]);
    }

    /**
     * Return param values of this route.
     */
    public Object[] getValues() {
        Collection valueCollection = params.values();
        return valueCollection.toArray(new Object[valueCollection.size()]);
    }

    /**
     * 判断是否存在某个参数
     *
     * @param name
     * @return
     */
    public boolean containsName(String name) {
        return params.containsKey(name);
    }

    /**
     * 判断是否存在某个值
     *
     * @param value
     * @return
     */
    public boolean containsValue(Object value) {
        return params.containsValue(value);
    }
}