1 /** 2 * ServingXML 3 * 4 * Copyright (C) 2006 Daniel Parker 5 * daniel.parker@servingxml.com 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 **/ 20 21 package com.servingxml.util; 22 23 import java.util.Map; 24 import java.util.HashMap; 25 26 /** 27 * 28 * 01/05/15 29 * @author Daniel A. Parker (daniel.parker@servingxml.com) 30 */ 31 32 public class ChildDictionary<K,V> implements MutableDictionary<K,V> { 33 private final Dictionary<K,V> parent; 34 private final Map<K,V> backingMap; 35 ChildDictionary(Dictionary<K,V> parent)36 public ChildDictionary(Dictionary<K,V> parent) { 37 this.parent = parent; 38 this.backingMap = new HashMap<K,V>(); 39 } 40 get(K key)41 public V get(K key) { 42 V value = backingMap.get(key); 43 return value != null ? value : parent.get(key); 44 } 45 add(K key, V value)46 public void add(K key, V value) { 47 backingMap.put(key,value); 48 } 49 size()50 public int size() { 51 return backingMap.size() + parent.size(); 52 } 53 createChildDictionary()54 public MutableDictionary<K,V> createChildDictionary() { 55 return new ChildDictionary<K,V>(this); 56 } 57 } 58