org.apache.shiro.subject.SimplePrincipalMap Maven / Gradle / Ivy
/*
* 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.shiro.subject;
import org.apache.shiro.util.CollectionUtils;
import java.util.*;
/**
* Default implementation of the {@link PrincipalMap} interface.
*
* *EXPERIMENTAL for Shiro 1.2 - DO NOT USE YET*
*
* @author Les Hazlewood
* @since 1.2
*/
public class SimplePrincipalMap implements PrincipalMap {
//Key: realm name, Value: map of principals specific to that realm
// internal map - key: principal name, value: principal
private Map> realmPrincipals;
//maintains the principals from all realms plus any that are modified via the Map modification methods
//this ensures a fast lookup of any named principal instead of needing to iterate over
//the realmPrincipals for each lookup.
private Map combinedPrincipals;
public SimplePrincipalMap() {
this(null);
}
public SimplePrincipalMap(Map> backingMap) {
if (!CollectionUtils.isEmpty(backingMap)) {
this.realmPrincipals = backingMap;
for (Map principals : this.realmPrincipals.values()) {
if (!CollectionUtils.isEmpty(principals) ) {
ensureCombinedPrincipals().putAll(principals);
}
}
}
}
public int size() {
return CollectionUtils.size(this.combinedPrincipals);
}
protected Map ensureCombinedPrincipals() {
if (this.combinedPrincipals == null) {
this.combinedPrincipals = new HashMap();
}
return this.combinedPrincipals;
}
public boolean containsKey(Object o) {
return this.combinedPrincipals != null && this.combinedPrincipals.containsKey(o);
}
public boolean containsValue(Object o) {
return this.combinedPrincipals != null && this.combinedPrincipals.containsKey(o);
}
public Object get(Object o) {
return this.combinedPrincipals != null && this.combinedPrincipals.containsKey(o);
}
public Object put(String s, Object o) {
return ensureCombinedPrincipals().put(s, o);
}
public Object remove(Object o) {
return this.combinedPrincipals != null ? this.combinedPrincipals.remove(o) : null;
}
public void putAll(Map extends String, ?> map) {
if (!CollectionUtils.isEmpty(map)) {
ensureCombinedPrincipals().putAll(map);
}
}
public Set keySet() {
return CollectionUtils.isEmpty(this.combinedPrincipals) ?
Collections.emptySet() :
Collections.unmodifiableSet(this.combinedPrincipals.keySet());
}
public Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy