Assert和Enumeration的用法

来源:互联网 发布:mt4软件开发 编辑:程序博客网 时间:2024/06/06 15:36

org.springframework.util.Assert
Assert
翻译为中文为"断言".用过JUNIT的应该都知道这个概念了.
就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常.
Assert
经常用于:
1.
判断METHOD的参数是否属于正常值.
2.JUNIT
中使用.


我发现SPRING1.2.6里面有BUG
请看:
org.springframework.core.io.support.EncodedResource

public EncodedResource(Resource resource, String encoding) {
  
Assert.notNull("Resource isrequired");
  this.resource = resource;
  this.encoding = encoding;
}


Assert.notNull("Resource is required");
这句应该为
Assert.notNull(resource,"Resource is required");
不然resource都没传过来,还断什么言啊,呵呵.

 

------------------------------------------------------------------------

上面是在网上看到了,但是我进入spring里面看了一下源码,如下:

/**
* Assert that an object is not <code>null</code> .
* <pre class="code">Assert.notNull(clazz, "The class mustnot be null");</pre>
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is<code>null</code>
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}

该函数的意思是传入的object必须不能为空。如果为空就抛出异常。

 

 

Enumeration(列举)

public interface Enumeration<E>实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。

例如,要输出 Vector<E> v 的所有元素,可使用以下方法:

for (Enumeration<E> e = v.elements();e.hasMoreElements();)

System.out.println(e.nextElement());

这些方法主要通过向量的元素、哈希表的键以及哈希表中的值进行枚举。枚举也用于将输入流指定到 SequenceInputStream 中。

注:此接口的功能与 Iterator 接口的功能是重复的。此外,Iterator接口添加了一个可选的移除操作,并使用较短的方法名。新的实现应该优先考虑使用 Iterator 接口而不是 Enumeration 接口。

有两个方法:

hasMoreElements() 测试此枚举是否包含更多的元素。

nextElement() 如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。

0 0
原创粉丝点击