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

org.soulwing.cas.service.TransformingMap Maven / Gradle / Ivy

Go to download

Wildfly subsystem that monitors web application deployments and attaches CAS authentication support.

There is a newer version: 2.0.3
Show newest version
/*
 * File created on Feb 25, 2015 
 *
 * Copyright (c) 2014 Virginia Polytechnic Institute and State University
 *
 * Licensed 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.soulwing.cas.service;

import java.util.AbstractMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.jboss.as.controller.transform.Transformers;
import org.soulwing.cas.api.Transformer;

/**
 * A {@link Map} that transforms mapped values in a delegate map using 
 * a map of corresponding {@link Transformers}.
 * 

* If the specified transformer map does not contain a value for a key defined * in the delegate map, the value of the delegate map is not transformed * (in other words, an identity transform is applied). * * @author Carl Harris */ class TransformingMap extends AbstractMap { private final Lock lock = new ReentrantLock(); private final Map delegate; private final Map> transformers; private Set> entrySet; /** * Constructs a new instance. * @param delegate */ public TransformingMap(Map delegate, Map> transformers) { this.delegate = delegate; this.transformers = transformers; } @Override public Set> entrySet() { if (entrySet == null) { lock.lock(); try { if (entrySet == null) { entrySet = new LinkedHashSet<>(); for (String key : delegate.keySet()) { Transformer transformer = transformers.get(key); Object s = delegate.get(key); Object t = transform(s, transformer); entrySet.add(new AbstractMap.SimpleEntry(key, t)); } } } finally { lock.unlock(); } } return entrySet; } private Object transform(Object s, Transformer transformer) { if (transformer == null) return s; if (s instanceof Iterable) { List t = new LinkedList<>(); for (Object obj : (Iterable) s) { t.add(transformer.transform(obj)); } return t; } else { return transformer.transform(s); } } }