
io.permazen.core.CollectionField Maven / Gradle / Ivy
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package io.permazen.core;
import com.google.common.base.Preconditions;
import com.google.common.reflect.TypeToken;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
/**
* Superclass of fields with types assignable to {@link Collection}.
*
* @param Java collection type
* @param Java collection element type
*/
public abstract class CollectionField, E> extends ComplexField {
public static final String ELEMENT_FIELD_NAME = "element";
final SimpleField elementField;
/**
* Constructor.
*
* @param name the name of the field
* @param storageId field content storage ID
* @param typeToken Java type for the field's values
* @param schema schema version
* @param elementField this field's element sub-field
* @throws IllegalArgumentException if any parameter is null
* @throws IllegalArgumentException if {@code storageId} is non-positive
*/
CollectionField(String name, int storageId, Schema schema, TypeToken typeToken, SimpleField elementField) {
super(name, storageId, schema, typeToken);
Preconditions.checkArgument(elementField != null, "null elementField");
this.elementField = elementField;
assert this.elementField.parent == null;
this.elementField.parent = this;
}
// Public methods
/**
* Get the element field.
*
* @return collection element field
*/
public SimpleField getElementField() {
return this.elementField;
}
@Override
public final List> getSubFields() {
return Collections.>singletonList(this.elementField);
}
@Override
public boolean hasDefaultValue(Transaction tx, ObjId id) {
return this.getValue(tx, id).isEmpty();
}
@Override
public abstract C getValue(Transaction tx, ObjId id);
// Non-public methods
@Override
@SuppressWarnings("unchecked")
Iterable iterateSubField(Transaction tx, ObjId id, SimpleField subField) {
if (subField == this.elementField)
return (Iterable)this.getValue(tx, id);
throw new IllegalArgumentException("unknown sub-field");
}
@Override
void unreferenceRemovedTypes(Transaction tx, ObjId id, ReferenceField subField, SortedSet removedStorageIds) {
assert subField == this.elementField;
for (Iterator> i = this.getValueInternal(tx, id).iterator(); i.hasNext(); ) {
final ObjId ref = (ObjId)i.next();
if (ref != null && removedStorageIds.contains(ref.getStorageId()))
i.remove();
}
}
@Override
boolean isUpgradeCompatible(Field> field) {
if (field.getClass() != this.getClass())
return false;
final CollectionField, ?> that = (CollectionField, ?>)field;
return this.elementField.isUpgradeCompatible(that.elementField);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy