英文:
Handle repetitive argument check that throws an exception
问题
所以我在我的代码中遇到了一个问题:我有大约30个继承自一个名为"Minigame"的超类的类。
每个类都有相似的参数,但也有每个类特定的参数,取决于小游戏的类型。
其中一些参数可能是不合法的(例如,负半径),在这种情况下,我想抛出我的MinigameConfigurationException异常。
问题是,这段代码看起来太过重复,我找不到更好的处理方法。
这是当前的代码:
@Override
public void setupMinigameParameters() {
super.setupMinigameParameters();
YamlConfiguration config = super.getGame().getMain().getFileManager().getConfig(Parameters.MINIGAMES_DIR() + super.getMinigameName()).getConfig();
if(config.contains("parameters.radius")) radius = config.getInt("parameters.radius");
if(config.contains("parameters.maximum_amount_of_animals")) maximum_amount_of_animals = config.getInt("parameters.maximum_amount_of_animals");
if(config.contains("parameters.minimum_amount_of_animals")) minimum_amount_of_animals = config.getInt("parameters.minimum_amount_of_animals");
if(config.contains("parameters.negative_animals_percentage")) negative_animals_percentage = config.getInt("parameters.negative_animals_percentage");
if(config.contains("parameters.negative_points_loss_percentage")) negative_points_loss_percentage = config.getInt("parameters.negative_points_loss_percentage");
if(config.contains("parameters.animals") && config.isConfigurationSection("parameters.animals")) {
for(String s : config.getConfigurationSection("parameters.animals").getKeys(false))
animal_types.put(EntityType.valueOf(s), config.getInt("parameters.animals." + s));
} else {
animal_types.put(EntityType.CHICKEN, 1);
animal_types.put(EntityType.PIG, 3);
animal_types.put(EntityType.COW, 5);
}
try {
handleExceptions();
} catch(MinigameConfigurationException e) {
stopMinigame();
e.printStackTrace();
}
this.animals = new HashMap<>();
}
然后handleExceptions()方法是这样的:
public void handleExceptions() throws MinigameConfigurationException {
if(radius <= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, radius + "", "> 0");
if(maximum_amount_of_animals < 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, maximum_amount_of_animals + "", "> 0");
if(minimum_amount_of_animals < 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, minimum_amount_of_animals + "", "> 0");
if(negative_animals_percentage <= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, negative_animals_percentage + "", "=> 0 & <= 100");
if(negative_points_loss_percentage <= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, negative_points_loss_percentage + "", "=> 0 & <= 100");
}
感觉这是不好的代码,但另一方面,我找不到不同的解决方法。
- 顺便说一下,这段代码来自一个类,我需要在另外30个类中也这样做,所以显得非常重复。
英文:
So I came across a problem with my code: I have ~30 classes which inherit from a superclass called "Minigame".
Each class has similar parameters, but also per-class parameters, depending on the type of the minigame.
Some of the parameters can be illegal (for example, a negative radius), and in a case like this, I want to throw my MinigameConfigurationException.
The problem is that the code seems too repetitive and I couldn't find a better way to handle it.
This is the current code:
@Override
public void setupMinigameParameters() {
super.setupMinigameParameters();
YamlConfiguration config = super.getGame().getMain().getFileManager().getConfig(Parameters.MINIGAMES_DIR() + super.getMinigameName()).getConfig();
if(config.contains("parameters.radius")) radius = config.getInt("parameters.radius");
if(config.contains("parameters.maximum_amount_of_animals")) maximum_amount_of_animals = config.getInt("parameters.maximum_amount_of_animals");
if(config.contains("parameters.minimum_amount_of_animals")) minimum_amount_of_animals = config.getInt("parameters.minimum_amount_of_animals");
if(config.contains("parameters.negative_animals_percentage")) negative_animals_percentage = config.getInt("parameters.negative_animals_percentage");
if(config.contains("parameters.negative_points_loss_percentage")) negative_points_loss_percentage = config.getInt("parameters.negative_points_loss_percentage");
if(config.contains("parameters.animals") && config.isConfigurationSection("parameters.animals")) {
for(String s : config.getConfigurationSection("parameters.animals").getKeys(false))
animal_types.put(EntityType.valueOf(s), config.getInt("parameters.animals." + s));
} else {
animal_types.put(EntityType.CHICKEN, 1);
animal_types.put(EntityType.PIG, 3);
animal_types.put(EntityType.COW, 5);
}
try {
handleExceptions();
} catch(MinigameConfigurationException e) {
stopMinigame();
e.printStackTrace();
}
this.animals = new HashMap<>();
}
and then the handleExceptions() method is this one:
public void handleExceptions() throws MinigameConfigurationException {
if(radius <= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, radius + "", "> 0");
if(maximum_amount_of_animals < 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, maximum_amount_of_animals + "", "> 0");
if(minimum_amount_of_animals < 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, minimum_amount_of_animals + "", "> 0");
if(negative_animals_percentage <= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, negative_animals_percentage + "", "=> 0 & <= 100");
if(negative_points_loss_percentage <= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, negative_points_loss_percentage + "", "=> 0 & <= 100");
}
It feels like bad code, but on the other hand, I couldn't find a different solution.
- By the way, this code is from one class, I need to do it in 30 other classes, that's why it's too repetitive.
答案1
得分: 0
在这种情况下,我看到您想要验证来自“外部”的数据,因此在这种情况下,Java Bean Validation库可能会有所帮助。您可以使用注释来指定每个字段的范围,并在一个方法中调用验证执行器来进行检查。如果对象包含某些非法状态,您可以将值和消息传递给异常。
英文:
In this case I see that you want to validate the data comes from "outside", so in this case Java Bean Validation lib can be helpful. You can use annotations to specify range of each field and in one method invoke validation executor which checks it. If object contains some illegal state you can pass value and message to the exception.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论