/*
* Class DataType
*
* USAGE: Methods for determining object type
*
*
* WRITTEN BY: Dr Michael Thomas Flanagan
*
* DATE: 8-12 April 2012
* AMENDED:
*
* DOCUMENTATION:
* See Michael Thomas Flanagan's Java library on-line web pages:
* http://www.ee.ucl.ac.uk/~mflanaga/java/
* http://www.ee.ucl.ac.uk/~mflanaga/java/DataType.html
*
* Copyright (c) 2012
*
* PERMISSION TO COPY:
* Permission to use, copy and modify this software and its documentation for
* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
*
* Dr Michael Thomas Flanagan makes no representations about the suitability
* or fitness of the software for any or for a particular purpose.
* Michael Thomas Flanagan shall not be liable for any damages suffered
* as a result of using, modifying or distributing this software or its derivatives.
*
***************************************************************************************/
package flanagan.math;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ArrayDeque;
import java.util.Vector;
import java.math.BigDecimal;
import java.math.BigInteger;
public class DataType{
private final String[] typeList = {"String", "BigDecimal", "BigInteger", "Double", "Float", "Long", "Integer", "Character", "Short", "Byte", "Boolean", "Object", "ArrayList", "LinkedList", "ArrayDeque", "Vector", "Complex", "Phasor", "ErrorProp", "ComplexErrorProp", "Matrix", "ComplexMatrix", "PhasorMatrix", "Polynomial", "ComplexPoly", "ArrayMaths", "VectorMaths", "Point", "BlackBox", "OpenLoop", "ClosedLoop", "Prop", "PropDeriv", "PropInt", "PropIntDeriv", "FirstOrder", "SecondOrder", "Compensator", "LowPassPassive", "HighPassPassive", "DelayLine", "ZeroOrderHold", "Transducer","AtoD", "DtoA"};
private final int nTypes = this.typeList.length;
private final int nNumerical = 9; // number of numerical types for inclusion in highest precision replacement
private Object obj = null; // entered Object
private String objTypeName = null; // Object type name
private int objTypeDim = 0; // number of dimensions of an Objects arrays
private int objTypeCode = -1; // object type code
private boolean typeFound = false; // = true when Object type name, code and dimensions found
private Object[] arrayObjects = null; // array Objects
private String[] arrayNames = null; // array type names
private int[] arrayDims = null; // array numbers of dimensions
private int[] arrayCodes = null; // array codes
private int nArrayElements = 0; // number of array elements
private boolean arrayDone = false; // = true when an array analysed
private int arrayFlag = -1; // array flag
// = 0: object does not contain an array, i.e. is not an ArrayList, ArrayDeque, LinkedList or Vector
// = 1: Object contains an array, e.g is ArrayList, and the number of dimensions = 0 and all elements are numerically interconvertable
// = 2: Object contains an array, e.g is ArrayList, and the number of dimensions > 0
// = 3: Object contains an array but not all elements are numerically interconvertable
private boolean highestDone = false; // = true whwn highest precision name found
private String highestName = null; // highest precision name
private int highestCode = -1; // highest precision code
private Object highestArray = null; // highest precision converted array
// CONSTRUCTORS
public DataType(){
}
public DataType(Object obj){
this.obj = obj;
this.inner();
}
// TYPE NAME
// Type name returned as name or name[]...
// Instance method
public final String getTypeName(){
if(!this.typeFound)this.inner();
return this.objTypeName;
}
// Static method
public static String getTypeName(Object obj){
DataType objType = new DataType(obj);
return objType.getTypeName();
}
// private method -> type name code and dimension
private void inner(){
this.arrayFlag = 0;
String fullname = obj.getClass().getName();
// Dimension
boolean test = true;
this.objTypeDim = 0;
int pos0 = -1;
int pos1 = 0;
while(test){
pos0 = fullname.indexOf("[", pos1);
if(pos0==-1){
test = false;
}
else{
pos1 = pos0+1;
this.objTypeDim++;
}
}
// Type Name
this.objTypeName = null;
test = true;
while(test){
pos0 = fullname.lastIndexOf(".");
this.objTypeName = fullname.substring(pos0+1);
if(fullname.charAt(fullname.length()-1)==';')this.objTypeName = this.objTypeName.substring(0,this.objTypeName.length()-1);
test = false;
}
for(int j=0;j=this.nTypes){
String rearrName = null;
if(this.objTypeDim==0){
rearrName = fullname;
}
else{
rearrName = fullname.substring(this.objTypeDim+1, fullname.length()-1);
}
System.out.println("Method Object.getTypeCode: the object, " + rearrName + ", is not included in the Conv class list of objects, -1 returned");
test = false;
}
}
}
this.arrayInner();
this.typeFound = true;
}
// private method -> array names, codes and dimensions
private void arrayInner(){
this.arrayNames = null;
this.arrayCodes = null;
this.arrayDims = null;
if(this.objTypeName.equals("ArrayList")){
this.arrayFlag = 1;
ArrayList al = (ArrayList)this.obj;
this.nArrayElements = al.size();
this.arrayObjects = al.toArray();
this.fillArray();
}
else{
if(this.objTypeName.equals("ArrayDeque")){
this.arrayFlag = 1;
ArrayDeque ad = (ArrayDeque)this.obj;
this.nArrayElements = ad.size();
this.arrayObjects = ad.toArray();
this.fillArray();
}
else{
if(this.objTypeName.equals("LinkedList")){
this.arrayFlag = 1;
LinkedList ll = (LinkedList)this.obj;
this.nArrayElements = ll.size();
this.arrayObjects = ll.toArray();
this.fillArray();
}
else{
if(this.objTypeName.equals("Vector")){
this.arrayFlag = 1;
Vector vv = (Vector)this.obj;
this.nArrayElements = vv.size();
this.arrayObjects = vv.toArray();
this.fillArray();
}
else{
if(this.objTypeName.equals("Object[]")){
this.arrayFlag = 1;
this.arrayObjects = (Object[])this.obj;
this.nArrayElements = this.arrayObjects.length;
this.fillArray();
}
}
}
}
}
this.arrayDone = true;
}
private void fillArray(){
this.arrayNames = new String[this.nArrayElements];
this.arrayCodes = new int[this.nArrayElements];
this.arrayDims = new int[this.nArrayElements];
Object[] retd = null;
for(int i=0; i0){
this.arrayFlag = 2;
}
}
else{
this.arrayFlag = 3;
}
if(this.arrayFlag==2 || this.arrayFlag==3)break;
}
}
// GET TYPE CODE
// Code returned as index of typeList name
// instance method
public final int getTypeCode(){
if(!this.typeFound)this.inner();
return this.objTypeCode;
}
// Static method
public static int getTypeCode(Object obj){
DataType objType = new DataType(obj);
return objType.getTypeCode();
}
// GET NUMBER OF DIMENSIONS
// Code returned as index intof typeList name
public final int getTypeDimension(){
if(!this.typeFound)this.inner();
return this.objTypeDim;
}
// Static method
public static int getTypeDimension(Object obj){
DataType objType = new DataType(obj);
return objType.getTypeDimension();
}
// GET ALL TYPE DATA
// -> type name, dimension, code
public final Object[] getTypeData(){
Object[] ret = new Object[3];
if(!this.typeFound)this.inner();
ret[0] = (Object)this.objTypeName;
ret[1] = (Object)(new Integer(this.objTypeDim));
ret[2] = (Object)(new Integer(this.objTypeCode));
return ret;
}
// Static method
public static Object[] getTypeData(Object obj){
DataType objType = new DataType(obj);
return objType.getTypeData();
}
// ARRAY ELEMENT NAMES
// instant method
public String[] getArrayNames(){
String[] ret = null;
if(!this.arrayDone)this.arrayInner();
switch(this.arrayFlag){
case 0: System.out.println("Method DataType.getArayNames: The entered Object is not a collection - null returned");
break;
case 1: ret = this.arrayNames;
break;
case 2: System.out.println("Method DataType.getArayNames: The entered Object is an array of collections - null returned");
break;
}
return ret;
}
// static method
public static String[] getArrayNames(Object al){
DataType objType = new DataType(al);
return objType.getArrayNames();
}
// ARRAY ELEMENT DIMENSIONS
// instant method
public int[] getArrayDimensions(){
int[] ret = null;
if(!this.arrayDone)this.arrayInner();
switch(this.arrayFlag){
case 0: System.out.println("Method DataType.getArayDimensions: The entered Object is not a collection - null returned");
break;
case 1:
case 2: ret = this.arrayDims;
break;
}
return ret;
}
// static method
public static int[] getArrayDimensions(Object al){
DataType objType = new DataType(al);
return objType.getArrayDimensions();
}
// ARRAY ELEMENT CODES
// instant method
public int[] getArrayCodes(){
int[] ret = null;
if(!this.arrayDone)this.arrayInner();
switch(this.arrayFlag){
case 0: System.out.println("Method DataType.getArrayCodes: The entered Object is not a collection - null returned");
break;
case 1:
case 2: ret = this.arrayCodes;
}
return ret;
}
// static method
public static int[] getArrayCodes(Object al){
DataType objType = new DataType(al);
return objType.getArrayCodes();
}
// ALL ARRAY ELEMENT DATA
// -> type name, dimension, code for all elements
// instant method
public final ArrayList
Ads help maintain this website.