Exemplo n.º 1
0
    @SuppressWarnings("rawtypes")
    @Override
    public final E map(R record) {
      try {
        E result = instance != null ? instance : constructor.newInstance();

        for (int i = 0; i < fields.length; i++) {
          for (java.lang.reflect.Field member : members[i]) {

            // [#935] Avoid setting final fields
            if ((member.getModifiers() & Modifier.FINAL) == 0) {
              map(record, result, member, i);
            }
          }

          for (java.lang.reflect.Method method : methods[i]) {
            Class<?> mType = method.getParameterTypes()[0];
            Object value = record.getValue(i, mType);

            // [#3082] Map nested collection types
            if (value instanceof Collection && List.class.isAssignableFrom(mType)) {
              Class componentType =
                  (Class)
                      ((ParameterizedType) method.getGenericParameterTypes()[0])
                          .getActualTypeArguments()[0];
              method.invoke(result, Convert.convert((Collection) value, componentType));
            }

            // Default reference types (including arrays)
            else {
              method.invoke(result, record.getValue(i, mType));
            }
          }
        }

        for (Entry<String, List<RecordMapper<R, Object>>> entry : nested.entrySet()) {
          String prefix = entry.getKey();

          for (RecordMapper<R, Object> mapper : entry.getValue()) {
            Object value = mapper.map(record);

            for (java.lang.reflect.Field member : getMatchingMembers(configuration, type, prefix)) {

              // [#935] Avoid setting final fields
              if ((member.getModifiers() & Modifier.FINAL) == 0) {
                map(value, result, member);
              }
            }

            for (Method method : getMatchingSetters(configuration, type, prefix)) {
              method.invoke(result, value);
            }
          }
        }

        return result;
      } catch (Exception e) {
        throw new MappingException("An error ocurred when mapping record to " + type, e);
      }
    }
Exemplo n.º 2
0
    @Override
    public final E map(R record) {
      try {
        for (int i = 0; i < fields.length; i++) {
          if (propertyIndexes[i] != null) {
            parameterValues[propertyIndexes[i]] = record.getValue(i);
          } else {
            for (java.lang.reflect.Field member : members[i]) {
              int index = propertyNames.indexOf(member.getName());

              if (index >= 0) {
                parameterValues[index] = record.getValue(i);
              }
            }

            if (methods[i] != null) {
              String name = getPropertyName(methods[i].getName());
              int index = propertyNames.indexOf(name);

              if (index >= 0) {
                parameterValues[index] = record.getValue(i);
              }
            }
          }
        }

        Object[] converted = Convert.convert(parameterValues, parameterTypes);
        return accessible(constructor).newInstance(converted);
      } catch (Exception e) {
        throw new MappingException("An error ocurred when mapping record to " + type, e);
      }
    }
Exemplo n.º 3
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K, V> Map<K, V> intoMap(Field<K> key, Field<V> value) {
    int kIndex = fieldsRow().indexOf(key);
    int vIndex = fieldsRow().indexOf(value);

    Map<K, V> map = new LinkedHashMap<K, V>();

    for (R record : this) {
      if (map.put((K) record.getValue(kIndex), (V) record.getValue(vIndex)) != null) {
        throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
      }
    }

    return map;
  }
Exemplo n.º 4
0
    @Override
    public Object map(R record) {
      AbstractRecord copy = (AbstractRecord) DSL.using(configuration).newRecord(f);

      for (int i = 0; i < f.length; i++) if (f[i] != null) copy.setValue(i, record.getValue(i));

      return d.map(record);
    }
Exemplo n.º 5
0
    @Override
    public final E map(R record) {
      int size = record.size();
      if (size != 1)
        throw new MappingException(
            "Cannot map multi-column record of degree " + size + " to value type " + type);

      return record.getValue(0, type);
    }
Exemplo n.º 6
0
  /** Extract a list of values from a set of records given some fields */
  private static <R extends Record> List<Object> extractValues(
      Collection<? extends R> records, TableField<R, ?> field2) {
    List<Object> result = new ArrayList<Object>();

    for (R record : records) {
      result.add(record.getValue(field2));
    }

    return result;
  }
Exemplo n.º 7
0
  @Override
  public final List<?> getValues(int fieldIndex) {
    List<Object> result = new ArrayList<Object>(size());

    for (R record : this) {
      result.add(record.getValue(fieldIndex));
    }

    return result;
  }
Exemplo n.º 8
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K, E> Map<K, E> intoMap(Field<K> key, Class<? extends E> type) {
    int index = getIndex(key);
    Map<K, E> map = new LinkedHashMap<K, E>();

    for (R record : this) {
      if (map.put((K) record.getValue(index), record.into(type)) != null) {
        throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
      }
    }

    return map;
  }
Exemplo n.º 9
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K, E> Map<K, E> intoMap(Field<K> key, Class<? extends E> type) {
    RecordMapper<R, E> mapper = new ReflectionMapper<R, E>(fields.fields, type);
    int index = fieldsRow().indexOf(key);
    Map<K, E> map = new LinkedHashMap<K, E>();

    for (R record : this) {
      if (map.put((K) record.getValue(index), mapper.map(record)) != null) {
        throw new InvalidResultException("Key " + key + " is not unique in Result for " + this);
      }
    }

    return map;
  }
