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

org.apache.sling.api.wrappers.ValueMapDecorator Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sling.api.wrappers;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.impl.ObjectConverter;

/**
 * ValueMapDecorator decorates another {@link Map}
 * to provide a basic implementation for the additional methods
 * of a {@link ValueMap}.
 */
public class ValueMapDecorator implements ValueMap {

    /**
     * underlying map
     */
    private final Map base;

    /**
     * Creates a new wrapper around a given map.
     * @param base wrapped object
     */
    public ValueMapDecorator(Map base) {
        this.base = base;
    }

    /**
     * {@inheritDoc}
     */
    public  T get(String name, Class type) {
        if (base instanceof ValueMap) {
            // shortcut if decorated map is ValueMap
            return ((ValueMap)base).get(name, type);
        }
        return ObjectConverter.convert(get(name), type);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public  T get(String name, T defaultValue) {
        if (base instanceof ValueMap) {
            // shortcut if decorated map is ValueMap
            return ((ValueMap)base).get(name, defaultValue);
        }
        if (defaultValue == null) {
            return (T)get(name);
        }
        T value = get(name, (Class) defaultValue.getClass());
        return value == null ? defaultValue : value;
    }

    /**
     * {@inheritDoc}
     */
    public int size() {
        return base.size();
    }

    /**
     * {@inheritDoc}
     */
    public boolean isEmpty() {
        return base.isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    public boolean containsKey(Object key) {
        return base.containsKey(key);
    }

    /**
     * {@inheritDoc}
     */
    public boolean containsValue(Object value) {
        return base.containsValue(value);
    }

    /**
     * {@inheritDoc}
     */
    public Object get(Object key) {
        return base.get(key);
    }

    /**
     * {@inheritDoc}
     */
    public Object put(String key, Object value) {
        return base.put(key, value);
    }

    /**
     * {@inheritDoc}
     */
    public Object remove(Object key) {
        return base.remove(key);
    }

    /**
     * {@inheritDoc}
     */
    public void putAll(Map t) {
        base.putAll(t);
    }

    /**
     * {@inheritDoc}
     */
    public void clear() {
        base.clear();
    }

    /**
     * {@inheritDoc}
     */
    public Set keySet() {
        return base.keySet();
    }

    /**
     * {@inheritDoc}
     */
    public Collection values() {
        return base.values();
    }

    /**
     * {@inheritDoc}
     */
    public Set> entrySet() {
        return base.entrySet();
    }

    @Override
    public String toString() {
        return super.toString() + " : " + this.base.toString();
    }

    @Override
    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return base.hashCode();
    }

    @Override
    /**
     * {@inheritDoc}
     */
    public boolean equals(Object obj) {
        return base.equals(obj);
    }


}