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

org.jasig.cas.extension.clearpass.EhcacheBackedMap Maven / Gradle / Ivy

The newest version!
package org.jasig.cas.extension.clearpass;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * EhCache-backed implementation of a Map for caching a set of Strings.
 *
 * @deprecated As of 4.1, use {@link org.jasig.cas.authentication.CacheCredentialsMetaDataPopulator} instead.
 * @author Scott Battaglia
 * @since 1.0
 */
@Deprecated
public final class EhcacheBackedMap implements Map {

    @NotNull
    private final Cache cache;

    /**
     * Instantiates a new ehcache backed map.
     *
     * @param cache the cache
     */
    public EhcacheBackedMap(final Cache cache) {
        this.cache = cache;
    }

    @Override
    public int size() {
        return this.cache.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.cache.getSize() == 0;
    }

    @Override
    public boolean containsKey(final Object key) {
        return get(key) != null;
    }

    @Override
    public boolean containsValue(final Object value) {
        final Collection col = values();
        return col.contains(value);
    }

    @Override
    public String get(final Object key) {
        final Element element = this.cache.get(key);

        return element == null ? null : (String) element.getValue();
    }

    @Override
    public String put(final String key, final String value) {
        this.cache.put(new Element(key, value));
        return value;
    }

    @Override
    public String remove(final Object key) {
        final String keyValue = get(key);
        this.cache.remove(key);
        return keyValue;
    }

    @Override
    public void putAll(final Map m) {
        for (final Map.Entry entry : m.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public void clear() {
        this.cache.removeAll();
    }

    @Override
    public Set keySet() {
        return new HashSet<>(this.cache.getKeys());
    }

    @Override
    public Collection values() {
        final Set keys = keySet();
        final Collection values = new ArrayList<>();

        for (final String key : keys) {
            final String value = get(key);
            if (value != null) {
                values.add(value);
            }
        }

        return values;
    }

    @Override
    public Set> entrySet() {
        final Set keys = keySet();
        final Set> entries = new HashSet<>();

        for (final String key : keys) {
            final Element element = this.cache.get(key);

            if (element != null) {
                entries.add(new ElementMapEntry(element));
            }
        }

        return entries;

    }

    protected static final class ElementMapEntry implements Map.Entry {

        private final Element element;

        /**
         * Instantiates a new element map entry.
         *
         * @param element the element
         */
        public ElementMapEntry(final Element element) {
            this.element = element;
        }
        @Override
        public String getKey() {
            return (String) element.getKey();
        }

        @Override
        public String getValue() {
            return (String) element.getValue();
        }

        @Override
        public String setValue(final String value) {
            throw new UnsupportedOperationException("Operation Not Supported");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy