Jodd 一 款优雅的 Java 工具集,
Jodd 一 款优雅的 Java 工具集,
为大家介绍一个几乎全能的Java类库——Jodd【文章示例使用3.7.1版本的jodd】
Jodd公用工具
BeanUtil 最快的bean处理库。
一个简单的JavaBean:
/** * 拿客 * 网站:www.coderknock.com * QQ群:213732117 * 三产 创建于 2016年07月02日 22:51:34。 */ public class Foo { private String readwrite; // 提供 getter 和 setter private String readonly; // 只提供getter ... //省略掉个getter和setter }
使用BeanUtil进行操作:
import jodd.bean.BeanUtil; /** * 拿客 * 网站:www.coderknock.com * QQ群:213732117 * 三产 创建于 2016年07月02日 22:56:47。 */ public class TestFoo { public static void main(String[] args) { 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 集优雅与最大限度的精确为一体的时间处理库 【使用教程】
Type Converter 方便高效的类型转换器。
StringUtil 超过100个额外字符串工具方法。
StringTemplateParser 简单的字符串模板解析器。
import jodd.util.StringTemplateParser; import java.util.HashMap; import java.util.Map; /** * 拿客 * www.coderknock.com * QQ群:213732117 * 创建时间:2016年07月14日 * 描述:StringTemplateParser */ public class JoddStringTemplateParser { public static void main(String[] args) { // prepare template String template = "Hello ${foo}. Today is ${dayName}."; // prepare data Map<String, String> map = new HashMap<String, String>(); 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; /** * 拿客 * www.coderknock.com * QQ群:213732117 * 创建时间:2016年07月14日 * 描述:FastCharBuffer */ public class JoddFastCharBuffer { public static void main(String[] args) { 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; /** * 拿客 * www.coderknock.com * QQ群:213732117 * 创建时间:2016年07月14日 * 描述:FastCharBuffer */ public class JoddFastCharBuffer { public static void main(String[] args) { 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<String, String> WildcardInExRules = // new InExRules<String, String>( // InExRuleMatcher.WILDCARD_RULE_MATCHER); } }
Dir Watcher 提供一个对目录的监控,可以在目录中文件发生变化时进行一些特定的处理。
import jodd.io.watch.DirWatcher; import jodd.io.watch.DirWatcherListener; import java.io.File; /** * 拿客 * www.coderknock.com * QQ群:213732117 * 创建时间:2016年07月14日 * 描述:JDateTime */ public class JoddWatcher { public static void main(String[] args) { //只有这个watch.txt文件修改时才触发 // DirWatcher dirWatcher = new DirWatcher("D:\\Windows\\Desktop").monitor("*.txt") // .useWatchFile("watch.txt"); //把这个目录当做空目录对待(如果目录里一开始就有txt文件会提示 CREATED) DirWatcher dirWatcher = new DirWatcher("D:\\Windows\\Desktop") .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还有一些更为强大,但相对较为复杂的功能我们也会在后期进行讲解。
相关文章
- 暂无相关文章
用户点评