MapSchemaParserImpl.java

package space.sunqian.common.object.data;

import space.sunqian.annotations.Nonnull;
import space.sunqian.common.reflect.ReflectionException;
import space.sunqian.common.reflect.TypeKit;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

enum MapSchemaParserImpl implements MapSchemaParser {

    INST;

    @Override
    public @Nonnull MapSchema parse(@Nonnull Type type) throws DataObjectException {
        try {
            return new MapSchemaImpl(type);
        } catch (Exception e) {
            throw new DataObjectException(e);
        }
    }

    @Override
    public @Nonnull MapSchema parse(@Nonnull Type type, @Nonnull Type keyType, @Nonnull Type valueType) {
        return new MapSchemaImpl(type, keyType, valueType);
    }

    private final class MapSchemaImpl implements MapSchema {

        private final @Nonnull Type type;
        private final @Nonnull Type keyType;
        private final @Nonnull Type valueType;

        private MapSchemaImpl(@Nonnull Type type) throws ReflectionException {
            this.type = type;
            List<Type> actualTypes = TypeKit.resolveActualTypeArguments(type, Map.class);
            this.keyType = actualTypes.get(0);
            this.valueType = actualTypes.get(1);
        }

        private MapSchemaImpl(@Nonnull Type type, @Nonnull Type keyType, @Nonnull Type valueType) {
            this.type = type;
            this.keyType = keyType;
            this.valueType = valueType;
        }

        @Override
        public @Nonnull Type type() {
            return type;
        }

        @Override
        public @Nonnull MapSchemaParser parser() {
            return MapSchemaParserImpl.this;
        }

        @Override
        public @Nonnull Type keyType() {
            return keyType;
        }

        @Override
        public @Nonnull Type valueType() {
            return valueType;
        }

        @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
        @Override
        public boolean equals(Object o) {
            return DataObjectKit.equals(this, o);
        }

        @Override
        public int hashCode() {
            return DataObjectKit.hashCode(this);
        }

        @Override
        public @Nonnull String toString() {
            return DataObjectKit.toString(this);
        }
    }
}