001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id: ScopedMap.java 20283 2007-06-11 09:45:21Z dmihalache $ 020 */ 021 022package org.nuxeo.common.collections; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.HashMap; 027import java.util.List; 028import java.util.Map; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032 033/** 034 * Scoped map holding data for a given scope. 035 * <p> 036 * Used to store context data and invalidate some data given its scope. Implements Map for easier use from interface. 037 * 038 * @see ScopeType 039 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 040 */ 041public class ScopedMap extends HashMap<String, Serializable> { 042 043 private static final Log log = LogFactory.getLog(ScopedMap.class); 044 045 private static final long serialVersionUID = -616031057117818344L; 046 047 /** 048 * Gets value for given scope and given key. 049 */ 050 public Serializable getScopedValue(ScopeType scope, String key) { 051 Serializable res = null; 052 if (scope != null && key != null) { 053 res = get(scope.getScopedKey(key)); 054 } 055 return res; 056 } 057 058 /** 059 * Gets value for given key using default scope. 060 */ 061 public Serializable getScopedValue(String key) { 062 return getScopedValue(ScopeType.DEFAULT, key); 063 } 064 065 /** 066 * Creates a Map with entries from DEFAULT scope. 067 */ 068 public Map<String, Serializable> getDefaultScopeValues() { 069 return getScopeValues(ScopeType.DEFAULT); 070 } 071 072 /** 073 * Creates a Map with entries from specified scope. 074 */ 075 public Map<String, Serializable> getScopeValues(ScopeType scopeType) { 076 Map<String, Serializable> defMap = new HashMap<String, Serializable>(); 077 078 final String defaultScopePrefix = scopeType.getScopePrefix(); 079 final int prefixLen = defaultScopePrefix.length(); 080 for (Map.Entry<String, Serializable> entry : entrySet()) { 081 String key = entry.getKey(); 082 if (key.startsWith(defaultScopePrefix)) { 083 defMap.put(key.substring(prefixLen), entry.getValue()); 084 } 085 } 086 087 return defMap; 088 } 089 090 /** 091 * Sets value for given scope and given key. 092 */ 093 public void putScopedValue(ScopeType scope, String key, Serializable value) { 094 if (scope == null || key == null) { 095 log.error(String.format("Cannot set scope value using scopeType=%s and key=%s", scope, key)); 096 } else { 097 put(scope.getScopedKey(key), value); 098 } 099 } 100 101 /** 102 * Sets key using default scope. 103 */ 104 public void putScopedValue(String key, Serializable value) { 105 putScopedValue(ScopeType.DEFAULT, key, value); 106 } 107 108 /** 109 * Removes all mappings for given scope. 110 */ 111 public void clearScope(ScopeType scopeType) { 112 if (scopeType == null) { 113 log.error("Cannot clear map, specified scope is null"); 114 } else { 115 String prefix = scopeType.getScopePrefix(); 116 List<String> toRemove = new ArrayList<String>(); 117 for (Map.Entry<String, Serializable> entry : entrySet()) { 118 String key = entry.getKey(); 119 if (key.startsWith(prefix)) { 120 toRemove.add(key); 121 } 122 } 123 for (String key : toRemove) { 124 remove(key); 125 } 126 } 127 } 128 129}