001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.internal; 014 015import static org.apache.juneau.internal.ThrowableUtils.*; 016 017import java.util.*; 018 019import org.apache.juneau.*; 020 021/** 022 * Wrapper around a map where the key names are overridden. 023 * 024 * @param <K> The key class type. 025 * @param <V> The value class type. 026 */ 027public final class FilteredMap<K,V> extends AbstractMap<K,V> implements Delegate<Map<K,V>> { 028 029 final Map<K,V> innerMap; 030 final Set<Map.Entry<K,V>> entries; 031 final ClassMeta<Map<K,V>> classMeta; 032 033 /** 034 * Constructor. 035 * 036 * @param classMeta The class type of the map being wrapped. 037 * @param innerMap The map being wrapped. Must not be <jk>null</jk>. 038 * @param keys The keys in the new map. Must not be <jk>null</jk>. 039 */ 040 public FilteredMap(ClassMeta<Map<K,V>> classMeta, Map<K,V> innerMap, K[] keys) { 041 assertFieldNotNull(innerMap, "innerMap"); 042 assertFieldNotNull(keys, "keys"); 043 044 this.classMeta = classMeta; 045 this.innerMap = innerMap; 046 List<Map.Entry<K,V>> l = new ArrayList<>(keys.length); 047 for (K k : keys) 048 if (innerMap.containsKey(k)) 049 l.add(createEntry(k)); 050 entries = new ListSet<>(l); 051 } 052 053 private Map.Entry<K,V> createEntry(final K key) { 054 return new Map.Entry<K,V>() { 055 056 @Override /* Map.Entry */ 057 public K getKey() { 058 return key; 059 } 060 061 @Override /* Map.Entry */ 062 public V getValue() { 063 return innerMap.get(key); 064 } 065 066 @Override /* Map.Entry */ 067 public V setValue(V v) { 068 return innerMap.put(key, v); 069 } 070 }; 071 } 072 073 074 @Override /* Map */ 075 public Set<Map.Entry<K,V>> entrySet() { 076 return entries; 077 } 078 079 /** 080 * A set with ordered entries (a List with a Set API). 081 */ 082 private static final class ListSet<E> extends AbstractSet<E> { 083 084 private List<E> entries; 085 086 public ListSet(List<E> entries) { 087 this.entries = entries; 088 } 089 090 @Override /* Set */ 091 public Iterator<E> iterator() { 092 return entries.iterator(); 093 } 094 095 @Override /* Set */ 096 public int size() { 097 return entries.size(); 098 } 099 } 100 101 @Override /* Delegate */ 102 public ClassMeta<Map<K,V>> getClassMeta() { 103 return classMeta; 104 } 105}