@SuppressWarnings({"unchecked", "rawtypes"})
 static Generator<Object> createMultiplicityWrapper(
     ComponentDescriptor instance, Generator<?> generator, BeneratorContext context) {
   String container = instance.getContainer();
   if (container == null) {
     Generator<Long> longCountGenerator =
         DescriptorUtil.createDynamicCountGenerator(instance, 1L, 1L, true, context);
     NonNullGenerator<Integer> countGenerator =
         WrapperFactory.asNonNullGenerator(
             new AsIntegerGeneratorWrapper<Number>((Generator) longCountGenerator));
     return new SimplifyingSingleSourceArrayGenerator(generator, countGenerator);
   }
   // handle container
   Generator<Long> longCountGenerator;
   if (instance.getLocalType().getSource() != null)
     longCountGenerator =
         DescriptorUtil.createDynamicCountGenerator(instance, null, null, true, context);
   else
     longCountGenerator =
         DescriptorUtil.createDynamicCountGenerator(instance, 1L, 1L, true, context);
   NonNullGenerator<Integer> countGenerator =
       WrapperFactory.asNonNullGenerator(
           new AsIntegerGeneratorWrapper<Number>((Generator) longCountGenerator));
   if ("array".equals(container))
     return new SingleSourceArrayGenerator(
         generator, generator.getGeneratedType(), countGenerator);
   else if ("list".equals(container))
     return new SingleSourceCollectionGenerator(generator, ArrayList.class, countGenerator);
   else if ("set".equals(container))
     return new SingleSourceCollectionGenerator(generator, HashSet.class, countGenerator);
   else if ("map".equals(container)) return (Generator<Object>) generator;
   else throw new SyntaxError("Not a supported container", container);
 }
 protected static ComponentBuilder<?> createScriptBuilder(
     ComponentDescriptor component, BeneratorContext context) {
   TypeDescriptor type = component.getTypeDescriptor();
   if (type == null) return null;
   String scriptText = type.getScript();
   if (scriptText == null) return null;
   Script script = ScriptUtil.parseScriptText(scriptText);
   Generator<?> generator = new ScriptGenerator(script);
   generator =
       DescriptorUtil.createConvertingGenerator(component.getTypeDescriptor(), generator, context);
   return builderFromGenerator(generator, component, context);
 }
 private static ComponentBuilder<?> builderFromGenerator(
     Generator<?> source, ComponentDescriptor descriptor, BeneratorContext context) {
   boolean nullability = DescriptorUtil.isNullable(descriptor, context);
   Double nullQuota = descriptor.getNullQuota();
   if (nullQuota != null && nullQuota != 0)
     source = context.getGeneratorFactory().applyNullSettings(source, nullability, nullQuota);
   TypeDescriptor typeDescriptor = descriptor.getTypeDescriptor();
   String scope = (typeDescriptor != null ? typeDescriptor.getScope() : null);
   if (descriptor instanceof ArrayElementDescriptor) {
     int index = ((ArrayElementDescriptor) descriptor).getIndex();
     return new ArrayElementBuilder(index, source, scope);
   } else return new PlainEntityComponentBuilder(descriptor.getName(), source, scope);
 }
 @SuppressWarnings({"unchecked", "rawtypes"})
 static ComponentBuilder<?> wrapWithCondition(
     ComponentDescriptor descriptor, ComponentBuilder<?> builder) {
   TypeDescriptor typeDescriptor = descriptor.getTypeDescriptor();
   if (typeDescriptor == null) return builder;
   String conditionText = typeDescriptor.getCondition();
   if (!StringUtil.isEmpty(conditionText)) {
     Expression<Boolean> condition = new ScriptExpression<Boolean>(conditionText);
     return new ConditionalComponentBuilder(builder, condition);
   } else return builder;
 }
  public static ComponentBuilder<?> createComponentBuilder(
      ComponentDescriptor descriptor, Uniqueness ownerUniqueness, BeneratorContext context) {
    LOGGER.debug("createComponentBuilder({})", descriptor.getName());

    ComponentBuilder<?> result = null;
    if (descriptor instanceof ArrayElementDescriptor)
      result = createPartBuilder(descriptor, ownerUniqueness, context);
    else if (descriptor instanceof PartDescriptor) {
      TypeDescriptor type = descriptor.getTypeDescriptor();
      if (type instanceof AlternativeGroupDescriptor)
        result =
            createAlternativeGroupBuilder(
                (AlternativeGroupDescriptor) type, ownerUniqueness, context);
      else result = createPartBuilder(descriptor, ownerUniqueness, context);
    } else if (descriptor instanceof ReferenceDescriptor)
      result = createReferenceBuilder((ReferenceDescriptor) descriptor, context);
    else if (descriptor instanceof IdDescriptor)
      result = createIdBuilder((IdDescriptor) descriptor, ownerUniqueness, context);
    else throw new ConfigurationError("Not a supported element: " + descriptor.getClass());
    result = wrapWithCondition(descriptor, result);
    return result;
  }