Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell11
提交
0cf0a27c
D
dragonwell11
项目概览
openanolis
/
dragonwell11
通知
7
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell11
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0cf0a27c
编写于
10月 19, 2009
作者:
D
darcy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
6889858: Add nonNull methods to java.util.Objects
Reviewed-by: darcy Contributed-by: jjb@google.com
上级
a43de921
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
96 addition
and
0 deletion
+96
-0
jdk/src/share/classes/java/util/Objects.java
jdk/src/share/classes/java/util/Objects.java
+46
-0
jdk/test/java/util/Objects/BasicObjectsTest.java
jdk/test/java/util/Objects/BasicObjectsTest.java
+50
-0
未找到文件。
jdk/src/share/classes/java/util/Objects.java
浏览文件 @
0cf0a27c
...
...
@@ -107,4 +107,50 @@ public class Objects {
public
static
<
T
>
int
compare
(
T
a
,
T
b
,
Comparator
<?
super
T
>
c
)
{
return
(
a
==
b
)
?
0
:
c
.
compare
(
a
,
b
);
}
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.nonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public
static
<
T
>
T
nonNull
(
T
obj
)
{
if
(
obj
==
null
)
throw
new
NullPointerException
();
return
obj
;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.nonNull(bar, "bar must not be null");
* this.baz = Objects.nonNull(baz, "baz must not be null");
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public
static
<
T
>
T
nonNull
(
T
obj
,
String
message
)
{
if
(
obj
==
null
)
throw
new
NullPointerException
(
message
);
return
obj
;
}
}
jdk/test/java/util/Objects/BasicObjectsTest.java
浏览文件 @
0cf0a27c
...
...
@@ -37,6 +37,7 @@ public class BasicObjectsTest {
errors
+=
testHashCode
();
errors
+=
testToString
();
errors
+=
testCompare
();
errors
+=
testNonNull
();
if
(
errors
>
0
)
throw
new
RuntimeException
();
}
...
...
@@ -102,4 +103,53 @@ public class BasicObjectsTest {
}
return
errors
;
}
private
static
int
testNonNull
()
{
int
errors
=
0
;
String
s
;
// Test 1-arg variant
try
{
s
=
Objects
.
nonNull
(
"pants"
);
if
(
s
!=
"pants"
)
{
System
.
err
.
printf
(
"1-arg non-null failed to return its arg"
);
errors
++;
}
}
catch
(
NullPointerException
e
)
{
System
.
err
.
printf
(
"1-arg nonNull threw unexpected NPE"
);
errors
++;
}
try
{
s
=
Objects
.
nonNull
(
null
);
System
.
err
.
printf
(
"1-arg nonNull failed to throw NPE"
);
errors
++;
}
catch
(
NullPointerException
e
)
{
// Expected
}
// Test 2-arg variant
try
{
s
=
Objects
.
nonNull
(
"pants"
,
"trousers"
);
if
(
s
!=
"pants"
)
{
System
.
err
.
printf
(
"2-arg nonNull failed to return its arg"
);
errors
++;
}
}
catch
(
NullPointerException
e
)
{
System
.
err
.
printf
(
"2-arg nonNull threw unexpected NPE"
);
errors
++;
}
try
{
s
=
Objects
.
nonNull
(
null
,
"pantaloons"
);
System
.
err
.
printf
(
"2-arg nonNull failed to throw NPE"
);
errors
++;
}
catch
(
NullPointerException
e
)
{
if
(
e
.
getMessage
()
!=
"pantaloons"
)
{
System
.
err
.
printf
(
"2-arg nonNull threw NPE w/ bad detail msg"
);
errors
++;
}
}
return
errors
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录