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

org.randombits.confluence.metadata.impl.handler.MapHandler Maven / Gradle / Ivy

There is a newer version: 7.4.1
Show newest version
package org.randombits.confluence.metadata.impl.handler;

import org.randombits.confluence.metadata.MetadataManager;
import org.randombits.confluence.metadata.TypeConversionException;
import org.randombits.confluence.metadata.TypeHandler;

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

/**
 * Handles converting objects stored inside Maps to and from 'storable' instances.
 */
public class MapHandler implements TypeHandler {

    private final MetadataManager metadataManager;

    public MapHandler(MetadataManager metadataManager) {
        this.metadataManager = metadataManager;
    }

    public boolean supportsOriginal( Object original ) {
        return original instanceof Map;
    }

    public boolean supportsStorable( Object stored ) {
        return stored instanceof Map;
    }

    public Object getOriginal( Object stored ) throws TypeConversionException {
        Map storedMap = (Map) stored;
        Map originalMap = new HashMap();
        for ( Map.Entry entry : storedMap.entrySet() ) {
            originalMap.put( metadataManager.fromStorable( entry.getKey() ), metadataManager.fromStorable( entry.getValue() ) );
        }

        return originalMap;
    }

    public Object getStorable( Object original ) throws TypeConversionException {

        Map originalMap = (Map) original;
        Map storableMap = new HashMap();
        for ( Map.Entry entry : originalMap.entrySet() ) {
            storableMap.put( metadataManager.toStorable( entry.getKey() ), metadataManager.toStorable( entry.getValue() ) );
        }

        return storableMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy