| 
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
 23 /**
 24  * @author Andres Almiray
 25  * @since 2.2.0
 26  */
 27 public abstract class AbstractContext implements Context {
 28     protected Context parentContext;
 29
 30     public AbstractContext(@Nullable Context parentContext) {
 31         this.parentContext = parentContext;
 32     }
 33
 34     @Nullable
 35     @Override
 36     @SuppressWarnings("unchecked")
 37     public <T> T get(@Nonnull String key, @Nullable T defaultValue) {
 38         T value = (T) get(key);
 39         return value != null ? value : defaultValue;
 40     }
 41
 42     @Nullable
 43     @Override
 44     public Object getAt(@Nonnull String key) {
 45         return get(key);
 46     }
 47
 48     @Nullable
 49     @Override
 50     public <T> T getAt(@Nonnull String key, @Nullable T defaultValue) {
 51         return get(key, defaultValue);
 52     }
 53
 54     @Nullable
 55     @Override
 56     public Object get(@Nonnull String key) {
 57         if (containsKey(key)) {
 58             return doGet(key);
 59         } else if (parentContext != null) {
 60             return parentContext.get(key);
 61         } else {
 62             return null;
 63         }
 64     }
 65
 66     @Override
 67     public void destroy() {
 68         parentContext = null;
 69     }
 70
 71     @Nullable
 72     protected abstract Object doGet(@Nonnull String key);
 73 }
 |