View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class DataType
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:05 $
11   * 
12   * $Id: DataType.java,v 1.3 2004/03/03 11:53:05 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.data;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  // import foo.*;
27  
28  /*====================================================================
29                     Implementation of class DataType                   
30  ====================================================================*/
31  /***
32   * DataType defines privitive data type and few array related methods.
33   * 
34   * @version $Revision: 1.3 $
35   * @author Masahiro Takatsuka (masa@jbeans.net)
36   */
37  
38  public class DataType {
39  	protected DataType() {
40  		super();
41  	}
42  	
43  	// These types are returned by GetDataType to indicate pixel type.
44  	public static final int VOID = 		0;
45  	public static final int BIT = 		1;	
46  	public static final int CHAR = 		2;
47  	public static final int SHORT =		3;
48  	public static final int INT = 		4;
49  	public static final int LONG = 		5;
50  	public static final int FLOAT = 	6;
51  	public static final int DOUBLE = 	7;
52  	public static final int STRING = 	8;
53  	public static final int BOOLEAN = 	9;		
54  
55    	public static final int POLY_DATA          = 0;
56  //  	public static final int STRUCTURED_POINTS  = 1;
57  //  	public static final int STRUCTURED_GRID    = 2;
58  //  	public static final int RECTILINEAR_GRID   = 3;
59  //  	public static final int UNSTRUCTURED_GRID  = 4;
60  
61  	public static boolean isArray(Object obj) {
62  		return obj.getClass().isArray();
63  	}
64  	
65  	public static int getArrayDimension(Class class1) {
66  		int dim = -1;
67  		if (! class1.isArray()) {
68  			dim = -1;
69  		} else {
70  			String name = class1.getName();
71  			int idx = -1;
72  			dim = 0;
73  			for (;;) {
74  				idx = name.indexOf("[");
75  				if (idx == -1) {
76  					break;
77  				}
78  				dim++;
79  				name = name.substring(idx + 1);
80  			}
81  		}
82  		return dim;
83  	}
84  	
85  	public static int getArrayDimension(Object obj) {
86  		return getArrayDimension(obj.getClass());
87  	}
88  	
89  	public static Class getArrayType(Class class1) {
90  		if (! class1.isArray()) {
91  			return null;
92  		} 
93  		String name = class1.getName();
94  		int idx = name.lastIndexOf("[");
95  		name = name.substring(idx + 1);
96  		if (name.startsWith("B")) { // byte
97  			return Byte.TYPE;
98  		} else if (name.startsWith("C")) { // char
99  			return Character.TYPE;
100 		} else if (name.startsWith("D")) { // double
101 			return Double.TYPE;
102 		} else if (name.startsWith("F")) { // flaot
103 			return Float.TYPE;
104 		} else if (name.startsWith("I")) { // int
105 			return Integer.TYPE;
106 		} else if (name.startsWith("J")) { // long
107 			return Long.TYPE;
108 		} else if (name.startsWith("S")) { // short
109 			return Short.TYPE;
110 		} else if (name.startsWith("B")) { // boolean
111 			return Boolean.TYPE;
112 		} else if (name.startsWith("L")) { // class or interface
113 			int index = name.indexOf(";");
114 			name = name.substring(1, index);
115 			try {
116 				return Class.forName(name);
117 			} catch (ClassNotFoundException e) {
118 				e.printStackTrace();
119 				return null;
120 			}
121 		}
122 		return null;
123 	}			   
124 	
125 	public static Class getArrayType(Object obj) {
126 		return getArrayType(obj.getClass());
127 	}			   
128 	
129 	protected static void checkLength(int len1, int len2) throws IndexOutOfBoundsException {
130 		if (len1 != len2) {
131 			throw new IndexOutOfBoundsException(len1 + " != " + len2);
132 		}
133 	}
134 }
135