DefaultContext.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.codehaus.griffon.runtime.core;
019 
020 import griffon.core.Context;
021 
022 import javax.annotation.Nonnull;
023 import javax.annotation.Nullable;
024 import java.util.HashSet;
025 import java.util.Map;
026 import java.util.Set;
027 import java.util.concurrent.ConcurrentHashMap;
028 
029 import static griffon.util.GriffonNameUtils.requireNonBlank;
030 
031 /**
032  @author Andres Almiray
033  @since 2.2.0
034  */
035 public class DefaultContext extends AbstractContext {
036     protected static final String ERROR_KEY_BLANK = "Argument 'key' must not be blank";
037     private final Map<String, Object> attributes = new ConcurrentHashMap<>();
038 
039     public DefaultContext() {
040         this(null);
041     }
042 
043     public DefaultContext(@Nullable Context parentContext) {
044         super(parentContext);
045     }
046 
047     @Nullable
048     @Override
049     protected Object doGet(@Nonnull String key) {
050         requireNonBlank(key, ERROR_KEY_BLANK);
051         return attributes.get(key);
052     }
053 
054     @Override
055     public boolean hasKey(@Nonnull String key) {
056         requireNonBlank(key, ERROR_KEY_BLANK);
057         return attributes.containsKey(key);
058     }
059 
060     @Nullable
061     @Override
062     public Object remove(@Nonnull String key) {
063         requireNonBlank(key, ERROR_KEY_BLANK);
064         return attributes.remove(key);
065     }
066 
067     @Nullable
068     @Override
069     @SuppressWarnings("unchecked")
070     public <T> T removeAs(@Nonnull String key) {
071         return (Tremove(key);
072     }
073 
074     @Nullable
075     @Override
076     @SuppressWarnings("unchecked")
077     public <T> T removeConverted(@Nonnull String key, @Nonnull Class<T> type) {
078         return convertValue(remove(key), type);
079     }
080 
081     @Override
082     public void put(@Nonnull String key, @Nullable Object value) {
083         requireNonBlank(key, ERROR_KEY_BLANK);
084         attributes.put(key, value);
085     }
086 
087     @Override
088     public void putAt(@Nonnull String key, @Nullable Object value) {
089         put(key, value);
090     }
091 
092     @Override
093     public void destroy() {
094         attributes.clear();
095         super.destroy();
096     }
097 
098     @Nonnull
099     @Override
100     public Set<String> keySet() {
101         Set<String> keys = new HashSet<>(attributes.keySet());
102         if (parentContext != null) {
103             keys.addAll(parentContext.keySet());
104         }
105         return keys;
106     }
107 }