DefaultContext.java
001 /*
002  * Copyright 2008-2016 the original author or authors.
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 package org.codehaus.griffon.runtime.core;
017 
018 import griffon.core.Context;
019 
020 import javax.annotation.Nonnull;
021 import javax.annotation.Nullable;
022 import java.util.HashSet;
023 import java.util.Map;
024 import java.util.Set;
025 import java.util.concurrent.ConcurrentHashMap;
026 
027 import static griffon.util.GriffonNameUtils.requireNonBlank;
028 
029 /**
030  @author Andres Almiray
031  @since 2.2.0
032  */
033 public class DefaultContext extends AbstractContext {
034     protected static final String ERROR_KEY_BLANK = "Argument 'key' must not be blank";
035     private final Map<String, Object> attributes = new ConcurrentHashMap<>();
036 
037     public DefaultContext() {
038         this(null);
039     }
040 
041     public DefaultContext(@Nullable Context parentContext) {
042         super(parentContext);
043     }
044 
045     @Nullable
046     @Override
047     protected Object doGet(@Nonnull String key) {
048         requireNonBlank(key, ERROR_KEY_BLANK);
049         return attributes.get(key);
050     }
051 
052     @Override
053     public boolean hasKey(@Nonnull String key) {
054         requireNonBlank(key, ERROR_KEY_BLANK);
055         return attributes.containsKey(key);
056     }
057 
058     @Nullable
059     @Override
060     public Object remove(@Nonnull String key) {
061         requireNonBlank(key, ERROR_KEY_BLANK);
062         return attributes.remove(key);
063     }
064 
065     @Nullable
066     @Override
067     @SuppressWarnings("unchecked")
068     public <T> T removeAs(@Nonnull String key) {
069         return (Tremove(key);
070     }
071 
072     @Nullable
073     @Override
074     @SuppressWarnings("unchecked")
075     public <T> T removeConverted(@Nonnull String key, @Nonnull Class<T> type) {
076         return convertValue(remove(key), type);
077     }
078 
079     @Override
080     public void put(@Nonnull String key, @Nullable Object value) {
081         requireNonBlank(key, ERROR_KEY_BLANK);
082         attributes.put(key, value);
083     }
084 
085     @Override
086     public void putAt(@Nonnull String key, @Nullable Object value) {
087         put(key, value);
088     }
089 
090     @Override
091     public void destroy() {
092         attributes.clear();
093         super.destroy();
094     }
095 
096     @Nonnull
097     @Override
098     public Set<String> keySet() {
099         Set<String> keys = new HashSet<>(attributes.keySet());
100         if (parentContext != null) {
101             keys.addAll(parentContext.keySet());
102         }
103         return keys;
104     }
105 }