处理重复的参数检查,抛出异常。

huangapple go评论109阅读模式
英文:

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(&quot;parameters.radius&quot;)) radius = config.getInt(&quot;parameters.radius&quot;);
if(config.contains(&quot;parameters.maximum_amount_of_animals&quot;)) maximum_amount_of_animals = config.getInt(&quot;parameters.maximum_amount_of_animals&quot;);
if(config.contains(&quot;parameters.minimum_amount_of_animals&quot;)) minimum_amount_of_animals = config.getInt(&quot;parameters.minimum_amount_of_animals&quot;);
if(config.contains(&quot;parameters.negative_animals_percentage&quot;)) negative_animals_percentage = config.getInt(&quot;parameters.negative_animals_percentage&quot;);
if(config.contains(&quot;parameters.negative_points_loss_percentage&quot;)) negative_points_loss_percentage = config.getInt(&quot;parameters.negative_points_loss_percentage&quot;);
if(config.contains(&quot;parameters.animals&quot;) &amp;&amp; config.isConfigurationSection(&quot;parameters.animals&quot;)) {
for(String s : config.getConfigurationSection(&quot;parameters.animals&quot;).getKeys(false))
animal_types.put(EntityType.valueOf(s), config.getInt(&quot;parameters.animals.&quot; + 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&lt;&gt;();
}

and then the handleExceptions() method is this one:

    public void handleExceptions() throws MinigameConfigurationException {
if(radius &lt;= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, radius + &quot;&quot;, &quot;&gt; 0&quot;);
if(maximum_amount_of_animals &lt; 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, maximum_amount_of_animals + &quot;&quot;, &quot;&gt; 0&quot;);
if(minimum_amount_of_animals &lt; 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, minimum_amount_of_animals + &quot;&quot;, &quot;&gt; 0&quot;);
if(negative_animals_percentage &lt;= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, negative_animals_percentage + &quot;&quot;, &quot;=&gt; 0 &amp; &lt;= 100&quot;);
if(negative_points_loss_percentage &lt;= 0)
throw new MinigameConfigurationException(MinigameConfigurationException.MinigameConfigurationExceptionType.ILLEGAL_ARGUMENT, negative_points_loss_percentage + &quot;&quot;, &quot;=&gt; 0 &amp; &lt;= 100&quot;);
}

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.

huangapple
  • 本文由 发表于 2020年10月22日 07:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64473083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定