io.permazen.PermazenMapField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of permazen-main Show documentation
Show all versions of permazen-main Show documentation
Permazen classes that map Java model classes onto the core API.
The newest version!
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package io.permazen;
import com.google.common.base.Converter;
import com.google.common.base.Preconditions;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import io.permazen.change.MapFieldAdd;
import io.permazen.change.MapFieldClear;
import io.permazen.change.MapFieldRemove;
import io.permazen.change.MapFieldReplace;
import io.permazen.core.MapField;
import io.permazen.core.ObjId;
import io.permazen.core.Transaction;
import io.permazen.schema.MapSchemaField;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
* Represents a map field in a {@link PermazenClass}.
*/
public class PermazenMapField extends PermazenComplexField {
final PermazenSimpleField keyField;
final PermazenSimpleField valueField;
// Constructor
PermazenMapField(String name, int storageId, io.permazen.annotation.PermazenMapField annotation,
PermazenSimpleField keyField, PermazenSimpleField valueField, String description, Method getter) {
super(name, storageId, annotation, description, getter);
Preconditions.checkArgument(keyField != null, "null keyField");
Preconditions.checkArgument(valueField != null, "null valueField");
this.keyField = keyField;
this.valueField = valueField;
}
// Public Methods
@Override
public io.permazen.annotation.PermazenMapField getDeclaringAnnotation() {
return (io.permazen.annotation.PermazenMapField)super.getDeclaringAnnotation();
}
/**
* Get the key sub-field.
*
* @return this field's key sub-field
*/
public PermazenSimpleField getKeyField() {
return this.keyField;
}
/**
* Get the value sub-field.
*
* @return this field's value sub-field
*/
public PermazenSimpleField getValueField() {
return this.valueField;
}
@Override
public NavigableMap, ?> getValue(PermazenObject pobj) {
Preconditions.checkArgument(pobj != null, "null pobj");
return pobj.getPermazenTransaction().readMapField(pobj.getObjId(), this.name, false);
}
@Override
public R visit(PermazenFieldSwitch target) {
Preconditions.checkArgument(target != null, "null target");
return target.casePermazenMapField(this);
}
@Override
public List getSubFields() {
return Arrays.asList(this.keyField, this.valueField);
}
@Override
public MapField, ?> getSchemaItem() {
return (MapField, ?>)super.getSchemaItem();
}
// Package Methods
@Override
MapSchemaField createSchemaItem() {
return new MapSchemaField();
}
@Override
MapSchemaField toSchemaItem() {
final MapSchemaField schemaField = (MapSchemaField)super.toSchemaItem();
schemaField.setKeyField(this.keyField.toSchemaItem());
schemaField.setValueField(this.valueField.toSchemaItem());
return schemaField;
}
@Override
@SuppressWarnings("unchecked")
Iterable iterateReferences(Transaction tx, ObjId id, PermazenReferenceField subField) {
final NavigableMap, ?> map = tx.readMapField(id, this.name, false);
if (subField == this.keyField)
return (Iterable)map.keySet();
if (subField == this.valueField)
return (Iterable)map.values();
throw new RuntimeException("internal error");
}
@Override
public TypeToken> getTypeToken() {
return this.buildTypeToken(this.keyField.getTypeToken().wrap(), this.valueField.getTypeToken().wrap());
}
// This method exists solely to bind the generic type parameters
@SuppressWarnings("serial")
private TypeToken> buildTypeToken(TypeToken keyType, TypeToken valueType) {
return new TypeToken>() { }
.where(new TypeParameter() { }, keyType)
.where(new TypeParameter() { }, valueType);
}
@Override
void addChangeParameterTypes(List> types, Class targetType) {
this.addChangeParameterTypes(types, targetType, this.keyField.getTypeToken(), this.valueField.getTypeToken());
}
// This method exists solely to bind the generic type parameters
@SuppressWarnings("serial")
private void addChangeParameterTypes(List> types,
Class targetType, TypeToken keyType, TypeToken valueType) {
types.add(new TypeToken>() { }
.where(new TypeParameter() { }, targetType)
.where(new TypeParameter() { }, keyType.wrap())
.where(new TypeParameter() { }, valueType.wrap()));
types.add(new TypeToken>() { }
.where(new TypeParameter() { }, targetType));
types.add(new TypeToken>() { }
.where(new TypeParameter() { }, targetType)
.where(new TypeParameter() { }, keyType.wrap())
.where(new TypeParameter() { }, valueType.wrap()));
types.add(new TypeToken>() { }
.where(new TypeParameter() { }, targetType)
.where(new TypeParameter() { }, keyType.wrap())
.where(new TypeParameter() { }, valueType.wrap()));
}
@Override
public NavigableMapConverter, ?, ?, ?> getConverter(PermazenTransaction ptx) {
Converter, ?> keyConverter = this.keyField.getConverter(ptx);
Converter, ?> valueConverter = this.valueField.getConverter(ptx);
if (keyConverter == null && valueConverter == null)
return null;
if (keyConverter == null)
keyConverter = Converter.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy