【问题标题】:JAVA: Loading a big configuration file to code - How to store it?JAVA:将大配置文件加载到代码中 - 如何存储它?
【发布时间】:2013-10-13 09:49:39
【问题描述】:

我的 bukkit 插件有很大的配置文件,但我不知道如何为其创建配置类。 如果我用这么多变量创建一个类,那么很难使用和阅读/编辑。

所以我有使用内部 clesses 的新想法,但是性能等呢?

那个示例配置文件:(在 YAML 中,真正的配置文件要大得多) http://pastebin.com/09WMq5qG

以及我对该示例的代码:

public class Cfg {
    protected General general;
    protected General2 general2;

    public Cfg(ConfigurationSection cfg) {
        general = new General(cfg.getConfigurationSection("General"));
        general2 = new General2(cfg.getConfigurationSection("General2"));
    }

    public class General {
        protected Gen2 gen2;
        protected Gen5 gen5;

        protected int op1;
        protected String op2;

        public General(ConfigurationSection general) {
            this.op1 = general.getInt("op1");
            this.op2 = general.getString("op2");
            gen2 = new Gen2(general.getConfigurationSection("Gen2"));
            gen5 = new Gen5(general.getConfigurationSection("Gen5"));
        }

        public class Gen2 {
            protected Gen3 gen3;
            protected Gen4 gen4;

            protected int lol;
            protected String edrd;

            public Gen2(ConfigurationSection gen2) {
                this.lol = gen2.getInt("lol");
                this.edrd = gen2.getString("edrd");
                gen3 = new Gen3(gen2.getConfigurationSection("Gen3"));
                gen4 = new Gen4(gen2.getConfigurationSection("Gen4"));
            }

            public class Gen3 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen3(ConfigurationSection gen3) {
                    this.dsdgdf = gen3.getInt("dsdgdf");
                    this.djkw4g = gen3.getString("djkw4g");
                }
            }

            public class Gen4 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen4(ConfigurationSection gen4) {
                    this.dsdgdf = gen4.getInt("dsdgdf");
                    this.djkw4g = gen4.getString("djkw4g");
                }
            }
        }

        public class Gen5 {
            protected Gen6 gen6;
            protected Gen7 gen7;

            protected int lol;
            protected String edrd;

            public Gen5(ConfigurationSection gen5) {
                this.lol = gen5.getInt("lol");
                this.edrd = gen5.getString("edrd");
                gen6 = new Gen6(gen5.getConfigurationSection("Gen6"));
                gen7 = new Gen7(gen5.getConfigurationSection("Gen7"));
            }

            public class Gen6 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen6(ConfigurationSection gen6) {
                    this.dsdgdf = gen6.getInt("dsdgdf");
                    this.djkw4g = gen6.getString("djkw4g");
                }
            }

            public class Gen7 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen7(ConfigurationSection gen7) {
                    this.dsdgdf = gen7.getInt("dsdgdf");
                    this.djkw4g = gen7.getString("djkw4g");
                }
            }
        }
    }

    public class General2 {
        protected Gen2 gen2;
        protected Gen5 gen5;

        protected int op1;
        protected String op2;

        public General2(ConfigurationSection general2) {
            this.op1 = general2.getInt("op1");
            this.op2 = general2.getString("op2");
            gen2 = new Gen2(general2.getConfigurationSection("Gen2"));
            gen5 = new Gen5(general2.getConfigurationSection("Gen5"));
        }

        public class Gen2 {
            protected Gen3 gen3;
            protected Gen4 gen4;

            protected int lol;
            protected String edrd;

            public Gen2(ConfigurationSection gen2) {
                this.lol = gen2.getInt("lol");
                this.edrd = gen2.getString("edrd");
                gen3 = new Gen3(gen2.getConfigurationSection("Gen3"));
                gen4 = new Gen4(gen2.getConfigurationSection("Gen4"));
            }

            public class Gen3 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen3(ConfigurationSection gen3) {
                    this.dsdgdf = gen3.getInt("dsdgdf");
                    this.djkw4g = gen3.getString("djkw4g");
                }
            }

            public class Gen4 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen4(ConfigurationSection gen4) {
                    this.dsdgdf = gen4.getInt("dsdgdf");
                    this.djkw4g = gen4.getString("djkw4g");
                }
            }
        }

        public class Gen5 {
            protected Gen6 gen6;
            protected Gen7 gen7;

            protected int lol;
            protected String edrd;

            public Gen5(ConfigurationSection gen5) {
                this.lol = gen5.getInt("lol");
                this.edrd = gen5.getString("edrd");
                gen6 = new Gen6(gen5.getConfigurationSection("Gen6"));
                gen7 = new Gen7(gen5.getConfigurationSection("Gen7"));
            }

            public class Gen6 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen6(ConfigurationSection gen6) {
                    this.dsdgdf = gen6.getInt("dsdgdf");
                    this.djkw4g = gen6.getString("djkw4g");
                }
            }

            public class Gen7 {
                protected int dsdgdf;
                protected String djkw4g;

                public Gen7(ConfigurationSection gen7) {
                    this.dsdgdf = gen7.getInt("dsdgdf");
                    this.djkw4g = gen7.getString("djkw4g");
                }
            }
        }
    }
}

