Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
07ec4017
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
07ec4017
编写于
10月 23, 2009
作者:
D
darcy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6891113: More methods for java.util.Objects: deepEquals, hash, toString with default
Reviewed-by: alanb, gafter
上级
7df8e5e8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
157 addition
and
23 deletion
+157
-23
src/share/classes/java/util/Arrays.java
src/share/classes/java/util/Arrays.java
+28
-21
src/share/classes/java/util/Objects.java
src/share/classes/java/util/Objects.java
+74
-1
test/java/util/Objects/BasicObjectsTest.java
test/java/util/Objects/BasicObjectsTest.java
+55
-1
未找到文件。
src/share/classes/java/util/Arrays.java
浏览文件 @
07ec4017
...
@@ -3928,6 +3928,7 @@ public class Arrays {
...
@@ -3928,6 +3928,7 @@ public class Arrays {
* @param a2 the other array to be tested for equality
* @param a2 the other array to be tested for equality
* @return <tt>true</tt> if the two arrays are equal
* @return <tt>true</tt> if the two arrays are equal
* @see #equals(Object[],Object[])
* @see #equals(Object[],Object[])
* @see Objects#deepEquals(Object, Object)
* @since 1.5
* @since 1.5
*/
*/
public
static
boolean
deepEquals
(
Object
[]
a1
,
Object
[]
a2
)
{
public
static
boolean
deepEquals
(
Object
[]
a1
,
Object
[]
a2
)
{
...
@@ -3949,27 +3950,7 @@ public class Arrays {
...
@@ -3949,27 +3950,7 @@ public class Arrays {
return
false
;
return
false
;
// Figure out whether the two elements are equal
// Figure out whether the two elements are equal
boolean
eq
;
boolean
eq
=
deepEquals0
(
e1
,
e2
);
if
(
e1
instanceof
Object
[]
&&
e2
instanceof
Object
[])
eq
=
deepEquals
((
Object
[])
e1
,
(
Object
[])
e2
);
else
if
(
e1
instanceof
byte
[]
&&
e2
instanceof
byte
[])
eq
=
equals
((
byte
[])
e1
,
(
byte
[])
e2
);
else
if
(
e1
instanceof
short
[]
&&
e2
instanceof
short
[])
eq
=
equals
((
short
[])
e1
,
(
short
[])
e2
);
else
if
(
e1
instanceof
int
[]
&&
e2
instanceof
int
[])
eq
=
equals
((
int
[])
e1
,
(
int
[])
e2
);
else
if
(
e1
instanceof
long
[]
&&
e2
instanceof
long
[])
eq
=
equals
((
long
[])
e1
,
(
long
[])
e2
);
else
if
(
e1
instanceof
char
[]
&&
e2
instanceof
char
[])
eq
=
equals
((
char
[])
e1
,
(
char
[])
e2
);
else
if
(
e1
instanceof
float
[]
&&
e2
instanceof
float
[])
eq
=
equals
((
float
[])
e1
,
(
float
[])
e2
);
else
if
(
e1
instanceof
double
[]
&&
e2
instanceof
double
[])
eq
=
equals
((
double
[])
e1
,
(
double
[])
e2
);
else
if
(
e1
instanceof
boolean
[]
&&
e2
instanceof
boolean
[])
eq
=
equals
((
boolean
[])
e1
,
(
boolean
[])
e2
);
else
eq
=
e1
.
equals
(
e2
);
if
(!
eq
)
if
(!
eq
)
return
false
;
return
false
;
...
@@ -3977,6 +3958,32 @@ public class Arrays {
...
@@ -3977,6 +3958,32 @@ public class Arrays {
return
true
;
return
true
;
}
}
static
boolean
deepEquals0
(
Object
e1
,
Object
e2
)
{
assert
e1
!=
null
;
boolean
eq
;
if
(
e1
instanceof
Object
[]
&&
e2
instanceof
Object
[])
eq
=
deepEquals
((
Object
[])
e1
,
(
Object
[])
e2
);
else
if
(
e1
instanceof
byte
[]
&&
e2
instanceof
byte
[])
eq
=
equals
((
byte
[])
e1
,
(
byte
[])
e2
);
else
if
(
e1
instanceof
short
[]
&&
e2
instanceof
short
[])
eq
=
equals
((
short
[])
e1
,
(
short
[])
e2
);
else
if
(
e1
instanceof
int
[]
&&
e2
instanceof
int
[])
eq
=
equals
((
int
[])
e1
,
(
int
[])
e2
);
else
if
(
e1
instanceof
long
[]
&&
e2
instanceof
long
[])
eq
=
equals
((
long
[])
e1
,
(
long
[])
e2
);
else
if
(
e1
instanceof
char
[]
&&
e2
instanceof
char
[])
eq
=
equals
((
char
[])
e1
,
(
char
[])
e2
);
else
if
(
e1
instanceof
float
[]
&&
e2
instanceof
float
[])
eq
=
equals
((
float
[])
e1
,
(
float
[])
e2
);
else
if
(
e1
instanceof
double
[]
&&
e2
instanceof
double
[])
eq
=
equals
((
double
[])
e1
,
(
double
[])
e2
);
else
if
(
e1
instanceof
boolean
[]
&&
e2
instanceof
boolean
[])
eq
=
equals
((
boolean
[])
e1
,
(
boolean
[])
e2
);
else
eq
=
e1
.
equals
(
e2
);
return
eq
;
}
/**
/**
* Returns a string representation of the contents of the specified array.
* Returns a string representation of the contents of the specified array.
* The string representation consists of a list of the array's elements,
* The string representation consists of a list of the array's elements,
...
...
src/share/classes/java/util/Objects.java
浏览文件 @
07ec4017
...
@@ -33,7 +33,7 @@ package java.util;
...
@@ -33,7 +33,7 @@ package java.util;
*
*
* @since 1.7
* @since 1.7
*/
*/
public
class
Objects
{
public
final
class
Objects
{
private
Objects
()
{
private
Objects
()
{
throw
new
AssertionError
(
"No java.util.Objects instances for you!"
);
throw
new
AssertionError
(
"No java.util.Objects instances for you!"
);
}
}
...
@@ -57,6 +57,32 @@ public class Objects {
...
@@ -57,6 +57,32 @@ public class Objects {
return
(
a
==
b
)
||
(
a
!=
null
&&
a
.
equals
(
b
));
return
(
a
==
b
)
||
(
a
!=
null
&&
a
.
equals
(
b
));
}
}
/**
* Returns {@code true} if the arguments are deeply equal to each other
* and {@code false} otherwise.
*
* Two {@code null} values are deeply equal. If both arguments are
* arrays, the algorithm in {@link Arrays#deepEquals(Object[],
* Object[]) Arrays.deepEquals} is used to determine equality.
* Otherwise, equality is determined by using the {@link
* Object#equals equals} method of the first argument.
*
* @param a an object
* @param b an object to be compared with {@code a} for deep equality
* @return {@code true} if the arguments are deeply equal to each other
* and {@code false} otherwise
* @see Arrays#deepEquals(Object[], Object[])
* @see Objects#equals(Object, Object)
*/
public
static
boolean
deepEquals
(
Object
a
,
Object
b
)
{
if
(
a
==
b
)
return
true
;
else
if
(
a
==
null
||
b
==
null
)
return
false
;
else
return
Arrays
.
deepEquals0
(
a
,
b
);
}
/**
/**
* Returns the hash code of a non-{@code null} argument and 0 for
* Returns the hash code of a non-{@code null} argument and 0 for
* a {@code null} argument.
* a {@code null} argument.
...
@@ -70,6 +96,36 @@ public class Objects {
...
@@ -70,6 +96,36 @@ public class Objects {
return
o
!=
null
?
o
.
hashCode
()
:
0
;
return
o
!=
null
?
o
.
hashCode
()
:
0
;
}
}
/**
* Generates a hash code for a sequence of input values. The hash
* code is generated as if all the input values were placed into an
* array, and that array were hashed by calling {@link
* Arrays#hashCode(Object[])}.
*
* <p>This method is useful for implementing {@link
* Object#hashCode()} on objects containing multiple fields. For
* example, if an object that has three fields, {@code x}, {@code
* y}, and {@code z}, one could write:
*
* <blockquote><pre>
* @Override public int hashCode() {
* return Objects.hash(x, y, z);
* }
* </pre></blockquote>
*
* <b>Warning: When a single object reference is supplied, the returned
* value does not equal the hash code of that object reference.</b> This
* value can be computed by calling {@link #hashCode(Object)}.
*
* @param values the values to be hashed
* @return a hash value of the sequence of input values
* @see Arrays#hashCode
* @see List#hashCode
*/
public
static
int
hash
(
Object
...
values
)
{
return
Arrays
.
hashCode
(
values
);
}
/**
/**
* Returns the result of calling {@code toString} for a non-{@code
* Returns the result of calling {@code toString} for a non-{@code
* null} argument and {@code "null"} for a {@code null} argument.
* null} argument and {@code "null"} for a {@code null} argument.
...
@@ -84,6 +140,23 @@ public class Objects {
...
@@ -84,6 +140,23 @@ public class Objects {
return
String
.
valueOf
(
o
);
return
String
.
valueOf
(
o
);
}
}
/**
* Returns the result of calling {@code toString} on the first
* argument if the first argument is not {@code null} and returns
* the second argument otherwise.
*
* @param o an object
* @param nullDefault string to return if the first argument is
* {@code null}
* @return the result of calling {@code toString} on the first
* argument if it is not {@code null} and the second argument
* otherwise.
* @see Objects#toString(Object)
*/
public
static
String
toString
(
Object
o
,
String
nullDefault
)
{
return
(
o
!=
null
)
?
o
.
toString
()
:
nullDefault
;
}
/**
/**
* Returns 0 if the arguments are identical and {@code
* Returns 0 if the arguments are identical and {@code
* c.compare(a, b)} otherwise.
* c.compare(a, b)} otherwise.
...
...
test/java/util/Objects/BasicObjectsTest.java
浏览文件 @
07ec4017
...
@@ -23,7 +23,7 @@
...
@@ -23,7 +23,7 @@
/*
/*
* @test
* @test
* @bug 6797535
* @bug 6797535
6889858 6891113
* @summary Basic tests for methods in java.util.Objects
* @summary Basic tests for methods in java.util.Objects
* @author Joseph D. Darcy
* @author Joseph D. Darcy
*/
*/
...
@@ -34,8 +34,11 @@ public class BasicObjectsTest {
...
@@ -34,8 +34,11 @@ public class BasicObjectsTest {
public
static
void
main
(
String
...
args
)
{
public
static
void
main
(
String
...
args
)
{
int
errors
=
0
;
int
errors
=
0
;
errors
+=
testEquals
();
errors
+=
testEquals
();
errors
+=
testDeepEquals
();
errors
+=
testHashCode
();
errors
+=
testHashCode
();
errors
+=
testHash
();
errors
+=
testToString
();
errors
+=
testToString
();
errors
+=
testToString2
();
errors
+=
testCompare
();
errors
+=
testCompare
();
errors
+=
testNonNull
();
errors
+=
testNonNull
();
if
(
errors
>
0
)
if
(
errors
>
0
)
...
@@ -60,6 +63,36 @@ public class BasicObjectsTest {
...
@@ -60,6 +63,36 @@ public class BasicObjectsTest {
return
errors
;
return
errors
;
}
}
private
static
int
testDeepEquals
()
{
int
errors
=
0
;
Object
[]
values
=
{
null
,
null
,
// Change to values later
new
byte
[]
{(
byte
)
1
},
new
short
[]
{(
short
)
1
},
new
int
[]
{
1
},
new
long
[]
{
1L
},
new
char
[]
{(
char
)
1
},
new
float
[]
{
1.0f
},
new
double
[]{
1.0d
},
new
String
[]{
"one"
}};
values
[
1
]
=
values
;
for
(
int
i
=
0
;
i
<
values
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
values
.
length
;
j
++)
{
boolean
expected
=
(
i
==
j
);
Object
a
=
values
[
i
];
Object
b
=
values
[
j
];
boolean
result
=
Objects
.
deepEquals
(
a
,
b
);
if
(
result
!=
expected
)
{
errors
++;
System
.
err
.
printf
(
"When equating %s to %s, got %b instead of %b%n."
,
a
,
b
,
result
,
expected
);
}
}
return
errors
;
}
private
static
int
testHashCode
()
{
private
static
int
testHashCode
()
{
int
errors
=
0
;
int
errors
=
0
;
errors
+=
(
Objects
.
hashCode
(
null
)
==
0
)
?
0
:
1
;
errors
+=
(
Objects
.
hashCode
(
null
)
==
0
)
?
0
:
1
;
...
@@ -68,6 +101,19 @@ public class BasicObjectsTest {
...
@@ -68,6 +101,19 @@ public class BasicObjectsTest {
return
errors
;
return
errors
;
}
}
private
static
int
testHash
()
{
int
errors
=
0
;
Object
[]
data
=
new
String
[]{
"perfect"
,
"ham"
,
"THC"
};
errors
+=
((
Objects
.
hash
((
Object
[])
null
)
==
0
)
?
0
:
1
);
errors
+=
(
Objects
.
hash
(
"perfect"
,
"ham"
,
"THC"
)
==
Arrays
.
hashCode
(
data
))
?
0
:
1
;
return
errors
;
}
private
static
int
testToString
()
{
private
static
int
testToString
()
{
int
errors
=
0
;
int
errors
=
0
;
errors
+=
(
"null"
.
equals
(
Objects
.
toString
(
null
))
)
?
0
:
1
;
errors
+=
(
"null"
.
equals
(
Objects
.
toString
(
null
))
)
?
0
:
1
;
...
@@ -76,6 +122,14 @@ public class BasicObjectsTest {
...
@@ -76,6 +122,14 @@ public class BasicObjectsTest {
return
errors
;
return
errors
;
}
}
private
static
int
testToString2
()
{
int
errors
=
0
;
String
s
=
"not the default"
;
errors
+=
(
s
.
equals
(
Objects
.
toString
(
null
,
s
))
)
?
0
:
1
;
errors
+=
(
s
.
equals
(
Objects
.
toString
(
s
,
"another string"
))
)
?
0
:
1
;
return
errors
;
}
private
static
int
testCompare
()
{
private
static
int
testCompare
()
{
int
errors
=
0
;
int
errors
=
0
;
String
[]
values
=
{
"e. e. cummings"
,
"zzz"
};
String
[]
values
=
{
"e. e. cummings"
,
"zzz"
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录