Created
April 3, 2018 15:59
-
-
Save desiderantes/1433fb8d52a3a2389eeaaf653251ef93 to your computer and use it in GitHub Desktop.
Array Converter from existing Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.desiderantes.jooq; | |
import org.jooq.Converter; | |
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ArrayConverter<T,U> implements Converter<T[], U[]> { | |
private Converter<T,U> converter; | |
public ArrayConverter(Converter<T, U> converter) { | |
this.converter = converter; | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public U[] from(T[] databaseObject) { | |
List<U> list = new ArrayList<>(); | |
for (T type : databaseObject) { | |
U from = converter.from(type); | |
list.add(from); | |
} | |
return (U[]) list.toArray(); | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public T[] to(U[] userObject) { | |
List<T> list = new ArrayList<>(); | |
for (U type : userObject) { | |
T to = converter.to(type); | |
list.add(to); | |
} | |
return (T[]) list.toArray(); | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public Class<T[]> fromType() { | |
return (Class<T[]>) Array.newInstance(converter.fromType(), 0).getClass(); | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public Class<U[]> toType() { | |
return (Class<U[]>) Array.newInstance(converter.toType(), 0).getClass(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment