@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);
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  static ComponentBuilder<?> createReferenceBuilder(
      ReferenceDescriptor descriptor, BeneratorContext context) {
    SimpleTypeDescriptor typeDescriptor = (SimpleTypeDescriptor) descriptor.getTypeDescriptor();

    // check uniqueness
    boolean unique = DescriptorUtil.isUnique(descriptor, context);
    Uniqueness uniqueness = (unique ? Uniqueness.SIMPLE : Uniqueness.NONE);

    // do I only need to generate nulls?
    if (DescriptorUtil.isNullable(descriptor, context)
        && DescriptorUtil.shouldNullifyEachNullable(descriptor, context))
      return builderFromGenerator(createNullGenerator(descriptor, context), descriptor, context);

    // TODO use SimpleTypeGeneratorFactory?
    Generator<?> generator = null;
    generator = DescriptorUtil.getGeneratorByName(typeDescriptor, context);
    if (generator == null)
      generator = SimpleTypeGeneratorFactory.createScriptGenerator(typeDescriptor);
    if (generator == null)
      generator = SimpleTypeGeneratorFactory.createConstantGenerator(typeDescriptor, context);
    if (generator == null)
      generator =
          SimpleTypeGeneratorFactory.createValuesGenerator(typeDescriptor, uniqueness, context);

    // get distribution
    Distribution distribution =
        FactoryUtil.getDistribution(
            typeDescriptor.getDistribution(), descriptor.getUniqueness(), false, context);

    // check source
    if (generator == null) {
      // check target type
      String targetTypeName = descriptor.getTargetType();
      ComplexTypeDescriptor targetType =
          (ComplexTypeDescriptor) context.getDataModel().getTypeDescriptor(targetTypeName);
      if (targetType == null) throw new ConfigurationError("Type not defined: " + targetTypeName);

      // check targetComponent
      String targetComponent = descriptor.getTargetComponent();

      // check source
      String sourceName = typeDescriptor.getSource();
      if (sourceName == null) throw new ConfigurationError("'source' is not set for " + descriptor);
      Object sourceObject = context.get(sourceName);
      if (sourceObject instanceof StorageSystem) {
        StorageSystem sourceSystem = (StorageSystem) sourceObject;
        String selector = typeDescriptor.getSelector();
        String subSelector = typeDescriptor.getSubSelector();
        boolean subSelect = !StringUtil.isEmpty(subSelector);
        String selectorToUse = (subSelect ? subSelector : selector);
        if (isIndividualSelector(selectorToUse)) {
          generator = new DataSourceGenerator(sourceSystem.query(selectorToUse, true, context));
        } else {
          generator =
              new DataSourceGenerator(
                  sourceSystem.queryEntityIds(
                      targetTypeName,
                      selectorToUse,
                      context)); // TODO v0.7.2 query by targetComponent
          if (selectorToUse == null && distribution == null)
            if (context.isDefaultOneToOne()) distribution = new ExpandSequence();
            else distribution = SequenceManager.RANDOM_SEQUENCE;
        }
        if (subSelect) generator = WrapperFactory.applyHeadCycler(generator);
      } else throw new ConfigurationError("Not a supported source type: " + sourceName);
    }

    // apply distribution if necessary
    if (distribution != null)
      generator = new DistributingGenerator(generator, distribution, unique);

    // check multiplicity
    generator = ComponentBuilderFactory.createMultiplicityWrapper(descriptor, generator, context);
    if (LOGGER.isDebugEnabled()) LOGGER.debug("Created " + generator);

    // check 'cyclic' config
    generator = DescriptorUtil.wrapWithProxy(generator, typeDescriptor);
    return builderFromGenerator(generator, descriptor, context);
  }