java jodd_Jodd :一款优雅的 Java 工具集

原标题:Jodd :一款优雅的 Java 工具集

来源:coderknock,

coderknock.com/blog/2016/07/02/Jodd.html

为大家介绍一个几乎全能的Java类库——Jodd【文章示例使用3.7.1版本的jodd】

Jodd公用工具

BeanUtil 最快的bean处理库。

一个简单的JavaBean:

public class Foo {

private String readwrite; // 提供 getter 和 setter

private String readonly; // 只提供getter

... //省略掉个getter和setter

}

使用BeanUtil进行操作:

import jodd.bean.BeanUtil;

public class TestFoo {

Foo foo = new Foo();

BeanUtil.pojo.setProperty(foo, "readwrite", "readwritedata");

System.out.println(BeanUtil.pojo.getProperty(foo, "readwrite").toString());

BeanUtil.declared.setProperty(foo, "readonly", "readonlydata");

System.out.println(foo.getReadonly());

}

}

输出结果:

readwritedata

readonlydata

注意:如果直接对没有setter的readonly属性使用BeanUtil.pojo.setProperty(foo, “readonly”, “readonlydata”);则会报错:

Exception in thread "main" jodd.bean.BeanException: Simple property not found: readonly. Invalid property: Foo#readonly (Foo#readonly)

at jodd.bean.BeanUtilBean.setSimpleProperty(BeanUtilBean.java:222)

at jodd.bean.BeanUtilBean._setIndexProperty(BeanUtilBean.java:408)

at jodd.bean.BeanUtilBean.setIndexProperty(BeanUtilBean.java:400)

at jodd.bean.BeanUtilBean.setProperty(BeanUtilBean.java:475)

at bean.TestFoo.main(TestFoo.java:14)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Cache【目前似乎还没有完全开发完成】 组通用缓存实现。

Printf 为Java提供像C一样格式化值打印。

Printf.str("%+i", 173); // +173

Printf.str("%04d", 1); // 0001

Printf.str("%f", 1.7); // 1.700000

Printf.str("%1.1f", 1.7); // 1.7

Printf.str("%.4e", 100.1e10); // 1.0010e+012

Printf.str("%G", 1.1e13); // 1.1E+013

Printf.str("%l", true); // true

Printf.str("%L", 123); // TRUE

Printf.str("%b", 13); // 1101

Printf.str("%,b", -13); // 11111111 11111111 11111111 11110011

Printf.str("%#X", 173); // 0XAD

Printf.str("%,x", -1); // ffff ffff

Printf.str("%s %s", new String[]{"one", "two"}); // one two

JDateTime 集优雅与最大限度的精确为一体的时间处理库 【使用教程】

http://coderknock.com/blog/2016/07/14/JDateTime.html

Type Converter 方便高效的类型转换器。

StringUtil 超过100个额外字符串工具方法。

StringTemplateParser 简单的字符串模板解析器。

import jodd.util.StringTemplateParser;

import java.util.HashMap;

import java.util.Map;

public class JoddStringTemplateParser {

// prepare template

String template = "Hello ${foo}. Today is ${dayName}.";

// prepare data

Map map = new HashMap();

map.put("foo", "Jodd");

map.put("dayName", "Sunday");

// parse

StringTemplateParser stp = new StringTemplateParser();

String result = stp.parse(template, new StringTemplateParser.MacroResolver() {

public String resolve(String macroName) {

return map.get(macroName);

}

});

// result == "Hello Jodd. Today is Sunday."

}

}

搜索、扫描、遍历文件的一些简单的方法。

Class finder 在classpath中快速找到对应的类。

Wildcard 在Java中便捷的使用通配符。

Wildcard.match("CfgOptions.class", "*C*g*cl*"); // true

Wildcard.match("CfgOptions.class", "*g*c**s"); // true

Wildcard.match("CfgOptions.class", "??gOpti*c?ass"); // true

Wildcard.match("CfgOpti*class", "*gOpti*class"); // true

Wildcard.match("CfgOptions.class", "C*ti*c?a?*"); // true

Wildcard.matchPath("/foo/soo/doo/boo", "/**/bo*"); // true

Wildcard.matchPath("/foo/one/two/three/boo", "**/t?o/**"); // true

Servlets 各种与Servlet相关的工具集。

Jodd tag library 为JSP提供很多高效实用的标签。

Form tag 使用一个简单的标签为页面提供自动填充表单的功能。

Class loading in Jodd 为加载类提供一个更好的方法。

Fast buffers 提供比StringBuilder更高效的字符串缓冲处理类。

import jodd.util.buffer.FastCharBuffer;

public class JoddFastCharBuffer {

long now = System.currentTimeMillis();

FastCharBuffer fb = new FastCharBuffer();

for (int i = 0; i < 1000000; i++) {

fb.append("测试一下性能");

}

System.out.println((System.currentTimeMillis() - now));

now = System.currentTimeMillis();

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 1000000; i++) {

sb.append(i + "");

}

System.out.println((System.currentTimeMillis() - now));

}

}

上面的运行示例,我机器测试结果FastCharBuffer为31毫秒而StringBuilder为101毫秒。

Include-Exclude rules 一款小型的用于过滤资源的规则引擎。

import jodd.util.InExRules;

public class JoddFastCharBuffer {

InExRules inExRules = new InExRules<>();

//白名单

inExRules.include("shelf.book.*");

//黑名单

inExRules.exclude("shelf.book.page.1");

System.out.println(inExRules.match("shelf.book.page.1"));//false

System.out.println(inExRules.match("shelf.book"));//true

System.out.println(inExRules.match("shelf.book.page.34"));//true

//通配符可以使用*通配符

// InExRules WildcardInExRules =

// new InExRules(

// InExRuleMatcher.WILDCARD_RULE_MATCHER);

}

}

Dir Watcher 提供一个对目录的监控,可以在目录中文件发生变化时进行一些特定的处理。

import jodd.io.watch.DirWatcher;

import jodd.io.watch.DirWatcherListener;

import java.io.File;

public class JoddWatcher {

//只有这个watch.txt文件修改时才触发

// DirWatcher dirWatcher = new DirWatcher("D:WindowsDesktop").monitor("*.txt")

// .useWatchFile("watch.txt");

//把这个目录当做空目录对待(如果目录里一开始就有txt文件会提示 CREATED)

DirWatcher dirWatcher = new DirWatcher("D:WindowsDesktop")

.monitor("*.txt")

.startBlank(true);

dirWatcher.register(new DirWatcherListener() {

public void onChange(File file, DirWatcher.Event event) {

System.out.println(file.getName() + ":" + event.name());

}

});

//这个有点儿问题,修改文件的命字,只会提示CREATED并不会提示之前的名字的文件DELETED

dirWatcher.start(1000);

while (true) {//防止主线程关闭

}

}

}

Jodd一些模块库

Email 更便捷的邮件收发库。

Props 为处理.properties文件提供更强大、便捷的功能。

HTTP 一款小型的使用十分简单且功能强大的HTTP客户端。

Methref —强类型方法名引用。

SwingSpy 检查swing组件的层次结构。

上面这些工具类大家可以通过链接去官方了解,或者关注我们后期的内容,我们会针对每块儿都做一个详细的讲解,jodd还有一些更为强大,但相对较为复杂的功能我们也会在后期进行讲解。返回搜狐,查看更多

责任编辑:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值