Return the 32 character left padded hex version of the given id. These
can be lexicographically sorted.

@param time     a time value to encode
@param sequence a sequence value to encode
@return 32 character left padded hex version of the given id
349010
@Override
    public String encodeAsString(long time, int sequence) {
        StringBuilder s = new StringBuilder(32);
        ByteBuffer bb = ByteBuffer.wrap(encodeAsBytes(time, sequence));
        s.append(leftPad(toHexString(bb.getLong()), 16, '0'));
        s.append(leftPad(toHexString(bb.getLong()), 16, '0'));

        return s.toString();
    }
791037
public static int convertStringToTimeSeconds(String time) {
        int result = 0;
        if (time.endsWith("H")) {
            int hoursToAdd = Integer.valueOf(StringUtils.remove(time, 'H'));
            result = (60 * 60) * hoursToAdd;
        } else if (time.endsWith("M")) {
            int minsToAdd = Integer.valueOf(StringUtils.remove(time, 'M'));
            result = 60 * minsToAdd;
        } else if (time.endsWith("S")) {
            int secsToAdd = Integer.valueOf(StringUtils.remove(time, 'S'));
            result = secsToAdd;
        }
        return result;
    }
484620
public static StringBuilder epochMicro(long timeMicros) {
        // 24 (human readable part) + 2 (parens) + 19 (max digits of a long integer)
        final StringBuilder buf = new StringBuilder(45);
        appendEpochMicros(buf, timeMicros);
        return buf;
    }
973762
public static String deepCleanSequence(String sequence) {
	sequence = SequenceUtil.cleanSequence(sequence);
	sequence = SequenceUtil.DIGIT.matcher(sequence).replaceAll("");
	sequence = SequenceUtil.NONWORD.matcher(sequence).replaceAll("");
	final Pattern othernonSeqChars = Pattern.compile("[_-]+");
	sequence = othernonSeqChars.matcher(sequence).replaceAll("");
	return sequence;
	}
480781
@Override
  public final String toDateTimeSecString(final Date pDate,
    final Map<String, Object> pAddParam) throws Exception {
    return this.dateTimeSecFormat.format(pDate);
  }
665929
@Override
    public CharSequence randomChars()
    {
        StringBuilder result = new StringBuilder(64);
        SecureRandom random = new SecureRandom();
        for (int i = 0, count = result.capacity(); i < count; ++i)
        {
            result.append(STATE_CHARS.charAt(random.nextInt(STATE_CHARS.length())));
        }
        return result.toString();
    }
982230
private static int indexInUpperAlphaRange(final CharSequence sequence, final int start)
    {
        final int length = sequence.length();
        if (start < 0 || start > length)
        {
            throw new IndexOutOfBoundsException();
        }

        for (int i = start; i < length; i++)
        {
            final char c = sequence.charAt(i);
            if ('A' <= c && c <= 'Z')
            {
                return i;
            }
        }

        return -1;
    }
280356
public M sequence(String sequenceKey, SequenceConfig sequenceConfig) {
        getSequence(sequenceKey, firstNonNull(sequenceConfig, defaultSequenceConfig()));
        return THIS();
    }
393546
protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException
    {
        long result;
        // lookup sequence name
        String sequenceName = calculateSequenceName(field);
        try
        {
            result = buildNextSequence(field.getClassDescriptor(), sequenceName);
        }
        catch (Throwable e)
        {
            // maybe the sequence was not created
            try
            {
                log.info("Create DB sequence key '"+sequenceName+"'");
                createSequence(field.getClassDescriptor(), sequenceName);
            }
            catch (Exception e1)
            {
                throw new SequenceManagerException(
                        SystemUtils.LINE_SEPARATOR +
                        "Could not grab next id, failed with " + SystemUtils.LINE_SEPARATOR +
                        e.getMessage() + SystemUtils.LINE_SEPARATOR +
                        "Creation of new sequence failed with " +
                        SystemUtils.LINE_SEPARATOR + e1.getMessage() + SystemUtils.LINE_SEPARATOR
                        , e1);
            }
            try
            {
                result = buildNextSequence(field.getClassDescriptor(), sequenceName);
            }
            catch (Throwable e1)
            {
                throw new SequenceManagerException("Could not grab next id, sequence seems to exist", e);
            }
        }
        return result;
    }
