
MyClass my = MyClass.builder().first(1).second(2.0).third("3").build(); new MyClass() {{ first = 1; second = 2.0; third = "3"; }} @Override public boolean equals(Object obj) { if(this == obj) return true; if(!super.equals(obj)) return false; if(getClass() != obj.getClass()) return false; ... } new TreeMap<String, Object>() {{ put("first", 1); put(second, 2.0); put("third", "3"); }} public static class Builder { public int first = -1 ; public double second = Double.NaN; public String third = null ; public MyClass create() { return new MyClass( first , second, third ); } } new MyClass.Builder(){{ first = 1; third = "3"; }}.create() public class MyBaseClass { protected static class BuilderImpl { public int first = -1 ; public double second = Double.NaN; public String third = null ; } public static class Builder extends BuilderImpl { public MyBaseClass create() { return new MyBaseClass( first , second, third ); } } ... } public class MyChildClass extends MyBaseClass { protected static class BuilderImpl extends MyBaseClass.BuilderImpl { public Object fourth = null; } public static class Builder extends BuilderImpl { public MyChildClass create() { return new MyChildClass( first , second, third , fourth ); } } ... } public static class Builder { public double second = Double.NaN; public String third = null ; public MyClass create(int first) { return new MyClass( first , second, third ); } } new MyClass.Builder(){{ third = "3"; }}.create(1) String fn = new fn(){{ first = 1; third = "3"; }}.invoke(); public interface TransitionNAME<T> { T NAME(TYPE v); } public interface GetterNAME { TYPE NAME(); } public interface TransitionNAME<T extends GetterNAME> { T NAME(TYPE v); } first second third - - - - - + - + - - + + + - - + - + + + - + + + first second third - - - / + - - /+ + + - /+/+ + + + /+/+/+ + - + /+/-/+ - + - /-/+ - + + /-/+/+ - - + /-/-/+ first second third - - - / * + - - /+ * + + - /+/+ * + + + /+/+/+ + - + /+/-/+ * - + - /-/+ - + + /-/+/+ * - - + /-/-/+ * first second third - - - / * + - - /+ * + + - /+/+ * + - + /+/-/+ * - + - /-/+ - + + /-/+/+ * - - + /-/-/+ * public interface Get_first { int first (); } public interface Get_second { double second(); } public interface Get_third { String third (); } public interface Trans_first <T extends Get_first > { T first (int first ); } public interface Trans_second<T extends Get_second> { T second(double second); } public interface Trans_third <T extends Get_third > { T third (String third ); } public interface G_1 extends Get_first {} public interface G_2 extends Get_second{} public interface G_3 extends Get_third {} public interface T_1<T extends G_1> extends Trans_first <T> {} public interface T_2<T extends G_2> extends Trans_second<T> {} public interface T_3<T extends G_3> extends Trans_third <T> {} public interface B extends T_1<B_1 >, T_2<B_2 >, T_3<B_3 > {} // - - - / * public interface B_1 extends T_2<B_1_2>, T_3<B_1_3> {} // + - - /+ * public interface B_1_2 extends {} // + + - /+/+ * public interface B_1_3 extends {} // + - + /+/-/+ * public interface B_2 extends T_1<B_1_2>, T_3<B_2_3> {} // /-/+ public interface B_2_3 extends {} // - + + /-/+/+ * public interface B_3 extends T_1<B_1_3>, T_2<B_2_3> {} // - - + /-/-/+ * public interface Built { MyClass build(); } // // | // | | // | | | // ------------- ---------------------------------- ----- // // first first first // | second | second | second // | | third| | third | | third // | | | | | | | | | public interface B extends T_1<B_1 >, T_2<B_2 >, T_3<B_3 >, Built {} // - - - / * public interface B_1 extends G_1, T_2<B_1_2>, T_3<B_1_3>, Built {} // + - - /+ * public interface B_1_2 extends G_1, G_2, Built {} // + + - /+/+ * public interface B_1_3 extends G_1, G_3, Built {} // + - + /+/-/+ * public interface B_2 extends G_2, T_1<B_1_2>, T_3<B_2_3> {} // /-/+ public interface B_2_3 extends G_2, G_3, Built {} // - + + /-/+/+ * public interface B_3 extends G_3, T_1<B_1_3>, T_2<B_2_3>, Built {} // - - + /-/-/+ * public interface Builder extends B {} public interface Built extends BuiltBase<MyClass> {} public interface Get_first extends GetBase { int first (); } public interface Get_second extends GetBase { double second(); } public interface Get_third extends GetBase { String third (); } public interface Trans_first <T extends Get_first > extends TransBase { T first (int first ); } public interface Trans_second<T extends Get_second> extends TransBase { T second(double second); } public interface Trans_third <T extends Get_third > extends TransBase { T third (String third ); } public MyClass build(B builder) { return new MyClass(-1 , Double.NaN , null); } public MyClass build(B_1 builder) { return new MyClass(builder.first(), Double.NaN , null); } public MyClass build(B_1_2 builder) { return new MyClass(builder.first(), builder.second(), null); } ... public MyClass build(BuiltValues values) { return new MyClass( // values ); } (values instanceof Get_first) ? ((Get_first) values).first() : -1 Object getValue(final Class< ? extends GetBase> key); (Integer) values.getValue(Get_first.class) public interface TypedGetter<T, GETTER> { Class<GETTER> getterClass(); }; public static final Classed<T> GET_FIRST = new Classed<Integer>(Get_first.class); public <T, GETTER> T get(TypedGetter<T, GETTER> typedGetter); <T extends GetBase> T get(Class<T> key); (null == values.get(Get_second.class)) ? Double.NaN: values.get(Get_second.class).second() <T extends GetBase> T get(Class<T> key, Object defaultValue); <T extends TransBase> T getDefault(Class< ? super T> key); values.getDefault(Get_third.class).third("1").third() protected static final Builder __builder = MegaBuilder.newBuilder( Builder.class, null, new ClassBuilder<Object, MyClass>() { @Override public MyClass build(Object context, BuiltValues values) { return new MyClass( (values instanceof Get_first) ? ((Get_first) values).first() : -1, (null == values.get(Get_second.class)) ? Double.NaN: values.get(Get_second.class).second(), values.getDefault(Get_third.class).third(null).third() ); } } ); public static Builder builder() { return __builder; } builder() .build(); builder().first(1) .build(); builder().first(1).second(2) .build(); builder().second(2 ).first (1).build(); builder().first(1) .third("3").build(); builder().third ("3").first (1).build(); builder() .second(2).third("3").build(); builder().third ("3").second(2).build(); builder() .third("3").build(); Source: https://habr.com/ru/post/261163/
All Articles