Commons Lang
Class ObjectUtils.Null
空占位符对象,ObjectUtils提供了静态常量对象NULL。适用场景,Map值占位符
Map<String,Object> mapping = new HashMap<>();
mapping.put("name", "test");
mapping.put("age", ObjectUtils.NULL);
System.out.println(ObjectUtils.NULL == mapping.get("age"));
System.out.println(ObjectUtils.NULL == mapping.get("class"));
// true
// false
EnumUtils 快捷操作枚举类
public static void main(String[] args) {
Tenum spring = EnumUtils.getEnumIgnoreCase(Tenum.class, "spring");
System.out.println(spring);
System.out.println(EnumUtils.<Tenum>getEnumList(Tenum.class).toString());
System.out.println(EnumUtils.getEnumMap(Tenum.class).toString());
System.out.println(EnumUtils.isValidEnumIgnoreCase(Tenum.class, "summer"));
}
enum Tenum {
Spring("春"), Winter("冬");
private String text;
Tenum(String text) {
this.text = text;
}
@Override
public String toString() {
return this.text;
}
}
春
[春, 冬]
{Spring=春, Winter=冬}
false
BooleanUtils
BooleanUtils.and(true, false) = false
BooleanUtils.and(true, true, false) = false
BooleanUtils.and(true, true, true) = true
BooleanUtils.toStringYesNo(true) = "yes"
BooleanUtils.toStringYesNo(false) = "no"
BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1
BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
BooleanUtils.toInteger(null, 1, 0, 2) = 2
builder包 Interface Builder
为Builder Design Pattern而设计,例如:
class FontBuilder implements Builder<Font> {
private Font font;
public FontBuilder(String fontName) {
this.font = new Font(fontName, Font.PLAIN, 12);
}
public FontBuilder bold() {
this.font = this.font.deriveFont(Font.BOLD);
return this;
}
public FontBuilder size(float pointSize) {
this.font = this.font.deriveFont(pointSize);
return this;
}
public Font build() {
return this.font;
}
}
Font bold14ptSansSerifFont = new FontBuilder(Font.SANS_SERIF).bold()
.size(14.0f)
.build();
builder包里面提供一部分Builder类,如EqualsBuilder
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) {
return false;
}
MyClass rhs = (MyClass) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(field1, rhs.field1)
.append(field2, rhs.field2)
.append(field3, rhs.field3)
.isEquals();
}
或者
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
甚至排除部分字段
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o, Arrays.asList("price"));
}
event包 classs EventListenerSupport
定义事件监听器接口
public interface PublishListener {
void published(PublishEvent e);
}
定义事件
public class PublishEvent {
private Publisher publisher;
private String bookName;
public PublishEvent(Publisher publisher, String bookName) {
this.publisher = publisher;
this.bookName = bookName;
}
@Override
public String toString() {
return publisher.toString() + "发布了书籍: " + bookName;
}
}
定义读者
public class BookReader {
private String name;
public BookReader(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
定义事件监听器
public class NormalPublishListener implements PublishListener {
public BookReader reader;
public NormalPublishListener(BookReader reader) {
this.reader = reader;
}
public void published(PublishEvent e) {
System.out.println(reader + "接收到消息:" + e.toString());
}
}
定义出版商
public class Publisher {
private String name;
public Publisher(String name) {
this.name = name;
}
private EventListenerSupport<PublishListener> actionListeners = EventListenerSupport.create(PublishListener.class);
public void publish(String bookName) {
PublishEvent e = new PublishEvent(this, bookName);
actionListeners.fire().published(e);
}
public void addListener(PublishListener listener) {
actionListeners.addListener(listener);
}
@Override
public String toString() {
return name;
}
}
测试
Publisher publisher = new Publisher("北凉");
publisher.addListener(new NormalPublishListener(new BookReader("剑九黄")));
publisher.addListener(new NormalPublishListener(new BookReader("王仙芝")));
publisher.publish("徐凤年绝断前三世");
测试结果
剑九黄接收到消息:北凉发布了书籍: 徐凤年绝断前三世
王仙芝接收到消息:北凉发布了书籍: 徐凤年绝断前三世
exception包class ContextedException
try {
...
} catch (Exception e) {
throw new ContextedException("Error posting account transaction", e)
.addContextValue("Account Number", accountNumber)
.addContextValue("Amount Posted", amountPosted)
.addContextValue("Previous Balance", previousBalance)
}
打印结果示例如下:
org.apache.commons.lang3.exception.ContextedException: java.lang.Exception: Error posting account transaction
Exception Context:
[1:Account Number=null]
[2:Amount Posted=100.00]
[3:Previous Balance=-2.17]
[4:Transaction Id=94ef1d15-d443-46c4-822b-637f26244899]
Commons BeanUtils
class PropertyUtils
Book book = new Book("Java","author", 100);
BookVo bookVo = new BookVo();
PropertyUtils.copyProperties(bookVo, book);
System.out.println(bookVo.getName() + "-"+ bookVo.getAuthor());
//Java-author
Map<String,Object> map = new HashMap<String, Object>();
map.put("name","Java");
map.put("author","Author");
BookVo bookVo = new BookVo();
PropertyUtils.copyProperties(bookVo, map);
System.out.println(bookVo.getName() + "-"+ bookVo.getAuthor());
//Java-Author
Interface DynaBean
Class LazyDynaBean
简单属性:
DynaBean myBean = new LazyDynaBean();
myBean.set("myProperty", "myValue");
索引属性(数组):
DynaBean myBean = new LazyDynaBean();
myBean.set("myIndexedProperty", 0, "myValue1");
myBean.set("myIndexedProperty", 1, "myValue2");
mapped 属性:
DynaBean myBean = new LazyDynaBean();
MutableDynaClass myClass = (MutableDynaClass)myBean.getDynaClass();
myClass.add("myMappedProperty", TreeMap.class);
myBean.set("myMappedProperty", "myKey", "myValue");
Commons Collections
迭代Map
IterableMap map = new HashedMap();
MapIterator it = map.mapIterator();
while (it.hasNext()) {
Object key = it.next();
Object value = it.getValue();
it.setValue(newValue);
}
Ordered Maps
OrderedMap map = new LinkedMap();
map.put("FIVE", "5");
map.put("SIX", "6");
map.put("SEVEN", "7");
map.firstKey(); // returns "FIVE"
map.nextKey("FIVE"); // returns "SIX"
map.nextKey("SIX"); // returns "SEVEN"
Bidirectional Maps
也可以通过value访问key
BidiMap bidi = new TreeBidiMap();
bidi.put("SIX", "6");
bidi.get("SIX"); // returns "6"
bidi.getKey("6"); // returns "SIX"
bidi.removeValue("6"); // removes the mapping
BidiMap inverse = bidi.inverseBidiMap(); // returns a map with keys and values swapped
Bags
提供元素的统计功能
Bag bag = new HashBag();
bag.add("ONE", 6); // add 6 copies of "ONE"
bag.remove("ONE", 2); // removes 2 copies of "ONE"
bag.getCount("ONE"); // returns 4, the number of copies in the bag (6 - 2)