private void generateInstanceOrBuild(SQLiteDatabaseSchema schema, String schemaName, boolean instance) {
		// instance
		MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(instance ? "getInstance" : "build")
				.addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(className(schemaName));

		if (!instance) {
			methodBuilder.addParameter(DataSourceOptions.class, "options");
			methodBuilder.addJavadoc(
					"<p>Build instance. This method can be used only one time, on the application start.</p>\n");
		} else {
			methodBuilder.addJavadoc("<p>Retrieve instance.</p>\n");
		}

		methodBuilder.addStatement("$T result=instance", className(schemaName));
		methodBuilder.beginControlFlow("if (result==null)");
		methodBuilder.beginControlFlow("synchronized(mutex)");
		methodBuilder.addStatement("result=instance");
		methodBuilder.beginControlFlow("if (result==null)");

		if (instance) {
			methodBuilder.addCode("$T options=$T.builder()", DataSourceOptions.class, DataSourceOptions.class);

			if (schema.configCursorFactoryClazz != null) {
				methodBuilder.addCode("\n\t.cursorFactory(new $T())",
						TypeUtility.className(schema.configCursorFactoryClazz));
			}
			if (schema.configDatabaseErrorHandlerClazz != null) {
				methodBuilder.addCode("\n\t.errorHandler(new $T())",
						TypeUtility.className(schema.configDatabaseErrorHandlerClazz));
			}
			if (schema.configDatabaseLifecycleHandlerClazz != null) {
				methodBuilder.addCode("\n\t.databaseLifecycleHandler(new $T())",
						TypeUtility.className(schema.configDatabaseLifecycleHandlerClazz));
			}
			if (schema.configPopulatorClazz != null) {
				methodBuilder.addCode("\n\t.populator(new $T())", TypeUtility.className(schema.configPopulatorClazz));
			}

			methodBuilder.addCode("\n\t.inMemory($L)", schema.configInMemory);

			methodBuilder.addCode("\n\t.log($L)", schema.configLogEnabled);

			if (schema.configUpdateTasks != null && schema.configUpdateTasks.size() > 0) {
				for (Pair<Integer, String> task : schema.configUpdateTasks) {
					methodBuilder.addCode("\n\t.addUpdateTask($L, new $T())", task.value0,
							TypeUtility.className(task.value1));
				}
			}

			methodBuilder.addCode("\n\t.build();\n", DataSourceOptions.class, DataSourceOptions.class);
		}
		methodBuilder.addStatement("instance=result=new $L(options)", schemaName);

		generatePopulate(schema, methodBuilder, instance);

		if (!instance) {
			methodBuilder.nextControlFlow("else");
			methodBuilder.addStatement("throw new $T($S)", KriptonRuntimeException.class,
					"Datasource " + schemaName + " is already builded");
		}
		methodBuilder.endControlFlow();
		methodBuilder.endControlFlow();
		if (!instance) {
			methodBuilder.nextControlFlow("else");
			methodBuilder.addStatement("throw new $T($S)", KriptonRuntimeException.class,
					"Datasource " + schemaName + " is already builded");
		}
		methodBuilder.endControlFlow();

		methodBuilder.addCode("return result;\n");

		classBuilder.addMethod(methodBuilder.build());
	}
private void generateInstanceOrBuild ( SQLiteDatabaseSchema schema , String schemaName , boolean instance ) { MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( instance ? "getInstance" : "build" ) . addModifiers ( Modifier . PUBLIC , Modifier . STATIC ) . returns ( className ( schemaName ) ) ; if ( ! instance ) { methodBuilder . addParameter ( DataSourceOptions . class , "options" ) ; methodBuilder . addJavadoc ( "<p>Build instance. This method can be used only one time, on the application start.</p>\n" ) ; } else { methodBuilder . addJavadoc ( "<p>Retrieve instance.</p>\n" ) ; } methodBuilder . addStatement ( "$T result=instance" , className ( schemaName ) ) ; methodBuilder . beginControlFlow ( "if (result==null)" ) ; methodBuilder . beginControlFlow ( "synchronized(mutex)" ) ; methodBuilder . addStatement ( "result=instance" ) ; methodBuilder . beginControlFlow ( "if (result==null)" ) ; if ( instance ) { methodBuilder . addCode ( "$T options=$T.builder()" , DataSourceOptions . class , DataSourceOptions . class ) ; if ( schema . configCursorFactoryClazz != null ) { methodBuilder . addCode ( "\n\t.cursorFactory(new $T())" , TypeUtility . className ( schema . configCursorFactoryClazz ) ) ; } if ( schema . configDatabaseErrorHandlerClazz != null ) { methodBuilder . addCode ( "\n\t.errorHandler(new $T())" , TypeUtility . className ( schema . configDatabaseErrorHandlerClazz ) ) ; } if ( schema . configDatabaseLifecycleHandlerClazz != null ) { methodBuilder . addCode ( "\n\t.databaseLifecycleHandler(new $T())" , TypeUtility . className ( schema . configDatabaseLifecycleHandlerClazz ) ) ; } if ( schema . configPopulatorClazz != null ) { methodBuilder . addCode ( "\n\t.populator(new $T())" , TypeUtility . className ( schema . configPopulatorClazz ) ) ; } methodBuilder . addCode ( "\n\t.inMemory($L)" , schema . configInMemory ) ; methodBuilder . addCode ( "\n\t.log($L)" , schema . configLogEnabled ) ; if ( schema . configUpdateTasks != null && schema . configUpdateTasks . size ( ) > 0 ) { for ( Pair < Integer , String > task : schema . configUpdateTasks ) { methodBuilder . addCode ( "\n\t.addUpdateTask($L, new $T())" , task . value0 , TypeUtility . className ( task . value1 ) ) ; } } methodBuilder . addCode ( "\n\t.build();\n" , DataSourceOptions . class , DataSourceOptions . class ) ; } methodBuilder . addStatement ( "instance=result=new $L(options)" , schemaName ) ; generatePopulate ( schema , methodBuilder , instance ) ; if ( ! instance ) { methodBuilder . nextControlFlow ( "else" ) ; methodBuilder . addStatement ( "throw new $T($S)" , KriptonRuntimeException . class , "Datasource " + schemaName + " is already builded" ) ; } methodBuilder . endControlFlow ( ) ; methodBuilder . endControlFlow ( ) ; if ( ! instance ) { methodBuilder . nextControlFlow ( "else" ) ; methodBuilder . addStatement ( "throw new $T($S)" , KriptonRuntimeException . class , "Datasource " + schemaName + " is already builded" ) ; } methodBuilder . endControlFlow ( ) ; methodBuilder . addCode ( "return result;\n" ) ; classBuilder . addMethod ( methodBuilder . build ( ) ) ; }
Generate inner code for instance and build methods. Inspired by <a href=
"https://www.journaldev.com/171/thread-safety-in-java-singleton-classes-with-example-code">this
link<</a>

@param schema
the schema
@param schemaName
the schema name
@param instance
the instance