对我来说,使用它比将所有变量存储在一个类中更容易,但性能如何?这将是一个好方法还是只是减慢代码?

//对不起我的英语。

【问题讨论】:

  • 为什么您认为这样的配置会成为性能问题?您指的是哪种性能 - 在启动期间加载配置或在“运行时”使用配置?
  • @home 我还是 Java 新手,只是不知道性能会怎样。我在启动时加载此配置(不一定很快)并使用该类来使用配置中的变量(并且必须很快)。

标签: java class configuration inner-classes bukkit


【解决方案1】:

您可以做什么:加载配置文件并将全部内容存储在一个变量中(数组、ArrayList、List、Hashmap,任何适合您的特定需求)。在此之后,您的所有班级都可以阅读它。您可以在配置文件内容中强制执行明确的顺序,以便程序的某些部分准确地知道,例如,他们必须在哪个行/数组索引范围内搜索他们需要的信息。您还可以定义一个方法,将所有配置详细信息分发到程序的先前初始化部分。所有决定权都在你手里。因为你没有明确指出你打算做什么,所以我也不能在这里详细说明。

但是,我建议您不要在类中创建类,等等。 Java 为您提供了出色的面向对象工具,可帮助您将代码组织成易于阅读的小部分。

【讨论】:

  • 那么我在获取该变量时会遇到问题;/ 无法记住我需要的选项在哪里。我只想有一种简单的方法来使用从配置加载的变量。所以我有一个“Cfg”类的实例,当我需要配置中的东西时我会使用它。我看到了所有选项,我还可以使用 cmets 添加描述。我只需要一个 int 所以我使用。 int i = cfg.general.gen5.dsdgdf 我只想知道:那会减慢代码(在运行时,而不是启动)?
  • 我认为这不会影响程序的性能。但是,随着配置启动的复杂性增加,阅读自己的代码将变得更加困难。如果其他人试图理解它,那将更加困难。您必须对交错类保持清晰而巧妙的设计,否则您迟早会面临我所写的问题。 Cfg.window.width 之类的东西听起来仍然合理,但最好避免像这样的参考丛林:Cfg.w.rec.r1.dim.w(这可能意味着:配置 -> 窗口 -> 矩形 -> 矩形 1 -> 尺寸 -> 宽度);)
  • 我使用配置路径中的名称,这样很容易找到。对我来说,这很容易阅读(比将其存储在 HashMap 中或在一个地方拥有 300 个大名称的变量要容易得多):)
猜你喜欢
  • 2014-06-30
  • 2011-04-16
  • 1970-01-01
  • 2012-01-06
  • 2011-02-15
  • 2018-09-23
  • 2014-08-05
  • 2021-12-28
  • 2012-04-08
相关资源
最近更新 更多