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

io.permazen.AbstractEnumPermazenField 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.TypeToken;

import io.permazen.encoding.Encoding;
import io.permazen.schema.AbstractEnumSchemaField;

import java.lang.reflect.Method;

import org.dellroad.stuff.java.EnumUtil;

/**
 * Support superclass for {@link PermazenSimpleField}'s that involve an {@link Enum} type.
 *
 * @param  the field's Java type
 * @param  the field's core API encoding type
 */
abstract class AbstractEnumPermazenField extends ConvertedSimplePermazenField {

    final Class> enumType;

    AbstractEnumPermazenField(String name, int storageId, TypeToken typeToken, Encoding encoding,
      Class> enumType, boolean indexed, io.permazen.annotation.PermazenField annotation,
      String description, Method getter, Method setter, Converter converter) {
        super(name, storageId, typeToken, encoding, indexed, annotation, description, getter, setter, converter);
        Preconditions.checkArgument(enumType != null, "null enumType");
        this.enumType = enumType;
    }

// Public Methods

    /**
     * Get the {@link Enum} type that this field uses.
     *
     * @return this field's {@link Enum} type
     */
    public Class> getEnumType() {
        return this.enumType;
    }

// Package Methods

    @Override
    AbstractEnumSchemaField toSchemaItem() {
        final AbstractEnumSchemaField schemaField = (AbstractEnumSchemaField)super.toSchemaItem();
        EnumUtil.getValues(this.enumType).forEach(value -> schemaField.getIdentifiers().add(value.name()));
        return schemaField;
    }

    @Override
    abstract AbstractEnumSchemaField createSchemaItem();
}