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

com.yixan.tools.common.sync.Args Maven / Gradle / Ivy

There is a newer version: 3.7.1
Show newest version
package com.yixan.tools.common.sync;

import java.util.Arrays;

/**
 * 调用方法的参数
 *
 * @author zhh
 * @version V1.0 2017年5月26日
 */
public interface Args {

    int length();

    Arg[] get();

    Class[] types();

    Object[] values();

    public static class Arg {

        private Class type;
        private T value;

        public Arg(Class type) {
            this.type = type;
        }

        public  Arg(Class type, V value) {
            this.type = type;
            this.value = value;
        }

        public Class getType() {
            return type;
        }

        public T getValue() {
            return value;
        }

    }

    public static class AnyArgs implements Args {

        private final Arg[] args;

        public AnyArgs(Object... objects) {
            int length = objects == null ? 0 : objects.length;
            this.args = new Arg[length];
            for (int i = 0; i < length; i++) {
                Object o = objects[i];
                if (o == null) {
                    throw new IllegalArgumentException("null value[" + i + "] must use new Arg(xxx.class, value)");
                } else if (o instanceof Arg) {
                    Arg arg = (Arg) o;
                    this.args[i] = arg;
                } else {
                    @SuppressWarnings("unchecked")
                    Class type = (Class) o.getClass();
                    this.args[i] = new Arg(type, o);
                }
            }
        }

        @Override
        public int length() {
            return args == null ? 0 : args.length;
        }

        @Override
        public Arg[] get() {
            return args;
        }

        @Override
        public Class[] types() {
            int length = length();
            Class[] types = new Class[length];
            for (int i = 0; i < length; i++) {
                types[i] = args[i].getType();
            }
            return types;
        }

        @Override
        public Object[] values() {
            int length = length();
            Object[] types = new Object[length];
            for (int i = 0; i < length; i++) {
                types[i] = args[i].getValue();
            }
            return types;
        }

        public String toString() {
            return Arrays.toString(this.values());
        }
    }
}