Coverage Report - org.apache.shindig.social.core.util.NullPropertyFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
NullPropertyFilter
95%
19/20
90%
18/20
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util;
 19  
 
 20  
 import java.util.Collection;
 21  
 
 22  
 import net.sf.json.JSONArray;
 23  
 import net.sf.json.JSONObject;
 24  
 import net.sf.json.util.PropertyFilter;
 25  
 
 26  
 /**
 27  
  * A property filter that rejects null values.
 28  
  */
 29  21
 public class NullPropertyFilter implements PropertyFilter {
 30  
 
 31  
   /**
 32  
    * Filter the output of a property, if it should be emitted return false.
 33  
    * @param source The object containing the value
 34  
    * @param name the name of the key in the output structure
 35  
    * @param value the value of the object
 36  
    * @return true if the property should be filtered, false if not.
 37  
    */
 38  
   public boolean apply(Object source, String name, Object value) {
 39  347
     if (value == null) {
 40  150
       return true;
 41  
     }
 42  197
     if (value instanceof JSONArray) {
 43  12
       JSONArray array = (JSONArray) value;
 44  12
       if (array.size() == 0) {
 45  1
         return true;
 46  
       }
 47  
     }
 48  196
     if (value instanceof JSONObject) {
 49  8
       JSONObject object = (JSONObject) value;
 50  8
       if (object.isNullObject() || object.isEmpty()) {
 51  0
         return true;
 52  
       }
 53  
     }
 54  196
     if (value instanceof Collection) {
 55  17
       Collection<?> collection = (Collection<?>) value;
 56  17
       if (collection.size() == 0) {
 57  1
         return true;
 58  
       }
 59  
     }
 60  195
     if (value instanceof Object[]) {
 61  2
       Object[] oarray = (Object[]) value;
 62  2
       if (oarray.length == 0) {
 63  1
         return true;
 64  
       }
 65  
     }
 66  194
     return false;
 67  
   }
 68  
 
 69  
 }