JcrProps.java 1.83 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.util.callbacks.Option
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.jetbrains.annotations.NotNull
 */
package com.adobe.cq.dam.s7imaging.impl.jcr.props;

import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropKey;
import com.scene7.is.util.callbacks.Option;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.jetbrains.annotations.NotNull;

public class JcrProps {
    private static final JcrProps EMPTY_PROPS = new JcrProps(null){

        @Override
        protected Option<Property> getValue(String key) throws RepositoryException {
            return Option.none();
        }
    };
    private final Node node;

    public static JcrProps jcrProps(@NotNull Node node) {
        return new JcrProps(node);
    }

    public static JcrProps jcrProps(@NotNull Resource resource) {
        return new JcrProps((Node)resource.adaptTo(Node.class));
    }

    public static JcrProps emptyJcrProps() {
        return EMPTY_PROPS;
    }

    public final <T> T get(PropKey<T> propKey) throws RepositoryException {
        Option<Property> propValue = this.getValue(propKey.name);
        return propKey.extractValue(propValue);
    }

    public final <T> T get(PropKey<Option<T>> propKey, T defaultValue) throws RepositoryException {
        return (T)this.get(propKey).getOrElse(defaultValue);
    }

    protected Option<Property> getValue(String key) throws RepositoryException {
        if (this.node.hasProperty(key)) {
            return Option.some((Object)this.node.getProperty(key));
        }
        return Option.none();
    }

    private JcrProps(Node node) {
        this.node = node;
    }

}