0

I have big configuration file to my bukkit plugin but I don't know how to create a config class for it. If I create one class with that amount of variables then is hard to use and read/edit.

So I have new idea using inner-clesses but how about performance etc?

That example configuration file: (in YAML, and the real configuration file is much bigger) http://pastebin.com/09WMq5qG

And my code to that example:

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");
                }
            }
        }
    }
}

Using this is easier for me than storing all variables in one class but how about performance? That will be good method or just slow down the code?

//Sorry for my English.

user2250333
  • 133
  • 3
  • 17
  • Why you think performance becomes an issue with a configuration like this? Which kind of performance do you refer to - loading the config during startup or using the config 'during runtime'? – home Oct 13 '13 at 10:41
  • @home I still new to java and I just don't know what will be effect performance. I loading this config on startup (that don't must be fast) and using that class to use variables from config (and that must be fast). – user2250333 Oct 13 '13 at 11:01

1 Answers1

0

What you could do: Load the configuration file and store the whole content in one variabel (array, ArrayList, List, Hashmap, whatever suits your particular needs). After this, all your classes are able to read it. You can either enforce a clear order in your configuration file content, so that parts of your program know exactly, for example, in which line/array index range they have to search for the information they need. You could also define a method which distributes all the configuration details to previously initialized parts of your program. It is all up to you. Because you didn't pointed out preciyle what you intend to do I can't do in much more detail here either.

However, I would suggest that you stay away from creating classes inside classes inside classes and so on. Java gives you great object-oriented tools to help you organize your code in small readable portions.

Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
  • Then I will be have problems with get that variables ;/ That not possible to remember where is that option what I need. I just want have easy way to using variables loaded from config. So i have one instance of that "Cfg" class, and i use it when I need something from config. And I see all options and I also can add description using comments. I just need one of int so i use. `int i = cfg.general.gen5.dsdgdf` I just want know: That will be slow down the code (in runtime, NOT startup)? – user2250333 Oct 13 '13 at 13:17
  • 1
    I don't think it will bad for the program's performance. However, with increasing complexity of your configuration startup, it will be harder to read your own code. If somebody else is trying to understand it, it will be even harder. You would have to keep a clear and smart design of you interlaced classes, or you will likely face the problems I wrote about sooner or later. Something like `Cfg.window.width` sounds still reasonable, but better avoid reference jungles like this one: `Cfg.w.rec.r1.dim.w` (which could mean: config -> window -> rectangles -> rectangle 1 -> dimension -> width) ;) – Florian R. Klein Oct 13 '13 at 14:36
  • I using names like in configuration path, so that will be easy to find. For me that is easy to read (much easier than storing that in HashMap or having 300 variables with big names in one place) :) – user2250333 Oct 13 '13 at 15:22