| 
01 /*02  * Copyright 2008-2015 the original author or authors.
 03  *
 04  * Licensed under the Apache License, Version 2.0 (the "License");
 05  * you may not use this file except in compliance with the License.
 06  * You may obtain a copy of the License at
 07  *
 08  *     http://www.apache.org/licenses/LICENSE-2.0
 09  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 package org.codehaus.griffon.runtime.core;
 17
 18 import griffon.core.Context;
 19
 20 import javax.annotation.Nonnull;
 21 import javax.annotation.Nullable;
 22 import java.util.HashSet;
 23 import java.util.Map;
 24 import java.util.Set;
 25 import java.util.concurrent.ConcurrentHashMap;
 26
 27 import static griffon.util.GriffonNameUtils.requireNonBlank;
 28
 29 /**
 30  * @author Andres Almiray
 31  * @since 2.2.0
 32  */
 33 public class DefaultContext extends AbstractContext {
 34     protected static final String ERROR_KEY_BLANK = "Argument 'key' must not be blank";
 35     private final Map<String, Object> attributes = new ConcurrentHashMap<>();
 36
 37     public DefaultContext() {
 38         this(null);
 39     }
 40
 41     public DefaultContext(@Nullable Context parentContext) {
 42         super(parentContext);
 43     }
 44
 45     @Nullable
 46     @Override
 47     protected Object doGet(@Nonnull String key) {
 48         requireNonBlank(key, ERROR_KEY_BLANK);
 49         return attributes.get(key);
 50     }
 51
 52     @Override
 53     public boolean hasKey(@Nonnull String key) {
 54         requireNonBlank(key, ERROR_KEY_BLANK);
 55         return attributes.containsKey(key);
 56     }
 57
 58     @Nullable
 59     @Override
 60     public Object remove(@Nonnull String key) {
 61         requireNonBlank(key, ERROR_KEY_BLANK);
 62         return attributes.remove(key);
 63     }
 64
 65     @Override
 66     public void put(@Nonnull String key, @Nullable Object value) {
 67         requireNonBlank(key, ERROR_KEY_BLANK);
 68         attributes.put(key, value);
 69     }
 70
 71     @Override
 72     public void putAt(@Nonnull String key, @Nullable Object value) {
 73         put(key, value);
 74     }
 75
 76     @Override
 77     public void destroy() {
 78         attributes.clear();
 79         super.destroy();
 80     }
 81
 82     @Nonnull
 83     @Override
 84     public Set<String> keySet() {
 85         Set<String> keys = new HashSet<>(attributes.keySet());
 86         if (parentContext != null) {
 87             keys.addAll(parentContext.keySet());
 88         }
 89         return keys;
 90     }
 91 }
 |