Package com.ajjpj.amapper.core2.impl

Source Code of com.ajjpj.amapper.core2.impl.IdentityCache

package com.ajjpj.amapper.core2.impl;

import com.ajjpj.amapper.core2.exclog.AMapperException;
import com.ajjpj.amapper.core2.path.APath;
import com.ajjpj.amapper.util.coll.AOption;

import java.util.IdentityHashMap;


/**
* @author arno
*/
class IdentityCache {
    private final IdentityHashMap<Object, AOption<Object>> map = new IdentityHashMap<Object, AOption<Object>>();

    public void register(Object source, Object target, APath path) {
        if(map.containsKey(source)) {
            final Object prev = map.get(source);
            if(prev == target) {
                throw new AMapperException("duplicate registration of same target " + target + " for " + source, path);
            }
            else {
                throw new  AMapperException("duplicate registration of different target " + target + " and " + prev + " for " + source, path);
            }
        }
        else {
            map.put(source, AOption.some(target));
        }
    }

    public AOption<Object> get(Object source) {
        final AOption<Object> result = map.get(source);
        if(result != null) {
            return result;
        }
        else {
            return AOption.none();
        }
    }
}
TOP

Related Classes of com.ajjpj.amapper.core2.impl.IdentityCache

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.