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

io.permazen.PermazenListField Maven / Gradle / Ivy

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.ListFieldAdd;
import io.permazen.change.ListFieldClear;
import io.permazen.change.ListFieldRemove;
import io.permazen.change.ListFieldReplace;
import io.permazen.core.ListField;
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 PermazenClass}.
 */
public class PermazenListField extends PermazenCollectionField {

// Constructor

    PermazenListField(String name, int storageId, io.permazen.annotation.PermazenListField annotation,
      PermazenSimpleField elementField, String description, Method getter) {
        super(name, storageId, annotation, elementField, description, getter);
    }

// Public Methods

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

    @Override
    public List getValue(PermazenObject pobj) {
        Preconditions.checkArgument(pobj != null, "null pobj");
        return pobj.getPermazenTransaction().readListField(pobj.getObjId(), this.name, false);
    }

    @Override
    public  R visit(PermazenFieldSwitch target) {
        Preconditions.checkArgument(target != null, "null target");
        return target.casePermazenListField(this);
    }

    @Override
    public ListField getSchemaItem() {
        return (ListField)super.getSchemaItem();
    }

// Package Methods

    @Override
    ListSchemaField createSchemaItem() {
        return new ListSchemaField();
    }

    @Override
    @SuppressWarnings("unchecked")
    Iterable iterateReferences(Transaction tx, ObjId id, PermazenReferenceField subField) {
        return (Iterable)tx.readListField(id, this.name, false);
    }

    @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(PermazenTransaction ptx) {
        final Converter elementConverter = this.elementField.getConverter(ptx);
        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.name, true);
    }

// Bytecode generation

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