Coverage report

  %line %branch
org.apache.turbine.util.ObjectUtils
80% 
79% 

 1  
 package org.apache.turbine.util;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.io.BufferedInputStream;
 20  
 import java.io.BufferedOutputStream;
 21  
 import java.io.ByteArrayInputStream;
 22  
 import java.io.ByteArrayOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.ObjectInputStream;
 25  
 import java.io.ObjectOutputStream;
 26  
 import java.io.Serializable;
 27  
 import java.util.Enumeration;
 28  
 import java.util.Hashtable;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * This is where common Object manipulation routines should go.
 33  
  *
 34  
  * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
 35  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 36  
  * @version $Id: ObjectUtils.java 264148 2005-08-29 14:21:04Z henning $
 37  
  */
 38  0
 public abstract class ObjectUtils
 39  
 {
 40  
     /**
 41  
      * Returns a default value if the object passed is null.
 42  
      *
 43  
      * @param o The object to test.
 44  
      * @param dflt The default value to return.
 45  
      * @return The object o if it is not null, dflt otherwise.
 46  
      * @deprecated Use org.apache.commons.lang.ObjectUtils.defaultIfNull()
 47  
      */
 48  
     public static Object isNull(Object o, Object dflt)
 49  
     {
 50  0
 		return org.apache.commons.lang.ObjectUtils.defaultIfNull(o,dflt);
 51  
     }
 52  
 
 53  
     /**
 54  
      * Adds an object to a vector, making sure the object is in the
 55  
      * vector only once.
 56  
      *
 57  
      * @param v The vector.
 58  
      * @param o The object.
 59  
      * @deprecated No replacement
 60  
      */
 61  
     public static void addOnce(List l, Object o)
 62  
     {
 63  154
         if (!l.contains(o))
 64  
         {
 65  0
             l.add(o);
 66  
         }
 67  154
     }
 68  
 
 69  
     /**
 70  
      * Converts a hashtable to a byte array for storage/serialization.
 71  
      *
 72  
      * @param hash The Hashtable to convert.
 73  
      *
 74  
      * @return A byte[] with the converted Hashtable.
 75  
      *
 76  
      * @exception Exception A generic exception.
 77  
      */
 78  
     public static byte[] serializeHashtable(Hashtable hash)
 79  
         throws Exception
 80  
     {
 81  22
         Hashtable saveData = new Hashtable(hash.size());
 82  22
         String key = null;
 83  22
         Object value = null;
 84  22
         byte[] byteArray = null;
 85  
 
 86  22
         Enumeration keys = hash.keys();
 87  
 
 88  35
         while (keys.hasMoreElements())
 89  
         {
 90  2
             key = (String) keys.nextElement();
 91  2
             value = hash.get(key);
 92  2
             if (value instanceof Serializable)
 93  
             {
 94  2
                 saveData.put (key, value);
 95  
             }
 96  
         }
 97  
 
 98  22
         ByteArrayOutputStream baos = null;
 99  22
         BufferedOutputStream bos = null;
 100  22
         ObjectOutputStream out = null;
 101  
         try
 102  
         {
 103  
             // These objects are closed in the finally.
 104  22
             baos = new ByteArrayOutputStream();
 105  22
             bos  = new BufferedOutputStream(baos);
 106  22
             out  = new ObjectOutputStream(bos);
 107  
 
 108  22
             out.writeObject(saveData);
 109  22
             out.flush();
 110  22
             bos.flush();
 111  
 
 112  22
             byteArray = baos.toByteArray();
 113  22
         }
 114  
         finally
 115  
         {
 116  0
             if (out != null)
 117  
             {
 118  22
                 out.close();
 119  
             }
 120  22
             if (bos != null)
 121  
             {
 122  22
                 bos.close();
 123  
             }
 124  22
             if (baos != null)
 125  
             {
 126  22
                 baos.close();
 127  
             }
 128  11
         }
 129  22
         return byteArray;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Deserializes a single object from an array of bytes.
 134  
      *
 135  
      * @param objectData The serialized object.
 136  
      *
 137  
      * @return The deserialized object, or <code>null</code> on failure.
 138  
      */
 139  
     public static Object deserialize(byte[] objectData)
 140  
     {
 141  16
         Object object = null;
 142  
 
 143  16
         if (objectData != null)
 144  
         {
 145  
             // These streams are closed in finally.
 146  16
             ObjectInputStream in = null;
 147  16
             ByteArrayInputStream bin = new ByteArrayInputStream(objectData);
 148  16
             BufferedInputStream bufin = new BufferedInputStream(bin);
 149  
 
 150  
             try
 151  
             {
 152  16
                 in = new ObjectInputStream(bufin);
 153  
 
 154  
                 // If objectData has not been initialized, an
 155  
                 // exception will occur.
 156  10
                 object = in.readObject();
 157  10
             }
 158  3
             catch (Exception e)
 159  3
             {
 160  6
             }
 161  
             finally
 162  
             {
 163  0
                 try
 164  
                 {
 165  16
                     if (in != null)
 166  
                     {
 167  10
                         in.close();
 168  
                     }
 169  
 
 170  16
                     bufin.close();
 171  16
                     bin.close();
 172  8
                 }
 173  0
                 catch (IOException e)
 174  
                 {
 175  24
                 }
 176  8
             }
 177  
         }
 178  16
         return object;
 179  
     }
 180  
 
 181  
     /**
 182  
      * Compares two Objects, returns true if their values are the
 183  
      * same.  It checks for null values prior to an o1.equals(o2)
 184  
      * check
 185  
      *
 186  
      * @param o1 The first object.
 187  
      * @param o2 The second object.
 188  
      * @return True if the values of both xstrings are the same.
 189  
      * @deprecated Use org.apache.commons.lang.ObjectUtils.equals()
 190  
      */
 191  
     public static boolean equals(Object o1, Object o2)
 192  
     {
 193  0
 		return org.apache.commons.lang.ObjectUtils.equals(o1,o2);
 194  
     }
 195  
 
 196  
     /**
 197  
      * Nice method for adding data to a Hashtable in such a way
 198  
      * as to not get NPE's. The point being that if the
 199  
      * value is null, Hashtable.put() will throw an exception.
 200  
      * That blows in the case of this class cause you may want to
 201  
      * essentially treat put("Not Null", null ) == put("Not Null", "")
 202  
      * We will still throw a NPE if the key is null cause that should
 203  
      * never happen.
 204  
      * @deprecated No replacement
 205  
      */
 206  
     public static final void safeAddToHashtable(Hashtable hash, Object key,
 207  
                                                 Object value)
 208  
             throws NullPointerException
 209  
     {
 210  0
         if (value == null)
 211  
         {
 212  0
             hash.put(key, "");
 213  
         }
 214  
         else
 215  
         {
 216  0
             hash.put(key, value);
 217  
         }
 218  0
     }
 219  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.