487001
public static <T extends Appendable> T toHexStringPadded(T dst, byte[] src, int offset, int length) {
        final int end = offset + length;
        for (int i = offset; i < end; i++) {
            byteToHexStringPadded(dst, src[i]);
        }
        return dst;
    }
579962
public static String toHex(byte[] data, int length) {
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i != length; i++) {
            int v = data[i] & 0xff;

            buffer.append(DIGITS.charAt(v >> 4));
            buffer.append(DIGITS.charAt(v & 0xf));
        }

        return buffer.toString();
    }
646025
private synchronized long nextId(boolean isPadding, long busId) {

        long timestamp = timeGen();
        long paddingnum = regionId;

        if (isPadding) {
            paddingnum = busId;
        }

        if (timestamp < lastTimestamp) {
            try {
                throw new Exception("Clock moved backwards.  Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //如果上次生成时间和当前时间相同,在同一毫秒内
        if (lastTimestamp == timestamp) {
            //sequence自增，因为sequence只有10bit，所以和sequenceMask相与一下，去掉高位
            sequence = (sequence + 1) & sequenceMask;
            //判断是否溢出,也就是每毫秒内超过1024，当为1024时，与sequenceMask相与，sequence就等于0
            if (sequence == 0) {
                //自旋等待到下一毫秒
                timestamp = tailNextMillis(lastTimestamp);
            }
        } else {
            // 如果和上次生成时间不同,重置sequence，就是下一毫秒开始，sequence计数重新从0开始累加,
            // 为了保证尾数随机性更大一些,最后一位设置一个随机数
            sequence = new SecureRandom().nextInt(10);
        }

        lastTimestamp = timestamp;

        return ((timestamp - twepoch) << timestampLeftShift) | (paddingnum << regionIdShift) | (workerId << workerIdShift) | sequence;
    }
195741
public static CharSequence getRelativeTimeSpanString(Context context, ReadablePartial time) {
        return getRelativeTimeSpanString(context, time.toDateTime(DateTime.now()));
    }
623637
public static long getId(int serverId)
	{
		synchronized (seq) {
			long second=System.currentTimeMillis()/1000;
			System.out.println("second:"+second);
			int inc=seq.incrementAndGet();
			System.out.println("inc:"+inc);
			return ((long)(serverId & 0xFFFF))<< 48 | 
					((second & 0x00000000FFFFFFFF) <<16) | (inc & 0x0000FFFF);
		}
	}
328857
public long readRawLittleEndian64() throws IOException
    {
        // final byte[] buffer = this.buffer;
        // int offset = this.offset;

        final byte[] bs = new byte[8];
        buffer.get(bs);

        // final byte b1 = buffer[offset++];
        // final byte b2 = buffer[offset++];
        // final byte b3 = buffer[offset++];
        // final byte b4 = buffer[offset++];
        // final byte b5 = buffer[offset++];
        // final byte b6 = buffer[offset++];
        // final byte b7 = buffer[offset++];
        // final byte b8 = buffer[offset++];
        //
        // this.offset = offset;

        return (((long) bs[0] & 0xff)) |
                (((long) bs[1] & 0xff) << 8) |
                (((long) bs[2] & 0xff) << 16) |
                (((long) bs[3] & 0xff) << 24) |
                (((long) bs[4] & 0xff) << 32) |
                (((long) bs[5] & 0xff) << 40) |
                (((long) bs[6] & 0xff) << 48) |
                (((long) bs[7] & 0xff) << 56);
    }
560896
public static String toHexLower(byte[] b, int off, int len) {
		return toHex(b, off, len, HEX_LOWER_CHAR);
	}
287562
@Override
  public int encodedLength(CharSequence in) {
    final Cache cache = CACHE.get();
    int estimated = (int) (in.length() * cache.encoder.maxBytesPerChar());
    if (cache.lastOutput == null || cache.lastOutput.capacity() < estimated) {
      cache.lastOutput = ByteBuffer.allocate(Math.max(128, estimated));
    }
    cache.lastOutput.clear();
    cache.lastInput = in;
    CharBuffer wrap = (in instanceof CharBuffer) ?
                          (CharBuffer) in : CharBuffer.wrap(in);
    CoderResult result = cache.encoder.encode(wrap, cache.lastOutput, true);
    if (result.isError()) {
      try {
        result.throwException();
      } catch (CharacterCodingException e) {
        throw new IllegalArgumentException("bad character encoding", e);
      }
    }
    return cache.lastOutput.remaining();
  }