ValueMapCreatingResourceOnDemand.java 3.27 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.granite.asset.core.impl;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public abstract class ValueMapCreatingResourceOnDemand
implements ModifiableValueMap {
    private Resource resource;
    private ValueMap valueMap;

    public abstract Resource getResource();

    public abstract Resource createResource() throws PersistenceException;

    public ValueMap getValueMap(Resource resource) {
        return (ValueMap)resource.adaptTo(ModifiableValueMap.class);
    }

    private ValueMap getMapForRead() {
        if (this.valueMap == null) {
            this.valueMap = ResourceUtil.getValueMap((Resource)this.getResource());
        }
        return this.valueMap;
    }

    private ValueMap getMapForWrite() {
        if (this.resource == null) {
            this.resource = this.getResource();
            if (this.resource == null) {
                try {
                    this.resource = this.createResource();
                }
                catch (PersistenceException e) {
                    throw new RuntimeException("Error creating resource on demand", (Throwable)e);
                }
            }
            this.valueMap = this.getValueMap(this.resource);
        }
        return this.valueMap;
    }

    public <T> T get(String name, Class<T> type) {
        return (T)this.getMapForRead().get(name, type);
    }

    public <T> T get(String name, T defaultValue) {
        return (T)this.getMapForRead().get(name, defaultValue);
    }

    public int size() {
        return this.getMapForRead().size();
    }

    public boolean isEmpty() {
        return this.getMapForRead().isEmpty();
    }

    public boolean containsKey(Object key) {
        return this.getMapForRead().containsKey(key);
    }

    public boolean containsValue(Object value) {
        return this.getMapForRead().containsValue(value);
    }

    public Object get(Object key) {
        return this.getMapForRead().get(key);
    }

    public Object put(String key, Object value) {
        return this.getMapForWrite().put((Object)key, value);
    }

    public Object remove(Object key) {
        return this.getMapForWrite().remove(key);
    }

    public void putAll(Map<? extends String, ?> map) {
        this.getMapForWrite().putAll(map);
    }

    public void clear() {
        this.getMapForWrite().clear();
    }

    public Set<String> keySet() {
        return this.getMapForRead().keySet();
    }

    public Collection<Object> values() {
        return this.getMapForRead().values();
    }

    public Set<Map.Entry<String, Object>> entrySet() {
        return this.getMapForRead().entrySet();
    }
}