Java Code Examples for com.google.api.client.util.Types#getIterableParameter()

The following examples show how to use com.google.api.client.util.Types#getIterableParameter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpHeaders.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/** Parses the specified case-insensitive header pair into this HttpHeaders instance. */
void parseHeader(String headerName, String headerValue, ParseHeaderState state) {
  List<Type> context = state.context;
  ClassInfo classInfo = state.classInfo;
  ArrayValueMap arrayValueMap = state.arrayValueMap;
  StringBuilder logger = state.logger;

  if (logger != null) {
    logger.append(headerName + ": " + headerValue).append(StringUtils.LINE_SEPARATOR);
  }
  // use field information if available
  FieldInfo fieldInfo = classInfo.getFieldInfo(headerName);
  if (fieldInfo != null) {
    Type type = Data.resolveWildcardTypeOrTypeVariable(context, fieldInfo.getGenericType());
    // type is now class, parameterized type, or generic array type
    if (Types.isArray(type)) {
      // array that can handle repeating values
      Class<?> rawArrayComponentType =
          Types.getRawArrayComponentType(context, Types.getArrayComponentType(type));
      arrayValueMap.put(
          fieldInfo.getField(),
          rawArrayComponentType,
          parseValue(rawArrayComponentType, context, headerValue));
    } else if (Types.isAssignableToOrFrom(
        Types.getRawArrayComponentType(context, type), Iterable.class)) {
      // iterable that can handle repeating values
      @SuppressWarnings("unchecked")
      Collection<Object> collection = (Collection<Object>) fieldInfo.getValue(this);
      if (collection == null) {
        collection = Data.newCollectionInstance(type);
        fieldInfo.setValue(this, collection);
      }
      Type subFieldType = type == Object.class ? null : Types.getIterableParameter(type);
      collection.add(parseValue(subFieldType, context, headerValue));
    } else {
      // parse value based on field type
      fieldInfo.setValue(this, parseValue(type, context, headerValue));
    }
  } else {
    // store header values in an array list
    @SuppressWarnings("unchecked")
    ArrayList<String> listValue = (ArrayList<String>) this.get(headerName);
    if (listValue == null) {
      listValue = new ArrayList<String>();
      this.set(headerName, listValue);
    }
    listValue.add(headerValue);
  }
}
 
Example 2
Source File: GoogleAtom.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
private static void appendFieldsFor(
    StringBuilder fieldsBuf, Class<?> dataClass, int[] numFields) {
  if (Map.class.isAssignableFrom(dataClass) || Collection.class.isAssignableFrom(dataClass)) {
    throw new IllegalArgumentException(
        "cannot specify field mask for a Map or Collection class: " + dataClass);
  }
  ClassInfo classInfo = ClassInfo.of(dataClass);
  for (String name : new TreeSet<String>(classInfo.getNames())) {
    FieldInfo fieldInfo = classInfo.getFieldInfo(name);
    if (fieldInfo.isFinal()) {
      continue;
    }
    if (++numFields[0] != 1) {
      fieldsBuf.append(',');
    }
    fieldsBuf.append(name);
    // TODO(yanivi): handle Java arrays?
    Class<?> fieldClass = fieldInfo.getType();
    if (Collection.class.isAssignableFrom(fieldClass)) {
      // TODO(yanivi): handle Java collection of Java collection or Java map?
      fieldClass = (Class<?>) Types.getIterableParameter(fieldInfo.getField().getGenericType());
    }
    // TODO(yanivi): implement support for map when server implements support for *:*
    if (fieldClass != null) {
      if (fieldInfo.isPrimitive()) {
        if (name.charAt(0) != '@' && !name.equals("text()")) {
          // TODO(yanivi): wait for bug fix from server to support text() -- already fixed???
          // buf.append("/text()");
        }
      } else if (!Collection.class.isAssignableFrom(fieldClass)
          && !Map.class.isAssignableFrom(fieldClass)) {
        int[] subNumFields = new int[1];
        int openParenIndex = fieldsBuf.length();
        fieldsBuf.append('(');
        // TODO(yanivi): abort if found cycle to avoid infinite loop
        appendFieldsFor(fieldsBuf, fieldClass, subNumFields);
        updateFieldsBasedOnNumFields(fieldsBuf, openParenIndex, subNumFields[0]);
      }
    }
  }
}