All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.permazen.JListField Maven / Gradle / Ivy

There is a newer version: 5.1.0
Show 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.ListFieldAdd;
import io.permazen.change.ListFieldClear;
import io.permazen.change.ListFieldRemove;
import io.permazen.change.ListFieldReplace;
import io.permazen.core.ObjId;
import io.permazen.core.Transaction;
import io.permazen.schema.ListSchemaField;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents a list field in a {@link JClass}.
 */
public class JListField extends JCollectionField {

    JListField(Permazen jdb, String name, int storageId,
      io.permazen.annotation.JListField annotation, JSimpleField elementField, String description, Method getter) {
        super(jdb, name, storageId, annotation, elementField, description, getter);
    }

    @Override
    public io.permazen.annotation.JListField getDeclaringAnnotation() {
        return (io.permazen.annotation.JListField)super.getDeclaringAnnotation();
    }

    @Override
    public List getValue(JObject jobj) {
        Preconditions.checkArgument(jobj != null, "null jobj");
        return jobj.getTransaction().readListField(jobj.getObjId(), this.storageId, false);
    }

    @Override
    public  R visit(JFieldSwitch target) {
        return target.caseJListField(this);
    }

    @Override
    ListSchemaField toSchemaItem(Permazen jdb) {
        final ListSchemaField schemaField = new ListSchemaField();
        super.initialize(jdb, schemaField);
        return schemaField;
    }

    @Override
    ListElementIndexInfo toIndexInfo(JSimpleField subField) {
        assert subField == this.elementField;
        return new ListElementIndexInfo(this);
    }

    @Override
    @SuppressWarnings("serial")
     TypeToken> buildTypeToken(TypeToken elementType) {
        return new TypeToken>() { }.where(new TypeParameter() { }, elementType);
    }

    // This method exists solely to bind the generic type parameters
    @Override
    @SuppressWarnings("serial")
     void addChangeParameterTypes(List> types, Class targetType, TypeToken elementType) {
        types.add(new TypeToken>() { }
          .where(new TypeParameter() { }, targetType)
          .where(new TypeParameter() { }, elementType.wrap()));
        types.add(new TypeToken>() { }
          .where(new TypeParameter() { }, targetType));
        types.add(new TypeToken>() { }
          .where(new TypeParameter() { }, targetType)
          .where(new TypeParameter() { }, elementType.wrap()));
        types.add(new TypeToken>() { }
          .where(new TypeParameter() { }, targetType)
          .where(new TypeParameter() { }, elementType.wrap()));
    }

    @Override
    public ListConverter getConverter(JTransaction jtx) {
        final Converter elementConverter = this.elementField.getConverter(jtx);
        return elementConverter != null ? this.createConverter(elementConverter) : null;
    }

    // This method exists solely to bind the generic type parameters
    private  ListConverter createConverter(Converter elementConverter) {
        return new ListConverter<>(elementConverter);
    }

// POJO import/export

    @Override
    List createPojoCollection(Class collectionType) {
        return new ArrayList<>();
    }

    @Override
    List readCoreCollection(Transaction tx, ObjId id) {
        return tx.readListField(id, this.storageId, true);
    }

// Bytecode generation

    @Override
    Method getFieldReaderMethod() {
        return ClassGenerator.JTRANSACTION_READ_LIST_FIELD_METHOD;
    }
}