@Override
 public void traverse(ObjectMapperListener objectMapperListener) {
   objectMapperListener.objectMapper(this);
   for (Mapper mapper : mappers.values()) {
     mapper.traverse(objectMapperListener);
   }
 }
 @Override
 public void unsetIncludeInAll() {
   includeInAll = null;
   // when called from outside, apply this on all the inner mappers
   for (Mapper mapper : mappers.values()) {
     if (mapper instanceof AllFieldMapper.IncludeInAll) {
       ((AllFieldMapper.IncludeInAll) mapper).unsetIncludeInAll();
     }
   }
 }
 @Override
 public void includeInAllIfNotSet(Boolean includeInAll) {
   if (this.includeInAll == null) {
     this.includeInAll = includeInAll;
   }
   // when called from outside, apply this on all the inner mappers
   for (Mapper mapper : mappers.values()) {
     if (mapper instanceof AllFieldMapper.IncludeInAll) {
       ((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll);
     }
   }
 }
  public void toXContent(XContentBuilder builder, Params params, ToXContent custom)
      throws IOException {
    builder.startObject(simpleName());
    if (nested.isNested()) {
      builder.field("type", NESTED_CONTENT_TYPE);
      if (nested.isIncludeInParent()) {
        builder.field("include_in_parent", true);
      }
      if (nested.isIncludeInRoot()) {
        builder.field("include_in_root", true);
      }
    } else if (mappers.isEmpty()
        && custom
            == null) { // only write the object content type if there are no properties, otherwise,
                       // it is automatically detected
      builder.field("type", CONTENT_TYPE);
    }
    if (dynamic != null) {
      builder.field("dynamic", dynamic.name().toLowerCase(Locale.ROOT));
    }
    if (enabled != Defaults.ENABLED) {
      builder.field("enabled", enabled);
    }
    if (pathType != Defaults.PATH_TYPE) {
      builder.field("path", pathType.name().toLowerCase(Locale.ROOT));
    }
    if (includeInAll != null) {
      builder.field("include_in_all", includeInAll);
    }

    if (custom != null) {
      custom.toXContent(builder, params);
    }

    doXContent(builder, params);

    // sort the mappers so we get consistent serialization format
    Mapper[] sortedMappers = Iterables.toArray(mappers.values(), Mapper.class);
    Arrays.sort(
        sortedMappers,
        new Comparator<Mapper>() {
          @Override
          public int compare(Mapper o1, Mapper o2) {
            return o1.name().compareTo(o2.name());
          }
        });

    int count = 0;
    for (Mapper mapper : sortedMappers) {
      if (!(mapper instanceof MetadataFieldMapper)) {
        if (count++ == 0) {
          builder.startObject("properties");
        }
        mapper.toXContent(builder, params);
      }
    }
    if (count > 0) {
      builder.endObject();
    }
    builder.endObject();
  }
 @Override
 public Iterator<Mapper> iterator() {
   return mappers.values().iterator();
 }
 @Override
 public void traverse(FieldMapperListener fieldMapperListener) {
   for (Mapper mapper : mappers.values()) {
     mapper.traverse(fieldMapperListener);
   }
 }
  public void toXContent(
      XContentBuilder builder, Params params, ToXContent custom, Mapper... additionalMappers)
      throws IOException {
    builder.startObject(name);
    if (nested.isNested()) {
      builder.field("type", NESTED_CONTENT_TYPE);
      if (nested.isIncludeInParent()) {
        builder.field("include_in_parent", true);
      }
      if (nested.isIncludeInRoot()) {
        builder.field("include_in_root", true);
      }
    } else if (mappers
        .isEmpty()) { // only write the object content type if there are no properties, otherwise,
                      // it is automatically detected
      builder.field("type", CONTENT_TYPE);
    }
    if (dynamic != null) {
      builder.field("dynamic", dynamic.name().toLowerCase(Locale.ROOT));
    }
    if (enabled != Defaults.ENABLED) {
      builder.field("enabled", enabled);
    }
    if (pathType != Defaults.PATH_TYPE) {
      builder.field("path", pathType.name().toLowerCase(Locale.ROOT));
    }
    if (includeInAll != null) {
      builder.field("include_in_all", includeInAll);
    }

    if (custom != null) {
      custom.toXContent(builder, params);
    }

    doXContent(builder, params);

    // sort the mappers so we get consistent serialization format
    Mapper[] sortedMappers = Iterables.toArray(mappers.values(), Mapper.class);
    Arrays.sort(
        sortedMappers,
        new Comparator<Mapper>() {
          @Override
          public int compare(Mapper o1, Mapper o2) {
            return o1.name().compareTo(o2.name());
          }
        });

    // check internal mappers first (this is only relevant for root object)
    for (Mapper mapper : sortedMappers) {
      if (mapper instanceof InternalMapper) {
        mapper.toXContent(builder, params);
      }
    }
    if (additionalMappers != null && additionalMappers.length > 0) {
      TreeMap<String, Mapper> additionalSortedMappers = new TreeMap<>();
      for (Mapper mapper : additionalMappers) {
        additionalSortedMappers.put(mapper.name(), mapper);
      }

      for (Mapper mapper : additionalSortedMappers.values()) {
        mapper.toXContent(builder, params);
      }
    }

    if (!mappers.isEmpty()) {
      builder.startObject("properties");
      for (Mapper mapper : sortedMappers) {
        if (!(mapper instanceof InternalMapper)) {
          mapper.toXContent(builder, params);
        }
      }
      builder.endObject();
    }
    builder.endObject();
  }
 @Override
 public void close() {
   for (Mapper mapper : mappers.values()) {
     mapper.close();
   }
 }