package cn.sagacloud.server.scheduler.utils

/*
 * Author: Jxing
 * Create Time: 2019/3/12
 */

import cn.sagacloud.server.scheduler.pojo.*
import com.fasterxml.uuid.Generators
import com.google.common.base.Strings
import java.nio.ByteBuffer
import java.nio.CharBuffer
import java.nio.charset.Charset
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.HashSet

object CommonUtil {
    val time: Long
        get() = System.currentTimeMillis() / 1000

    fun createSet(vararg elements: Command): Set<Command> {
        val set = HashSet<Command>()
        for (e in elements) {
            set.add(e)
        }
        return set
    }

    fun getBytes(chars: CharArray): ByteArray {
        val cs = Charset.forName("UTF-8")
        val cb = CharBuffer.allocate(chars.size)
        cb.put(chars)
        cb.flip()
        val bb = cs.encode(cb)
        return bb.array()
    }


    /**
     * 生成基于时间的UUID
     */
    fun generateUUID(prefix: String?, isRemoveDash: Boolean): String {
        //        UUID uuid = UUID.randomUUID();
        val uuid = Generators.timeBasedGenerator().generate()
        return arrayOf(Strings.nullToEmpty(prefix), if (isRemoveDash) uuid.toString().replace("-", "") else uuid.toString()).joinToString("")
    }

}
