考虑这两个类
class EmailService {
public Optional<String> getEmailAlias(String email);
}
enum Queue {
public static Optional<Queue> fromEmailAlias(String alias);
}
Run Code Online (Sandbox Code Playgroud)
上述方法的实现对于这个问题并不重要,所以为了简单起见,我把它留了下来.
我想做这个:
emailService.getEmailAlias("john@done")
.map(Queue::fromEmailAlias)
.ifPresent(queue -> {
// do something with the queue instance, oh wait it's an Optional<Queue> :(
});
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为queue是类型Optional<queue>(与返回的类型相同Queue::fromEmailAlias),所以我改为:
emailService.getEmailAlias("john@done")
.map(Queue::fromEmailAlias)
.ifPresent(q-> {
q.ifPresent(queue -> {
// do something with the queue instance
}
});
Run Code Online (Sandbox Code Playgroud)
一种丑陋的imho.
改变签名
public static Optional<Queue> fromEmailAlias(String alias);
Run Code Online (Sandbox Code Playgroud)
至
public static Queue fromEmailAlias(String alias);
Run Code Online (Sandbox Code Playgroud)
是一个快速修复,但这也会影响我需要的其他地方的代码Optional<Queue>.
有没有一种很好的方法来解开这个嵌套的Optional?
有没有办法Field在不使用反射的情况下访问类的特定内容?
考虑这个课程:
class MyType {
public int theNumber;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够访问theNumber的地方 java.lang.reflect.Field.
这肯定有效:
Field f = MyType.class.getDeclaredField("theNumber");
Run Code Online (Sandbox Code Playgroud)
但是,我想编译检查字段名称,所以理想情况下这样的东西(但当然我的例子不编译):
Field f = MyType.class::theNumber;
Run Code Online (Sandbox Code Playgroud)
这是可能的还是我对编译器能力的方式?
我正在测试Bootstrap框架(来自Twitter)并尝试在安装Node.js后在本地构建它.它失败了,因为它找不到较少的模块(我也用'npm install -g less'安装):
C:\Users\geir\code\bootstrap>make
lessc html/less/bootstrap.less > html/css/bootstrap.css
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'C:\cygdrive\c\Users\geir\AppData\Roaming\npm\node_modules\less\bin\lessc'
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
Makefile:2: recipe for target `all' failed
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)
它失败的原因是因为当make正在寻找模块时,"cygdrive"被预先添加到文件夹层次结构中,我已经验证了thtat'minorec'实际上是在C:\ Users\geir\AppData\Roaming \npm \中找到的node_modules\less\bin\lessc,我可以从命令行运行它.
我试过以下但没有成功:
任何线索?
我正在玩lambdas并进入我的脑海,我想尝试创建一个简单的数据库/对象映射器作为学习的一部分.
是的,有很多框架已经做到了这一点,但这更多的是关于学习和我遇到的问题是技术问题.
首先,我想在枚举中定义所有映射逻辑.
它只用一堆字段名称开始简单明了:
enum ThingColumn {
id, language;
}
Run Code Online (Sandbox Code Playgroud)
这让我创建了以下方法(实现不相关),它给出了api用户对列的编译检查:
public Collection<Thing> findAll(ThingColumn... columns);
Run Code Online (Sandbox Code Playgroud)
之后我想在枚举中定义更多规则,特别是如何将结果从a映射java.sql.ResultSet到我的Thing类.
从简单的开始我创建了一个功能界面:
@FunctionalInterface
static interface ThingResultMapper {
void map(Thing to, ResultSet from, String column) ;
}
Run Code Online (Sandbox Code Playgroud)
并将其添加到枚举中:
enum ThingColumn {
id((t, rs, col) -> t.setId(rs.getLong(col))),
language((t, rs, col) ->t.setLanguage(rs.getString(col)));
ThingColumn(ThingResultMapper mapper){..}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个mapResultSetRow方法,它使用枚举中的lambda来从以下位置提取数据ResultSet:
public Thing mapResultSetRow(ResultSet rs, ThingColumn... fields) {
Thing t = new Thing();
Stream.of(fields)
.forEach(f -> f.getMapper().map(t, rs, f.name()));
return t;
}
Run Code Online (Sandbox Code Playgroud)
以上findAll可以使用 …
我是QlikView的新手,并且想要使用某些版本控制系统(例如Git)来跟踪更改。
但是我不确定该怎么做,因为乍一看,.qvm文件包含代码和数据。作为开发者,这似乎有些奇怪,而且如果确实如此,则对源代码控制有很大的限制。
有人知道如何将代码与QlikView文件中的数据分开吗?
我正在尝试使用Spring Caching注释@Cacheable和@CacheEvictGuavaCacheManager.
我用这两个测试创建了一个测试用例:
cachesById- 验证对annotatted方法的两次调用是否@Cacheable返回相同的对象evict- 如果@CacheEvict在这两个调用之间调用了带注释的方法,则验证是否返回了两个不同的实例当我没有指定密钥时,两者都工作正常@CacheEvict,但是当我得到以下异常时:
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
at org.springframework.cache.guava.GuavaCache.evict(GuavaCache.java:135)
at org.springframework.cache.interceptor.AbstractCacheInvoker.doEvict(AbstractCacheInvoker.java:95)
at org.springframework.cache.interceptor.CacheAspectSupport.performCacheEvict(CacheAspectSupport.java:409)
at org.springframework.cache.interceptor.CacheAspectSupport.processCacheEvicts(CacheAspectSupport.java:392)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:362)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.myorg.caching.CacheTest$Repo$$EnhancerBySpringCGLIB$$eed50f3e.update(<generated>)
at com.myorg.caching.CacheTest.evict(CacheTest.java:50)
Run Code Online (Sandbox Code Playgroud)
这可以通过执行以下测试来再现.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes = { Repo.class, CacheTest.SpringConfig.class },
loader = AnnotationConfigContextLoader.class)
public class CacheTest {
private static final String CACHE_NAME = "cacheName";
@Inject
private Repo repo;
@Test
public void cachesById() {
Entity …Run Code Online (Sandbox Code Playgroud)