use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class JacksonMapper method skipChoice.
/**
* Handles skipping choice groups.
*
* @param propertyName the start property name
* @param obj the object to inspect
* @param reporter the reporter
* @return a pair of property name and value to use
*/
private Pair<String, Object> skipChoice(String propertyName, Object obj, IOReporter reporter) {
if (obj instanceof Group) {
Group group = (Group) obj;
// For choices search for the (only!) child and skip the choice.
if (group.getDefinition() instanceof GroupPropertyDefinition) {
if (((GroupPropertyDefinition) group.getDefinition()).getConstraint(ChoiceFlag.class).isEnabled()) {
Iterator<QName> childPropertyNames = group.getPropertyNames().iterator();
if (!childPropertyNames.hasNext()) {
reporter.warn(new IOMessageImpl("Found an empty choice.", null));
return null;
}
QName childPropertyName = childPropertyNames.next();
Object[] values = group.getProperty(childPropertyName);
Object value = values[0];
if (childPropertyNames.hasNext() || values.length > 1)
reporter.warn(new IOMessageImpl("Found a choice with multiple children. Using first.", null));
// delegate to only value
return skipChoice(childPropertyName.getLocalPart(), value, reporter);
}
}
}
return new Pair<>(propertyName, obj);
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class ShapeInstanceReader method getMostCompatibleShapeType.
/**
* Determine the type out of the the mapping relevant types in the given
* type index, that matches the given data type best.
*
* @param types the type index
* @param dataType the Shapefile data type
* @param preferredName the name of the preferred type
* @return the most compatible type found together with is compatibility
* rating or <code>null</code> if there is no type that at least has
* one matching property
*
* @see #checkCompatibility(TypeDefinition, TypeDefinition)
*/
public static Pair<TypeDefinition, Integer> getMostCompatibleShapeType(TypeIndex types, TypeDefinition dataType, String preferredName) {
int maxCompatibility = -1;
TypeDefinition maxType = null;
// check preferred name first
TypeDefinition preferredType = types.getType(new QName(ShapefileConstants.SHAPEFILE_NS, preferredName));
if (preferredType != null) {
int comp = checkCompatibility(preferredType, dataType);
if (comp >= 100) {
// return an exact match directly
return new Pair<TypeDefinition, Integer>(preferredType, 100);
} else {
maxType = preferredType;
maxCompatibility = comp;
}
}
for (TypeDefinition schemaType : types.getMappingRelevantTypes()) {
if (ShapefileConstants.SHAPEFILE_NS.equals(schemaType.getName().getNamespaceURI())) {
// is a shapefile type
int comp = checkCompatibility(schemaType, dataType);
if (comp >= 100) {
// return an exact match directly
return new Pair<TypeDefinition, Integer>(schemaType, 100);
} else if (comp > maxCompatibility) {
maxType = schemaType;
maxCompatibility = comp;
} else if (maxCompatibility > 0 && comp == maxCompatibility) {
// TODO debug message? possible duplicate?
}
}
}
if (maxType != null && maxCompatibility > 0) {
// return the type with the maximum compatibility rating
return new Pair<TypeDefinition, Integer>(maxType, maxCompatibility);
}
return null;
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class TypeMetaPairContentProvider method getElements.
/**
* @see TypeIndexContentProvider#getElements(Object)
*/
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof Pair<?, ?>) {
Pair<?, ?> pair = (Pair<?, ?>) inputElement;
Object type = pair.getFirst();
if (type instanceof TypeDefinition) {
type = new TypeEntityDefinition((TypeDefinition) type, getSchemaSpace(), null);
}
// second item will be a set of metadata keys
return new Object[] { type, pair.getSecond() };
} else
return new Object[0];
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class InstanceContentProvider method hasChildren.
/**
* @see ITreeContentProvider#hasChildren(Object)
*/
@Override
public boolean hasChildren(Object element) {
if (element instanceof Pair<?, ?>) {
Pair<?, ?> pair = (Pair<?, ?>) element;
if (pair.getFirst() == Metadata.METADATA && pair.getSecond() instanceof Instance) {
return true;
}
element = pair.getSecond();
}
if (element instanceof Group) {
Group group = (Group) element;
return !Iterables.isEmpty(group.getPropertyNames());
}
return false;
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class InstanceContentProvider method getChildren.
/**
* @see ITreeContentProvider#getChildren(Object)
*/
@Override
public Object[] getChildren(Object parentElement) {
boolean isMetaData = false;
if (parentElement instanceof Pair<?, ?>) {
Pair<?, ?> pair = (Pair<?, ?>) parentElement;
parentElement = pair.getSecond();
if (pair.getFirst() == Metadata.METADATA && pair.getSecond() instanceof Instance) {
isMetaData = true;
}
}
if (parentElement instanceof Group && isMetaData == false) {
Group group = (Group) parentElement;
List<Object> children = new ArrayList<Object>();
for (QName name : group.getPropertyNames()) {
Definition<?> childDef = group.getDefinition().getChild(name);
for (Object value : group.getProperty(name)) {
children.add(new Pair<Object, Object>(childDef, value));
}
}
return children.toArray();
}
if (parentElement instanceof Group && isMetaData == true) {
Instance inst = (Instance) parentElement;
List<Object> metachildren = new ArrayList<Object>();
for (String key : inst.getMetaDataNames()) {
List<Object> values = inst.getMetaData(key);
for (Object value : values) {
metachildren.add(new Pair<String, Object>(key, value));
}
}
return metachildren.toArray();
}
return new Object[0];
}
Aggregations