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

com.github.nagyesta.lowkeyvault.mapper.common.BaseEntityConverterRegistry Maven / Gradle / Ivy

There is a newer version: 2.5.81
Show newest version
package com.github.nagyesta.lowkeyvault.mapper.common;

import com.github.nagyesta.lowkeyvault.model.v7_2.BasePropertiesModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.common.BaseBackupListItem;
import com.github.nagyesta.lowkeyvault.model.v7_2.common.BaseBackupModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.BackupListContainer;
import com.github.nagyesta.lowkeyvault.service.EntityId;
import com.github.nagyesta.lowkeyvault.service.common.BaseVaultEntity;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseEntityConverterRegistry, M, DM extends M, PM extends BasePropertiesModel, IM, DIM extends IM,
        BLI extends BaseBackupListItem, BL extends BackupListContainer,
        B extends BaseBackupModel>
        implements EntityConverterRegistry {

    private final Map> modelConverters = new HashMap<>();
    private final Map> propertiesConverters = new HashMap<>();
    private final Map> itemConverters = new HashMap<>();
    private final Map> versionedItemConverters = new HashMap<>();
    private final Map> backupConverters = new HashMap<>();

    @Override
    public RecoveryAwareConverter modelConverter(final String apiVersion) {
        return getNonNullConverterForApiVersion(modelConverters, apiVersion);
    }

    @Override
    public void registerModelConverter(final RecoveryAwareConverter converter) {
        converter.supportedVersions().forEach(v -> modelConverters.put(v, converter));
    }

    @Override
    public AliasAwareConverter propertiesConverter(final String apiVersion) {
        return getNonNullConverterForApiVersion(propertiesConverters, apiVersion);
    }

    @Override
    public void registerPropertiesConverter(final AliasAwareConverter converter) {
        converter.supportedVersions().forEach(v -> propertiesConverters.put(v, converter));
    }

    @Override
    public RecoveryAwareConverter itemConverter(final String apiVersion) {
        return getNonNullConverterForApiVersion(itemConverters, apiVersion);
    }

    @Override
    public void registerItemConverter(final RecoveryAwareConverter converter) {
        converter.supportedVersions().forEach(v -> itemConverters.put(v, converter));
    }

    @Override
    public RecoveryAwareConverter versionedItemConverter(final String apiVersion) {
        return getNonNullConverterForApiVersion(versionedItemConverters, apiVersion);
    }

    @Override
    public void registerVersionedItemConverter(final RecoveryAwareConverter converter) {
        converter.supportedVersions().forEach(v -> versionedItemConverters.put(v, converter));
    }

    @Override
    public BackupConverter backupConverter(final String apiVersion) {
        return getNonNullConverterForApiVersion(backupConverters, apiVersion);
    }

    @Override
    public void registerBackupConverter(final BackupConverter converter) {
        converter.supportedVersions().forEach(v -> backupConverters.put(v, converter));
    }

    private  CV getNonNullConverterForApiVersion(final Map map, final String apiVersion) {
        if (!map.containsKey(apiVersion)) {
            throw new IllegalStateException("Unable to return converter for API version: "
                    + apiVersion + " available versions were: " + map.keySet());
        }
        return map.get(apiVersion);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy