如何正确测试自定义对象中的 char[ ] 字段是否存在空值、空白或空值?为什么 char[ ] 表示长度为 11?
How do I properly test the char[ ] fields in my custom object for nulls, blanks, or empty? and Why do char[ ] indicate a length of 11?
我正在研究如何使用反射来初始化自定义对象。举个例子,
我的对象有几个不同类型的字段,但是 NONE 个字段属于
字符串类型。我使用 chararrays 而不是字符串,因为我想清除对象
如果应用程序崩溃,不用担心堆中存在不可变字符串。
此外,由于 class 实现了 CharSequence,静态字段
buff、offset 和 count 是必需的。另外值得一提的是,getters 和 setters 是
synchronized 因为这个对象将在多线程环境中使用。最后,
有一个名为 People 的自定义 Person 对象列表,它位于自己的 People.java 文件中,并且
是 Person 对象的自定义集合。为简洁起见,省略了有关该对象的其他详细信息。
问题是,如何测试我的 Person 对象中的空字段、空白字段或 null 字段?在我的主要 class,
在将所述对象插入后端数据库之前,我想测试 null、empty 或 blank。我有
尝试创建一个 Person.isNull()
和一个类似的 Person.hasNull()
函数来测试空值
在 char[]
字段中,但结果绝对不是我的预期。里面的println语句
isCharArray 的测试揭示了一些我目前不理解的东西......
这是一些示例输出:
Size of byte: 1 bytes.
Size of short: 2 bytes.
Size of int: 4 bytes.
Size of long: 8 bytes.
Size of char: 2 bytes.
Size of float: 4 bytes.
Size of double: 8 bytes.
buff: char[] 11
personID: char[] 11
personTitle: char[] 11
personFirstName: char[] 11
personLastName: char[] 11
它是使用以下 System.out.println 语句创建的:
System.out.println(f.getName() + ": " + f.getType().getCanonicalName() + " " + String.valueOf(value).length());
并提出了 2 个问题:
- 为什么是 11?!!!!!!
- 如何正确测试空白、空白或零长度字段?!!!
请帮忙....
public class Person implements Serializable,
Comparable<Person>,
CharSequence,
Cloneable { // the object to model
// Fields 0, 1, and 2 are required to implement CharSequence
// http://www.java2s.com/Tutorial/Java/0040__Data-Type/implementsCharSequence.htm
private static char[] buff = {'[=12=]'}; // No static fields are ever // Field 0
private static int offset = 0; // written to file. Their values // Field 1
private static int count = 0; // must be reconstructed. // Field 2
// default serialVersion id
private static final long serialVersionUID = 7891011129876814235L; // Field 3
private final static LocalDateTime rightNow = LocalDateTime.now();
private long localSerialVersionUID= serialVersionUID; //1st field written to file // Field 4
private LocalDateTime personCreatedDateTime= rightNow; // Field 5
private LocalDateTime personLastUpdate = rightNow; // (YYYY-MM-DD) // Field 6
private char[] personID = {'[=12=]'}; // (PK) possibly int auto increment // Field 7
private char[] personTitle = {'[=12=]'}; // Field 8
private char[] personFirstName = {'[=12=]'}; // Field 9
private char[] personLastName = {'[=12=]'}; // Field 10
private LocalDate personDOB = LocalDate.parse("1010-10-10"); // (YYYY-MM-DD) // Field 11
public Person(
final long serialUID, //4
final LocalDateTime createdDateTime, //5
final LocalDateTime lastUpdate, //6
final char[] id, //7
final char[] title, //8
final char[] firstName, //9
final char[] lastName, //10
final LocalDate DOB //11
) {
this.localSerialVersionUID(serialUID); //4
this.personCreatedDateTime(LocalDateTime.now()); //5
this.personLastUpdate(lastUpdate); //6
this.personID(id); //7
this.personTitle(title); //8
this.personFirstName(firstName); //9
this.personLastName(lastName); //10
this.personDOB(DOB); //11
}
public boolean hasNull() {
Field fields[] = this.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
try{
Object value = f.get(this);
if ( value == null) {return true; } // default condition
boolean isCharArray = f.getType().getCanonicalName().equals("char[]" );
// No need to check fields that are not char[]
if (( isCharArray) ) {
// ************* this compiles and executes (doesn't crash), but it doesn't produce intended results *********
System.out.println(f.getName() + ": " + f.getType().getCanonicalName() + " " + String.valueOf(value).length());
if ( String.valueOf(value).length() == 0) {return true;}
if ( String.valueOf(value).isEmpty()) {return true;}
if ( String.valueOf(value).isBlank()) {return true;}
if ( String.valueOf(value).equals('[=12=]')) {return true;}
}
// ************* this compiles and executes (doesn't crash), but it doesn't produce intended results *********
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}
//enter code here
/**
* @param args
*/
public static void main(String[] args) {
People<Person> people = new People()<>; //customized list of person objects
People<Person> people2 = new People()<>; //customized list of person objects
Person thomas = new Person().personFirstName("Thomas".toCharArray());
// all other char array fields = {'[=12=]'}
people.add(thomas);
Person sybil = new Person().personFirstName("Sybil".toCharArray());
// all other char array fields = {'[=12=]'}
people2.add(sybil);
if (!thomas.hasNull() ){ // ******* currently this test fails and a Person gets inserted to the People list
People.insertPerson(thomas);
}
if (!sybil.hasNull() ){ // ******* currently this test fails and a Person gets inserted to the People list
People.insertPerson(sybil);
}
}
}
String.valueOf().length() returns 11 因为这是输出中 digits/characters 的数量(即 [@936016386 - 一个地址 - 有 11 个字符)
我正在研究如何使用反射来初始化自定义对象。举个例子, 我的对象有几个不同类型的字段,但是 NONE 个字段属于 字符串类型。我使用 chararrays 而不是字符串,因为我想清除对象 如果应用程序崩溃,不用担心堆中存在不可变字符串。
此外,由于 class 实现了 CharSequence,静态字段 buff、offset 和 count 是必需的。另外值得一提的是,getters 和 setters 是 synchronized 因为这个对象将在多线程环境中使用。最后, 有一个名为 People 的自定义 Person 对象列表,它位于自己的 People.java 文件中,并且 是 Person 对象的自定义集合。为简洁起见,省略了有关该对象的其他详细信息。
问题是,如何测试我的 Person 对象中的空字段、空白字段或 null 字段?在我的主要 class,
在将所述对象插入后端数据库之前,我想测试 null、empty 或 blank。我有
尝试创建一个 Person.isNull()
和一个类似的 Person.hasNull()
函数来测试空值
在 char[]
字段中,但结果绝对不是我的预期。里面的println语句
isCharArray 的测试揭示了一些我目前不理解的东西......
这是一些示例输出:
Size of byte: 1 bytes.
Size of short: 2 bytes.
Size of int: 4 bytes.
Size of long: 8 bytes.
Size of char: 2 bytes.
Size of float: 4 bytes.
Size of double: 8 bytes.
buff: char[] 11
personID: char[] 11
personTitle: char[] 11
personFirstName: char[] 11
personLastName: char[] 11
它是使用以下 System.out.println 语句创建的:
System.out.println(f.getName() + ": " + f.getType().getCanonicalName() + " " + String.valueOf(value).length());
并提出了 2 个问题:
- 为什么是 11?!!!!!!
- 如何正确测试空白、空白或零长度字段?!!!
请帮忙....
public class Person implements Serializable,
Comparable<Person>,
CharSequence,
Cloneable { // the object to model
// Fields 0, 1, and 2 are required to implement CharSequence
// http://www.java2s.com/Tutorial/Java/0040__Data-Type/implementsCharSequence.htm
private static char[] buff = {'[=12=]'}; // No static fields are ever // Field 0
private static int offset = 0; // written to file. Their values // Field 1
private static int count = 0; // must be reconstructed. // Field 2
// default serialVersion id
private static final long serialVersionUID = 7891011129876814235L; // Field 3
private final static LocalDateTime rightNow = LocalDateTime.now();
private long localSerialVersionUID= serialVersionUID; //1st field written to file // Field 4
private LocalDateTime personCreatedDateTime= rightNow; // Field 5
private LocalDateTime personLastUpdate = rightNow; // (YYYY-MM-DD) // Field 6
private char[] personID = {'[=12=]'}; // (PK) possibly int auto increment // Field 7
private char[] personTitle = {'[=12=]'}; // Field 8
private char[] personFirstName = {'[=12=]'}; // Field 9
private char[] personLastName = {'[=12=]'}; // Field 10
private LocalDate personDOB = LocalDate.parse("1010-10-10"); // (YYYY-MM-DD) // Field 11
public Person(
final long serialUID, //4
final LocalDateTime createdDateTime, //5
final LocalDateTime lastUpdate, //6
final char[] id, //7
final char[] title, //8
final char[] firstName, //9
final char[] lastName, //10
final LocalDate DOB //11
) {
this.localSerialVersionUID(serialUID); //4
this.personCreatedDateTime(LocalDateTime.now()); //5
this.personLastUpdate(lastUpdate); //6
this.personID(id); //7
this.personTitle(title); //8
this.personFirstName(firstName); //9
this.personLastName(lastName); //10
this.personDOB(DOB); //11
}
public boolean hasNull() {
Field fields[] = this.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
try{
Object value = f.get(this);
if ( value == null) {return true; } // default condition
boolean isCharArray = f.getType().getCanonicalName().equals("char[]" );
// No need to check fields that are not char[]
if (( isCharArray) ) {
// ************* this compiles and executes (doesn't crash), but it doesn't produce intended results *********
System.out.println(f.getName() + ": " + f.getType().getCanonicalName() + " " + String.valueOf(value).length());
if ( String.valueOf(value).length() == 0) {return true;}
if ( String.valueOf(value).isEmpty()) {return true;}
if ( String.valueOf(value).isBlank()) {return true;}
if ( String.valueOf(value).equals('[=12=]')) {return true;}
}
// ************* this compiles and executes (doesn't crash), but it doesn't produce intended results *********
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
}
//enter code here
/**
* @param args
*/
public static void main(String[] args) {
People<Person> people = new People()<>; //customized list of person objects
People<Person> people2 = new People()<>; //customized list of person objects
Person thomas = new Person().personFirstName("Thomas".toCharArray());
// all other char array fields = {'[=12=]'}
people.add(thomas);
Person sybil = new Person().personFirstName("Sybil".toCharArray());
// all other char array fields = {'[=12=]'}
people2.add(sybil);
if (!thomas.hasNull() ){ // ******* currently this test fails and a Person gets inserted to the People list
People.insertPerson(thomas);
}
if (!sybil.hasNull() ){ // ******* currently this test fails and a Person gets inserted to the People list
People.insertPerson(sybil);
}
}
}
String.valueOf().length() returns 11 因为这是输出中 digits/characters 的数量(即 [@936016386 - 一个地址 - 有 11 个字符)