MergedInheritanceValueMap.java 5.87 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.inherit.InheritanceValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 */
package com.day.cq.wcm.webservicesupport.impl;

import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.webservicesupport.impl.ReadOnlyEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;

class MergedInheritanceValueMap
implements InheritanceValueMap {
    private static final ValueMap EMPTY = new ValueMapDecorator(Collections.emptyMap());
    private ResourceResolver resolver;
    private final List<CacheEntry> cache;

    public MergedInheritanceValueMap(ResourceResolver resolver, String basePath, String[] segments, String cutoffPath) {
        this.resolver = resolver;
        if (cutoffPath == null) {
            cutoffPath = "/";
        }
        this.cache = new ArrayList<CacheEntry>();
        String path = basePath;
        while (!cutoffPath.equals(path) && path != null) {
            for (String segment : segments) {
                this.cache.add(new CacheEntry(path + "/" + segment));
            }
            path = ResourceUtil.getParent((String)path);
        }
    }

    public <T> T get(String name, Class<T> type) {
        Object value = this.override().get(name, type);
        if (value == null) {
            value = this.base().get(name, type);
        }
        return (T)value;
    }

    private ValueMap base() {
        return this.cache.get(1).getData(this.resolver);
    }

    private ValueMap override() {
        return this.cache.get(0).getData(this.resolver);
    }

    public <T> T get(String name, T defaultValue) {
        Object value = defaultValue != null ? this.override().get(name, defaultValue.getClass()) : this.override().get(name, null);
        if (value == null) {
            value = this.base().get(name, defaultValue);
        }
        return (T)value;
    }

    public int size() {
        int size = this.base().size();
        for (String key : this.override().keySet()) {
            if (this.base().containsKey((Object)key)) continue;
            ++size;
        }
        return size;
    }

    public boolean isEmpty() {
        return this.base().isEmpty() && this.override().isEmpty();
    }

    public boolean containsKey(Object key) {
        return this.base().containsKey(key) || this.override().containsKey(key);
    }

    public boolean containsValue(Object value) {
        return this.base().containsValue(value) || this.override().containsValue(value);
    }

    public Object get(Object key) {
        Object value = this.override().get(key);
        if (value == null) {
            value = this.base().get(key);
        }
        return value;
    }

    public Object put(String key, Object value) {
        throw new UnsupportedOperationException();
    }

    public Object remove(Object key) {
        throw new UnsupportedOperationException();
    }

    public void putAll(Map<? extends String, ? extends Object> m) {
        throw new UnsupportedOperationException();
    }

    public void clear() {
        throw new UnsupportedOperationException();
    }

    public Set<String> keySet() {
        HashSet<String> keys = new HashSet<String>(this.size());
        keys.addAll(this.base().keySet());
        keys.addAll(this.override().keySet());
        return keys;
    }

    public Collection<Object> values() {
        ArrayList<Object> values = new ArrayList<Object>(this.size());
        for (String key : this.keySet()) {
            values.add(this.get(key));
        }
        return values;
    }

    public Set<Map.Entry<String, Object>> entrySet() {
        HashSet<Map.Entry<String, Object>> entries = new HashSet<Map.Entry<String, Object>>(this.size());
        for (String key : this.keySet()) {
            entries.add(new ReadOnlyEntry(key, this.get(key)));
        }
        return entries;
    }

    public <T> T getInherited(String key, Class<T> type) {
        for (CacheEntry entry : this.cache) {
            ValueMap valueMap = entry.getData(this.resolver);
            Object res = valueMap.get(key, type);
            if (res == null) continue;
            return (T)res;
        }
        return null;
    }

    public <T> T getInherited(String key, T defaultValue) {
        for (CacheEntry entry : this.cache) {
            ValueMap valueMap = entry.getData(this.resolver);
            Object res = defaultValue != null ? valueMap.get(key, defaultValue.getClass()) : valueMap.get(key, defaultValue);
            if (res == null) continue;
            return (T)res;
        }
        return defaultValue;
    }

    public String toString() {
        return this.getClass().getSimpleName() + "[ cache: " + this.cache + "]";
    }

    private static class CacheEntry {
        private ValueMap data;
        private String path;

        public CacheEntry(String path) {
            this.path = path;
        }

        public ValueMap getData(ResourceResolver resolver) {
            if (this.data == null) {
                Resource res = resolver.getResource(this.path);
                this.data = res == null ? EMPTY : (ValueMap)res.adaptTo(ValueMap.class);
            }
            return this.data;
        }

        public String toString() {
            return this.getClass().getSimpleName() + "[ path: " + this.path + ", hasData: " + (this.data != null) + "]";
        }
    }

}