Last active
January 29, 2017 11:51
-
-
Save shymek/e784c964704eca007ab6d6966238df70 to your computer and use it in GitHub Desktop.
String List converter for requery.
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
import android.support.annotation.Nullable; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import io.requery.Converter; | |
/** | |
* @author Jakub Szymion | |
* @since 01.06.2016 | |
*/ | |
public class StringListConverter implements Converter<List<String>, String> { | |
private static final String SEPARATOR = "\0007"; | |
@Override | |
public Class<List<String>> mappedType() { | |
//noinspection unchecked | |
return (Class) List.class; | |
} | |
@Override | |
public Class<String> persistedType() { | |
return String.class; | |
} | |
@Nullable | |
@Override | |
public Integer persistedSize() { | |
return null; | |
} | |
@Override | |
public String convertToPersisted(List<String> list) { | |
if (list == null) { | |
return ""; | |
} | |
StringBuilder sb = new StringBuilder(); | |
int size = list.size(); | |
int index = 0; | |
for (String s : list) { | |
++index; | |
sb.append(s); | |
if (index < size) { | |
sb.append(SEPARATOR); | |
} | |
} | |
return sb.toString(); | |
} | |
@Override | |
public List<String> convertToMapped(Class<? extends List<String>> type, String value) { | |
if (value == null) { | |
return Collections.emptyList(); | |
} | |
return Arrays.asList(value.split(SEPARATOR)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment