
com.spikeify.converters.SetConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Simple ORM for Aerospike
package com.spikeify.converters;
import com.spikeify.Converter;
import com.spikeify.NoArgClassConstructor;
import com.spikeify.SpikeifyError;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@SuppressWarnings("unchecked")
public class SetConverter implements Converter, List> {
private final Class extends Set> setType;
private final Constructor extends Set> noArgConstructor;
public SetConverter(Class extends Set> setType) {
if (setType.equals(Set.class)) {
setType = HashSet.class;
}
this.setType = setType;
this.noArgConstructor = NoArgClassConstructor.getNoArgConstructor(setType);
}
public boolean canConvert(Class type) {
return List.class.isAssignableFrom(type);
}
public Set fromProperty(List list) {
if (list == null) {
return null;
}
Set set;
try {
set = noArgConstructor.newInstance();
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
throw new SpikeifyError("Error: could not instantiate '" + setType + "' via a default no-arg constructor.");
}
set.addAll(list);
return set;
}
public List fromField(Set fieldValue) {
return new ArrayList(fieldValue);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy