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

com.github.chengyuxing.sql.Args Maven / Gradle / Ivy

Go to download

Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql file.

There is a newer version: 9.0.2
Show newest version
package com.github.chengyuxing.sql;

import com.github.chengyuxing.common.MapExtends;
import com.github.chengyuxing.common.utils.ObjectUtil;
import com.github.chengyuxing.sql.utils.EntityUtil;
import org.jetbrains.annotations.NotNull;

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

/**
 * Simple sql args tool.
 *
 * @param  value type
 */
public final class Args extends HashMap implements MapExtends, V> {
    /**
     * Constructs a new empty Args.
     */
    public Args() {
    }

    public Args(int initialCapacity) {
        super(initialCapacity);
    }

    /**
     * Returns an empty Args.
     *
     * @param  value type
     * @return empty Args instance
     */
    public static  Args of() {
        return new Args<>();
    }

    /**
     * Returns an Args with generic initial value.
     *
     * @param k   key
     * @param v   value
     * @param  value type
     * @return generic Args instance
     */
    public static  Args of(String k, V v) {
        Args args = of();
        args.put(k, v);
        return args;
    }

    /**
     * Returns an Args from more than one pairs of value.
     *
     * @param input key-value pairs: k v, k v, ...
     * @return Args instance
     */
    public static Args of(Object... input) {
        return ObjectUtil.pairsToMap(i -> Args.of(), input);
    }

    /**
     * Returns an Args from standard java bean entity.
     *
     * @param entity standard java bean with annotation {@link javax.persistence.Entity @Entity}.
     * @param     entity type
     * @return Args instance
     */
    public static  Args ofEntity(T entity) {
        return EntityUtil.entityToMap(entity, Args::new);
    }

    /**
     * Returns an entity.
     *
     * @param entityClass standard java bean class with annotation {@link javax.persistence.Entity @Entity}.
     * @param          entity type
     * @return entity
     */
    @SuppressWarnings("unchecked")
    public  E toEntity(@NotNull Class entityClass) {
        return EntityUtil.mapToEntity((Map) this, entityClass);
    }
}