学习java朋友们,福利来了,今天小编给大家带来了一款计算器源码,看图:
视频演示
源码地址:
https://www.githubs.xyz/y171.html
源码是由一位java大神编写的,代码及其优雅。下面我们来分析代码。
黑白主题的实现细节:
public class ThemeLoader {
private ThemeLoader() {
throw new AssertionError("Constructor is not allowed");
}
public static Map<String, Theme> loadThemes() {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.findAndRegisterModules();
try {
ThemeList themeList = mapper.readValue(new File("src/main/resources/application.yaml"), ThemeList.class);
return themeList.getThemesAsMap();
} catch (IOException e) {
return Collections.emptyMap();
}
}
}
ThemeLoader 有个私有的构造器,看过effective-java书的人都知道这种写法。
通过ObjectMapper xml解析框架读取resources下面的主题yml文件:
可以看到都是主流的写法。这还只是冰山一角,学习java就应该从这么优雅的代码学起。
下面来看下这个工具类:
public class ColorUtil {
private ColorUtil() {
throw new AssertionError("Constructor is not allowed");
}
public static Color hex2Color(String colorHex) {
return Optional.ofNullable(colorHex)
.map(hex -> new Color(
Integer.valueOf(colorHex.substring(0, 2), 16),
Integer.valueOf(colorHex.substring(2, 4), 16),
Integer.valueOf(colorHex.substring(4, 6), 16)))
.orElse(null);
}
}
// source download: gitee.com/hadluo/java_code.git
运用了流式写法,完整全部文件找小编
结尾语
我是分享好物+教程+源码的老罗,欢迎关注!
用户评论