{"id":21864800,"url":"https://github.com/gmugra/net.cactusthorn.initializer","last_synced_at":"2025-03-21T21:11:35.473Z","repository":{"id":151118030,"uuid":"104585697","full_name":"Gmugra/net.cactusthorn.initializer","owner":"Gmugra","description":"A Java library to dynamically initialize Java Objects","archived":false,"fork":false,"pushed_at":"2019-04-29T07:53:08.000Z","size":109,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-26T15:33:42.360Z","etag":null,"topics":["annotation","java","java-library","java8","reflection"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gmugra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-23T17:24:47.000Z","updated_at":"2019-04-29T07:53:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"84d64169-33e3-4d1b-971f-4810cdc503a7","html_url":"https://github.com/Gmugra/net.cactusthorn.initializer","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gmugra%2Fnet.cactusthorn.initializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gmugra%2Fnet.cactusthorn.initializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gmugra%2Fnet.cactusthorn.initializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gmugra%2Fnet.cactusthorn.initializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gmugra","download_url":"https://codeload.github.com/Gmugra/net.cactusthorn.initializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244868763,"owners_count":20523590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotation","java","java-library","java8","reflection"],"created_at":"2024-11-28T04:12:30.418Z","updated_at":"2025-03-21T21:11:35.460Z","avatar_url":"https://github.com/Gmugra.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# net.cactusthorn.initializer\n\nInitializer is Java library that can be used to dynamically initialize object attributes.\n\nIt's about typical task to initialize some objects based on strings from, for example, configuration files.\n\n## What it can initialize for the moment?\n* all primitive and simple types, StringBuffer, StringBuilder, BigDecimal, BigInteger,\n* date/time: java.util.Date, java.util.Calendar, java.time.LocalDate, java.time.LocalDateTime, java.time.ZonedDateTime, java.time.Instant\n* one-dimensional arrays of any type which described above\n* Collections with generic of any types which described above\n* Maps with generic of any types which described above as key or value\n* recursive beans initialization -\u003e @InitBean\n* customizing date/time patterns\n* requirements policies\n* thread safe\n* naming -\u003e @InitPropertyName\n* initialization by environment variables -\u003e @InitEnvVariable\n* extendable for support new types, and new type will work with array, beans and coollection\n* Check unit tests - a lot of examples there\n\n## Simple example\nLet say we have the class:\n```java\nimport java.math.BigInteger;\nimport java.util.Arrays;\nimport java.util.Map;\n\nimport net.cactusthorn.initializer.annotations.*;\nimport static net.cactusthorn.initializer.annotations.InitPropertyPolicy.*;\n\n/*\n * All what need to do is mark attributes with @InitProperty or @InitPropertyName annotations. That is all.\n */\npublic class MyClass {\n\n\t@InitPropertyName(\"check\") private boolean isAvailable;\n\n\t@InitProperty private BigInteger[] values;\n\n\t@InitProperty(OPTIONAL) private Map\u003cString,java.util.Date\u003e dates;\n\n\t@Override\n\tpublic String toString() {\n\t\treturn dates.toString() + \" \u003c-\u003e \" + isAvailable + \" \u003c-\u003e \" + Arrays.asList(values).toString();\n\t}\n}\n```\nNow Lets initialize it:\n```java\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport net.cactusthorn.initializer.Initializer;\nimport net.cactusthorn.initializer.properties.InitProperties;\nimport net.cactusthorn.initializer.properties.InitPropertiesBuilder;\n\npublic class MyInit {\n\n\tpublic static void main(String... args) {\n\n\t\tMap\u003cString,String\u003e prop = new HashMap\u003c\u003e();\n\t\tprop.put(\"check\", \"true\");\n\t\tprop.put(\"values\", \"1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 7406529596973765\");\n\t\tprop.put(\"dates\", \"first=2017-09-17T11:16:50+01:00 , second=2017-10-05 , third=2017-09-17T11:16:50\");\n\n\t\tMyClass myClass =  new MyClass();\n\n\t\tInitProperties props = new InitPropertiesBuilder().setValuesSeparator(',').trimMultiValues(true).from(prop).build();\n\n\t\tnew Initializer().initialize(props, myClass);\n\n\t\tSystem.out.println(myClass.toString());\n\t}\n}\n```\n## Beans example\nProperties:\n```\ntest-bean.date = 2017-09-17T11:16:50+01:00\ntest-bean.map = A=10, B=20, C=30\ntest-bean.sub-test-bean.name = Super Name\ntest-bean.sub-test-bean.values = 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000\nsimple=SIMPLE\n```\nTest:\n```java\nimport org.junit.Test;\nimport static org.junit.Assert.*;\n\nimport java.io.IOException;\nimport java.net.URISyntaxException;\nimport java.nio.file.Path;\nimport java.nio.file.Paths;\nimport java.util.Map;\n\nimport net.cactusthorn.initializer.annotations.*;\nimport net.cactusthorn.initializer.properties.InitProperties;\nimport net.cactusthorn.initializer.properties.InitPropertiesBuilder;\n\nimport static net.cactusthorn.initializer.annotations.InitPropertyPolicy.*;\n\n@InitProperty(REQUIRED)\npublic class BeanTest {\n\n\t@InitProperty(REQUIRED)\n\tstatic class SubTestBean {\n\t\tString name;\n\t\tint[] values;\n\t}\n\n\t@InitProperty(REQUIRED)\n\tstatic class TestBean {\n\t\tjava.util.Date date;\n\t\tMap\u003cString, Integer\u003e map;\n\t\t@InitBean(\"sub-test-bean\") SubTestBean subTestBean;\n\t}\n\n\t@InitBean(\"test-bean\") TestBean testBean;\n\n\tString simple;\n\n\t@Test\n\tpublic void testBean() throws URISyntaxException, IOException {\n\n\t\tPath path = Paths.get(getClass().getClassLoader().getResource(\"init-bean.properties\").toURI());\n\n\t\tInitProperties prop = new InitPropertiesBuilder().load(path).trimMultiValues(true).build();\n\n\t\tnew Initializer().initialize(prop, this);\n\n\t\tassertEquals(\"Super Name\", testBean.subTestBean.name);\n\t\tassertEquals(9, testBean.subTestBean.values.length);\n\t\tassertEquals(3, testBean.map.size());\n\t\tassertEquals(\"SIMPLE\", simple);\n\t}\n}\n```\n## Custom types example\nProperties:\n```\nmyConcurrentMap = A = true,200 / B = true,300 / C = false,500\nmySimpleMap = C = true,200 / D = true,300 / F = false,500\nmySimpleArray = true,200/true,300/false,700\nmySimple = true,1000\n```\nTest:\n```java\nimport org.junit.Test;\nimport static org.junit.Assert.*;\n\nimport java.io.IOException;\nimport java.lang.reflect.Constructor;\nimport java.lang.reflect.Type;\nimport java.net.URISyntaxException;\nimport java.nio.file.Path;\nimport java.nio.file.Paths;\nimport java.util.*;\nimport java.util.concurrent.ConcurrentHashMap;\n\nimport net.cactusthorn.initializer.annotations.*;\nimport net.cactusthorn.initializer.properties.InitProperties;\nimport net.cactusthorn.initializer.properties.InitPropertiesBuilder;\nimport net.cactusthorn.initializer.types.*;\nimport static net.cactusthorn.initializer.InitializerException.StandardError.WRONG_VALUE;\n\npublic class CustomTypesTest {\n\n\tstatic class MySimple {\n\n\t\tboolean bool;\n\t\tint $int;\n\n\t\tpublic MySimple(boolean bool, int $int) {\n\t\t\tthis.bool = bool;\n\t\t\tthis.$int = $int;\n\t\t}\n\n\t\t@Override public String toString() { return bool + \"|\" + $int; }\n\n\t\t@Override\n\t\tpublic boolean equals(Object obj) {\n\t\t\tif (obj instanceof MySimple) {\n\t\t\t\tMySimple that = (MySimple)obj;\n\t\t\t\treturn bool == that.bool \u0026\u0026 $int == that.$int;\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\t@Override public int hashCode() { return $int; }\n\t}\n\n\tstatic class MySimpleType implements ITypes {\n\n\t\t@Override\n\t\tpublic MySimpleType clone() throws CloneNotSupportedException {\n\t\t\treturn (MySimpleType)super.clone();\n\t\t}\n\n\t\t@Override\n\t\tpublic Value\u003c?\u003e createObject(\n\t\t\t\tClass\u003c?\u003e fieldType,\n\t\t\t\tType fieldGenericType,\n\t\t\t\tInfo info,\n\t\t\t\tString propertyValue,\n\t\t\t\tInitProperties initProperties,\n\t\t\t\tCollection\u003cITypes\u003e availableTypes) throws InitializerException {\n\n\t\t\tboolean empty = propertyValue.isEmpty();\n\n\t\t\tif (MySimple.class.equals(fieldType) ) {\n\n\t\t\t\tif(empty) {\n\t\t\t\t\treturn Value._null();\n\t\t\t\t}\n\n\t\t\t\tString[] parts = propertyValue.split(\",\");\n\n\t\t\t\ttry {\n\t\t\t\t\treturn Value.of(new MySimple(Boolean.valueOf(parts[0] ), Integer.valueOf(parts[1] ) ) );\n\t\t\t\t} catch (Exception e) {\n\t\t\t\t\tthrow new InitializerException(info, WRONG_VALUE, e);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn Value.empty();\n\t\t}\n\t}\n\n\tstatic class MyMultiType extends MapTypes {\n\n\t\t@SuppressWarnings(\"unchecked\")\n\t\t@Override\n\t\tprotected Constructor\u003c? extends Map\u003cObject,Object\u003e\u003e getConstructor(Class\u003c?\u003e fieldType) {\n\n\t\t\tif (!ConcurrentHashMap.class.isAssignableFrom(fieldType) ) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\treturn (Constructor\u003c? extends Map\u003cObject,Object\u003e\u003e)ConcurrentHashMap.class.getConstructor();\n\t\t\t} catch (NoSuchMethodException|SecurityException e) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n\n\t}\n\n\t@InitProperty\n\tMySimple mySimple;\n\n\t@InitProperty\n\tMySimple[] mySimpleArray;\n\n\t@InitProperty\n\tSortedMap\u003cString,MySimple\u003e mySimpleMap;\n\n\t@InitProperty\n\tConcurrentHashMap\u003cString,MySimple\u003e myConcurrentMap;\n\n\n\t@Test\n\tpublic void testAll() throws URISyntaxException, IOException {\n\n\t\tMySimple[] correctArray = new MySimple[]{new MySimple(true, 200),new MySimple(true, 300),new MySimple(false, 700)};\n\n\t\tPath path = Paths.get(getClass().getClassLoader().getResource(\"init-custom.properties\").toURI());\n\n\t\tInitProperties prop = new InitPropertiesBuilder().setValuesSeparator('/').trimMultiValues(true).load(path).build();\n\n\t\tnew Initializer()\n\t\t\t.addTypes(MySimpleType.class)\n\t\t\t.addTypes(MyMultiType.class)\n\t\t\t.initialize(prop, this);\n\n\t\tassertEquals(3, myConcurrentMap.size());\n\t\tassertEquals(3, mySimpleMap.size());\n\t\tassertArrayEquals(correctArray, mySimpleArray);\n\t\tassertEquals(\"true|1000\", mySimple.toString());\n\t}\n}\n```\n\n## What it can NOT initialize\n* globally : attributes based on **not static** inner classes\n\n## License\nInitializer is released under the BSD 2-Clause License\n```\nCopyright (C) 2017, Alexei Khatskevich\nAll rights reserved.\n\nLicensed under the BSD 2-clause (Simplified) License (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\nhttp://opensource.org/licenses/BSD-2-Clause\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmugra%2Fnet.cactusthorn.initializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmugra%2Fnet.cactusthorn.initializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmugra%2Fnet.cactusthorn.initializer/lists"}