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.common.internal.StringUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.collections.*;
021
022/**
023 * Represents a wrapped {@link Map} where entries in the map can be removed without affecting the underlying map.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026
027 * </ul>
028 *
029 * @param <T> The class type of the wrapped bean.
030 * @serial exclude
031 */
032@SuppressWarnings("rawtypes")
033public class DelegateMap<T extends Map> extends JsonMap implements Delegate<T> {
034   private static final long serialVersionUID = 1L;
035
036   private transient ClassMeta<T> classMeta;
037
038   /**
039    * Constructor.
040    *
041    * @param m The metadata object that created this delegate object.
042    * @param session The current bean session.
043    */
044   @SuppressWarnings("unchecked")
045   public DelegateMap(T m, BeanSession session) {
046      this.classMeta = session.getClassMetaForObject(m);
047      m.forEach((k,v) -> put(stringify(k), v));
048   }
049
050   @Override /* Delegate */
051   public ClassMeta<T> getClassMeta() {
052      return classMeta;
053   }
054
055   /**
056    * Remove all but the specified keys from this map.
057    *
058    * <p>
059    * This does not affect the underlying map.
060    *
061    * @param keys The remaining keys in the map (in the specified order).
062    * @return This object.
063    */
064   public DelegateMap<T> filterKeys(List<String> keys) {
065      JsonMap m = new JsonMap();
066      keys.forEach(k -> {
067         if (containsKey(k))
068            m.put(k, get(k));
069      });
070      this.clear();
071      this.putAll(m);
072      return this;
073   }
074}