balance在java中的含义,自定义类型,其作用类似于Java中的盒形基元

我想创建类似integer或double的自定义类型。 免得说我有多级导航地图:// [UserLevel: [UserBalance: Multiplier]]

NavigableMap> multiplierByBalanceAndLevel

如果你不仔细看一下评论,这是绝对不可读的。 而且,当我使用这样的映射时,我总是应该转到声明并检查注释。 这真的很烦人,我想要这样的东西:NavigableMap> multiplierByBalanceAndLevel

例如,在scala中,可以使用:type UserLevel = Int

有没有可能以某种方式在Java实施? 也许有一些图书馆是为了这样的目的?

附注。 我可以用lombok帮助编写自定义实现import java.util.NavigableMap;

import com.google.common.collect.ImmutableSortedMap;

import lombok.Value;

import lombok.experimental.Delegate;

public class TestExample {

@Value

static class UserLevel extends Number implements Comparable {

@Delegate

Integer userLevel;

public int compareTo(UserLevel another) {

return this.userLevel.compareTo(another.getUserLevel());

}

}

@Value

static class UserBalance extends Number implements Comparable {

@Delegate

Double userBalance;

public int compareTo(UserBalance another) {

return this.userBalance.compareTo(another.getUserBalance());

}

}

@Value

static class Multiplier extends Number implements Comparable {

@Delegate

Double multiplier;

public int compareTo(Multiplier another) {

return this.multiplier.compareTo(another.getMultiplier());

}

}

public static void main(String[] args) {

double originalAmount = 10;

NavigableMap> multiplierByBalanceAndLevel =

ImmutableSortedMap.of(new UserLevel(1),

ImmutableSortedMap.of(new UserBalance(2.0), new Multiplier(3.0)));

Multiplier multiplier = multiplierByBalanceAndLevel

.floorEntry(new UserLevel(13)).getValue()

.ceilingEntry(new UserBalance(1.8)).getValue();

double amount = originalAmount * multiplier.getMultiplier();

}

}

但是使用包装好的类看起来很难看,而且会产生很多样板代码,因为它们的行为不像自动装箱的代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值