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

com.adobe.granite.ui.components.ds.ValueMapResource Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.granite.ui.components.ds;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;

/**
 * A {@link SyntheticResource} that will return the passed ValueMap when this
 * resource is adapted to ValueMap.
 */
public class ValueMapResource extends SyntheticResource {
    @Nonnull
    private ValueMap vm;

    private Collection children;

    /**
     * Instantiates with an empty ValueMap initially.
     *
     * @param resourceResolver
     *            The resource resolver
     * @param path
     *            The resource path
     * @param resourceType
     *            The resource type
     */
    public ValueMapResource(ResourceResolver resourceResolver, String path, String resourceType) {
        this(resourceResolver, path, resourceType, new ValueMapDecorator(new HashMap<>()));
    }

    /**
     * Instantiates with the given ValueMap.
     *
     * @param resourceResolver
     *            The resource resolver
     * @param path
     *            The resource path
     * @param resourceType
     *            The resource type
     * @param vm
     *            The ValueMap of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, String path, String resourceType, @Nonnull ValueMap vm) {
        this(resourceResolver, path, resourceType, vm, null);
    }

    /**
     * Instantiates with the given child resources.
     *
     * @param resourceResolver
     *            The resource resolver
     * @param path
     *            The resource path
     * @param resourceType
     *            The resource type
     * @param vm
     *            The ValueMap of the resource
     * @param children
     *            The children of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, String path, String resourceType, @Nonnull ValueMap vm,
            @CheckForNull Collection children) {
        super(resourceResolver, path, resourceType);
        this.vm = vm;
        this.children = children;
    }

    /**
     * Instantiates with an empty ValueMap initially.
     *
     * @param resourceResolver
     *            The resource resolver
     * @param rm
     *            The resource meta data
     * @param resourceType
     *            The resource type
     */
    public ValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType) {
        this(resourceResolver, rm, resourceType, new ValueMapDecorator(new HashMap<>()));
    }

    /**
     * Instantiates with the given ValueMap.
     *
     * @param resourceResolver
     *            The resource resolver
     * @param rm
     *            The resource meta data
     * @param resourceType
     *            The resource type
     * @param vm
     *            The ValueMap of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType,
            @Nonnull ValueMap vm) {
        this(resourceResolver, rm, resourceType, vm, null);
    }

    /**
     * Instantiates with the given child resources.
     *
     * @param resourceResolver
     *            The resource resolver
     * @param rm
     *            The resource meta data
     * @param resourceType
     *            The resource type
     * @param vm
     *            The ValueMap of the resource
     * @param children
     *            The children of the resource
     */
    public ValueMapResource(ResourceResolver resourceResolver, ResourceMetadata rm, String resourceType,
            @Nonnull ValueMap vm, @CheckForNull Collection children) {
        super(resourceResolver, rm, resourceType);
        this.vm = vm;
        this.children = children;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  Type adaptTo(@SuppressWarnings("null") Class type) {
        if (ValueMap.class.equals(type)) {
            return (Type) vm;
        } else {
            return super.adaptTo(type);
        }
    }

    @Override
    public Resource getChild(String relPath) {
        if (children == null) {
            return super.getChild(relPath);
        }
        for (Resource child : children) {
            if (child.getPath().equals(getPath() + "/" + relPath)) {
                return child;
            }
        }
        return null;
    }

    @Override
    public Iterator listChildren() {
        if (children == null) {
            return super.listChildren();
        }
        return children.iterator();
    }

    @Override
    public Iterable getChildren() {
        // This method cannot be removed due to baseline, even though it is identical to
        // the super class
        return super.getChildren();
    }

    @Override
    public boolean hasChildren() {
        if (children == null) {
            return super.hasChildren();
        }
        return !children.isEmpty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy