DefaultContext.java
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.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24 
25 import static griffon.util.GriffonNameUtils.requireNonBlank;
26 
27 /**
28  @author Andres Almiray
29  @since 2.2.0
30  */
31 public class DefaultContext extends AbstractContext {
32     protected static final String ERROR_KEY_BLANK = "Argument 'key' must not be blank";
33     private final Map<String, Object> attributes = new ConcurrentHashMap<>();
34 
35     public DefaultContext() {
36         this(null);
37     }
38 
39     public DefaultContext(@Nullable Context parentContext) {
40         super(parentContext);
41     }
42 
43     @Nullable
44     @Override
45     protected Object doGet(@Nonnull String key) {
46         requireNonBlank(key, ERROR_KEY_BLANK);
47         return attributes.get(key);
48     }
49 
50     @Override
51     public boolean containsKey(@Nonnull String key) {
52         requireNonBlank(key, ERROR_KEY_BLANK);
53         return attributes.containsKey(key);
54     }
55 
56     @Nullable
57     @Override
58     public Object remove(@Nonnull String key) {
59         requireNonBlank(key, ERROR_KEY_BLANK);
60         return attributes.remove(key);
61     }
62 
63     @Override
64     public void put(@Nonnull String key, @Nullable Object value) {
65         requireNonBlank(key, ERROR_KEY_BLANK);
66         attributes.put(key, value);
67     }
68 
69     @Override
70     public void putAt(@Nonnull String key, @Nullable Object value) {
71         put(key, value);
72     }
73 
74     @Override
75     public void destroy() {
76         attributes.clear();
77         super.destroy();
78     }
79 }