java static enum_java enum 的方法跟 static方法有什么区别

展开全部

enum的方62616964757a686964616fe78988e69d8331333337396330法是普通的方法。

下面是TimeUnit的部分代码public enum TimeUnit {

NANOSECONDS(0), MICROSECONDS(1), MILLISECONDS(2), SECONDS(3);

/** the index of this unit */

private final int index;

/** Internal constructor */

TimeUnit(int index) {

this.index = index;

}

/** Lookup table for conversion factors */

private static final int[] multipliers = {

1,

1000,

1000 * 1000,

1000 * 1000 * 1000

};

/**

* Lookup table to check saturation.  Note that because we are

* dividing these down, we don't have to deal with asymmetry of

* MIN/MAX values.

*/

private static final long[] overflows = {

0, // unused

Long.MAX_VALUE / 1000,

Long.MAX_VALUE / (1000 * 1000),

Long.MAX_VALUE / (1000 * 1000 * 1000)

};

/**

* Perform conversion based on given delta representing the

* difference between units

* @param delta the difference in index values of source and target units

* @param duration the duration

* @return converted duration or saturated value

*/

private static long doConvert(int delta, long duration) {

if (delta == 0)

return duration;

if (delta 

return duration / multipliers[-delta];

if (duration > overflows[delta])

return Long.MAX_VALUE;

if (duration 

return Long.MIN_VALUE;

return duration * multipliers[delta];

}

/**

* Convert the given time duration in the given unit to this

* unit.  Conversions from finer to coarser granularities

* truncate, so lose precision. For example converting

999 milliseconds to seconds results in

0. Conversions from coarser to finer granularities

* with arguments that would numerically overflow saturate to

Long.MIN_VALUE if negative or Long.MAX_VALUE

* if positive.

*

* @param duration the time duration in the given unit

* @param unit the unit of the duration argument

* @return the converted duration in this unit,

* or Long.MIN_VALUE if conversion would negatively

* overflow, or Long.MAX_VALUE if it would positively overflow.

*/

public long convert(long duration, TimeUnit unit) {

return doConvert(unit.index - index, duration);

}

/**

* Equivalent to NANOSECONDS.convert(duration, this).

* @param duration the duration

* @return the converted duration,

* or Long.MIN_VALUE if conversion would negatively

* overflow, or Long.MAX_VALUE if it would positively overflow.

* @see #convert

*/

public long toNanos(long duration) {

return doConvert(index, duration);

}

/**

* Equivalent to MICROSECONDS.convert(duration, this).

* @param duration the duration

* @return the converted duration,

* or Long.MIN_VALUE if conversion would negatively

* overflow, or Long.MAX_VALUE if it would positively overflow.

* @see #convert

*/

public long toMicros(long duration) {

return doConvert(index - MICROSECONDS.index, duration);

}

/**

* Equivalent to MILLISECONDS.convert(duration, this).

* @param duration the duration

* @return the converted duration,

* or Long.MIN_VALUE if conversion would negatively

* overflow, or Long.MAX_VALUE if it would positively overflow.

* @see #convert

*/

public long toMillis(long duration) {

return doConvert(index - MILLISECONDS.index, duration);

}

/**

* Equivalent to SECONDS.convert(duration, this).

* @param duration the duration

* @return the converted duration.

* @see #convert

*/

public long toSeconds(long duration) {

return doConvert(index - SECONDS.index, duration);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值