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;
014
015import java.lang.reflect.*;
016import java.util.*;
017
018/**
019 * Subclass of {@link ObjectMap} that avoids adding common default values.
020 */
021public class DefaultFilteringObjectMap extends ObjectMap {
022
023   private static final long serialVersionUID = 1L;
024
025   /**
026    * Constructor.
027    *
028    * @param m
029    *    The object map to copy from.
030    *    <br>Can be <jk>null</jk>.
031    */
032   public DefaultFilteringObjectMap(ObjectMap m) {
033      super();
034      appendAll(m);
035   }
036
037   /**
038    * Constructor.
039    */
040   public DefaultFilteringObjectMap() {
041      super();
042   }
043
044   @Override /* ObjectMap */
045   public ObjectMap append(String key, Object value) {
046      if (! shouldSkip(value))
047         super.append(key, value);
048      return this;
049   }
050
051   /**
052    * Returns <jk>true</jk> if the specified value should be skipped.
053    *
054    * @param value The value to check.
055    * @return <jk>true</jk> if the specified value should be skipped.
056    */
057   protected boolean shouldSkip(Object value) {
058      return
059         value == null
060         || (value instanceof Boolean && value.equals(false))
061         || (value instanceof Number && ((Number)value).intValue() == -1)
062         || (value.getClass().isArray() && Array.getLength(value) == 0)
063         || (value instanceof Map && ((Map<?,?>)value).isEmpty())
064         || (value instanceof Collection && ((Collection<?>)value).isEmpty());
065
066   }
067}