Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/util/NullUtils.java


1   /*
2    * NullUtils.java
3    *
4    * Created on 20 febbraio 2002, 18.08
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.util;
26  
27  import java.util.List;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  
31  /**
32   * Contains some functions for use with null values.
33   * @author Antonio Petrelli
34   * @version 0.0.1
35   */
36  public class NullUtils {
37  
38      /** Creates new NullUtils */
39      public NullUtils() {
40      }
41  
42      /** Returns an array of null objects.
43       * @param count How many objects in the array?
44       * @return The requested array.
45       */    
46      public static Object[] nullArray(int count) {
47          int i;
48          Object[] response;
49          
50          if (count > 0) {
51              response = new Object[count];
52              for (i=0; i < count; i++)
53                  response[i] = null;
54          }
55          else
56              response = null;
57          return response;
58      }
59      
60      /** A List of null objects.
61       * @param count How many objects do you want?
62       * @return The requested list.
63       */    
64      public static List nullList(int count) {
65          int i;
66          List response;
67          
68          if (count > 0) {
69              response = new ArrayList();
70              for (i=0; i < count; i++)
71                  response.add(null);
72          }
73          else
74              response = null;
75          return response;
76      }
77      
78      /** Checks if an array is made only of null objects.
79       * @param arr The array to check.
80       * @return <CODE>true</CODE>: the array contains only null objects;
81       * <CODE>false</CODE>: otherwise.
82       */    
83      public static boolean isArrayNull(Object[] arr) {
84          int i, numObjects;
85          boolean response;
86          
87          numObjects = arr.length;
88          response = true;
89          for (i=0; i < numObjects && response; i++)
90              if (arr[i] != null)
91                  response = false;
92          return response;
93      }
94      
95      /** Checks if a list contains only null objects.
96       * @param lst The list to check.
97       * @return <CODE>true</CODE>: the list contains only null objects;
98       * <CODE>false</CODE>: otherwise.
99       */    
100     public static boolean isListNull(List lst) {
101         Iterator it;
102         boolean response;
103         
104         it = lst.iterator();
105         response = true;
106         while (it.hasNext() && response)
107             if (it.next() != null)
108                 response = false;
109         return response;
110     }
111 }