Exemple #1
0
  @Override
  public final <E> Map<Record, List<E>> intoGroups(Field<?>[] keys, Class<? extends E> type) {
    RecordMapper<R, E> mapper = new ReflectionMapper<R, E>(fields.fields, type);

    if (keys == null) {
      keys = new Field[0];
    }

    Map<Record, List<E>> map = new LinkedHashMap<Record, List<E>>();
    for (R record : this) {

      @SuppressWarnings({"rawtypes", "unchecked"})
      Record key = new RecordImpl(keys);

      for (Field<?> field : keys) {
        Utils.setValue(key, field, record, field);
      }

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

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

    return map;
  }
    @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);
      }
    }
Exemple #3
0
  @Override
  public final <E> List<E> into(Class<? extends E> type) {
    List<E> list = new ArrayList<E>(size());
    RecordMapper<R, E> mapper = new ReflectionMapper<R, E>(fields.fields, type);

    for (R record : this) {
      list.add(mapper.map(record));
    }

    return list;
  }
Exemple #4
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;
  }
    @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);
    }
Exemple #6
0
  @Override
  public final <E> List<E> map(RecordMapper<? super R, E> mapper) {
    List<E> result = new ArrayList<E>();

    for (R record : this) {
      result.add(mapper.map(record));
    }

    return result;
  }
Exemple #7
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;
  }
Exemple #8
0
  @Override
  public final <E> Map<List<?>, E> intoMap(Field<?>[] keys, Class<? extends E> type) {
    RecordMapper<R, E> mapper = new ReflectionMapper<R, E>(fields.fields, 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, mapper.map(record)) != null) {
        throw new InvalidResultException(
            "Key list " + keyValueList + " is not unique in Result for " + this);
      }
    }

    return map;
  }
  @Override
  public final E map(R record) {
    if (record == null) {
      return null;
    }

    try {
      return attach(delegate.map(record), record);
    }

    // Pass MappingExceptions on to client code
    catch (MappingException e) {
      throw e;
    }

    // All other reflection exceptions are intercepted
    catch (Exception e) {
      throw new MappingException("An error ocurred when mapping record to " + type, e);
    }
  }
Exemple #10
0
 @Override
 public final <E> E map(RecordMapper<Record, E> mapper) {
   return mapper.map(this);
 }
 @Override
 public final <E> E fetchOne(RecordMapper<? super R, E> mapper) {
   return mapper.map(fetchOne());
 }