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