Exemplo n.º 10
0
  /** Extract a list of row value expressions from a set of records given some fields */
  private static <R extends Record> List<RowN> extractRows(
      Collection<? extends R> records, TableField<R, ?>[] fields) {
    List<RowN> rows = new ArrayList<RowN>();

    for (R record : records) {
      Object[] values = new Object[fields.length];

      for (int i = 0; i < fields.length; i++) {
        values[i] = record.getValue(fields[i]);
      }

      rows.add(row(values));
    }

    return rows;
  }
Exemplo n.º 11
0
    @Override
    public final E map(R record) {
      int size = record.size();
      Class<?> componentType = type.getComponentType();
      Object[] result =
          (Object[]) (instance != null ? instance : Array.newInstance(componentType, size));

      // Just as in Collection.toArray(Object[]), return a new array in case
      // sizes don't match
      if (size > result.length) {
        result = (Object[]) Array.newInstance(componentType, size);
      }

      for (int i = 0; i < size; i++) {
        result[i] = Convert.convert(record.getValue(i), componentType);
      }

      return (E) result;
    }
Exemplo n.º 12
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K> Map<K, Result<R>> intoGroups(Field<K> key) {
    int index = fieldsRow().indexOf(key);
    Map<K, Result<R>> map = new LinkedHashMap<K, Result<R>>();

    for (R record : this) {
      K val = (K) record.getValue(index);
      Result<R> result = map.get(val);

      if (result == null) {
        result = new ResultImpl<R>(configuration, fields);
        map.put(val, result);
      }

      result.add(record);
    }

    return map;
  }
Exemplo n.º 13
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K, E> Map<K, List<E>> intoGroups(Field<K> key, Class<? extends E> type) {
    int index = getIndex(key);
    Map<K, List<E>> map = new LinkedHashMap<K, List<E>>();

    for (R record : this) {
      K keyVal = (K) record.getValue(index);

      List<E> list = map.get(keyVal);
      if (list == null) {
        list = new ArrayList<E>();
        map.put(keyVal, list);
      }

      list.add(record.into(type));
    }

    return map;
  }
Exemplo n.º 14
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K, E> Map<K, List<E>> intoGroups(Field<K> key, Class<? extends E> type) {
    RecordMapper<R, E> mapper = new ReflectionMapper<R, E>(fields.fields, type);
    int index = fieldsRow().indexOf(key);
    Map<K, List<E>> map = new LinkedHashMap<K, List<E>>();

    for (R record : this) {
      K keyVal = (K) record.getValue(index);

      List<E> list = map.get(keyVal);
      if (list == null) {
        list = new ArrayList<E>();
        map.put(keyVal, list);
      }

      list.add(mapper.map(record));
    }

    return map;
  }
Exemplo n.º 15
0
  @Override
  public final <E> Map<List<?>, E> intoMap(Field<?>[] keys, Class<? extends E> type) {
    if (keys == null) {
      keys = new Field[0];
    }

    Map<List<?>, E> map = new LinkedHashMap<List<?>, E>();

    for (R record : this) {
      List<Object> keyValueList = new ArrayList<Object>();
      for (Field<?> key : keys) {
        keyValueList.add(record.getValue(key));
      }

      if (map.put(keyValueList, record.into(type)) != null) {
        throw new InvalidResultException(
            "Key list " + keyValueList + " is not unique in Result for " + this);
      }
    }

    return map;
  }
Exemplo n.º 16
0
  @SuppressWarnings("unchecked")
  @Override
  public final <K, V> Map<K, List<V>> intoGroups(Field<K> key, Field<V> value) {
    int kIndex = fieldsRow().indexOf(key);
    int vIndex = fieldsRow().indexOf(value);

    Map<K, List<V>> map = new LinkedHashMap<K, List<V>>();

    for (R record : this) {
      K k = (K) record.getValue(kIndex);
      V v = (V) record.getValue(vIndex);
      List<V> result = map.get(k);

      if (result == null) {
        result = new ArrayList<V>();
        map.put(k, result);
      }

      result.add(v);
    }

    return map;
  }
Exemplo n.º 17
0
 @Override
 public final Object fetchOne(String fieldName) {
   R record = fetchOne();
   return record == null ? null : record.getValue(fieldName);
 }
Exemplo n.º 18
0
 @Override
 public final Object fetchOne(int fieldIndex) {
   R record = fetchOne();
   return record == null ? null : record.getValue(fieldIndex);
 }
Exemplo n.º 19
0
 @Override
 public final <T> T fetchOne(Field<T> field) {
   R record = fetchOne();
   return record == null ? null : record.getValue(field);
 }
Exemplo n.º 20
0
 final <T> void addValue(R record, Field<T> field) {
   addValue(field, record.getValue(field));
 }
Exemplo n.º 21
0
 @SuppressWarnings("unchecked")
 @Override
 public int compare(R record1, R record2) {
   return comparator.compare((T) record1.getValue(fieldIndex), (T) record2.getValue(fieldIndex));
 }