ComponentInheritanceValueMap.java 2.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.commons.inherit;

import com.day.cq.commons.ValueMapWrapper;
import com.day.cq.commons.inherit.InheritanceValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

public class ComponentInheritanceValueMap
extends ValueMapWrapper
implements InheritanceValueMap {
    protected Resource resource;

    public ComponentInheritanceValueMap(Resource resource) {
        super(ResourceUtil.getValueMap((Resource)resource));
        this.resource = resource;
    }

    public ComponentInheritanceValueMap(ValueMap map) {
        super(map);
        this.resource = null;
    }

    @Override
    public <T> T get(String name, Class<T> type) {
        if (type == null) {
            return (T)this.get(name);
        }
        return (T)super.get(name, type);
    }

    @Override
    public <T> T getInherited(String name, Class<T> type) {
        T value = this.get(name, type);
        if (value == null) {
            value = this.getParentComponentValue(name, type);
        }
        return value;
    }

    @Override
    public <T> T getInherited(String name, T defaultValue) {
        Class type = defaultValue == null ? null : defaultValue.getClass();
        Class value = this.getInherited(name, (T)type);
        if (value == null) {
            value = defaultValue;
        }
        return (T)value;
    }

    protected <T> T getParentComponentValue(String name, Class<T> type) {
        if (this.resource == null || this.resource.getName().equals("jcr:content")) {
            return null;
        }
        Resource parent = this.resource.getParent();
        if (parent == null) {
            return null;
        }
        return (T)new ComponentInheritanceValueMap(parent).getInherited(name, (T)type);
    }
}