小编tan*_*nvi的帖子

将选项列表收集到包含当前选项的列表

我正在尝试重构一些代码以返回可选列表而不是可选。

我的班级有以下清单

private final List<Mapper<? extends Message>> mappers;
Run Code Online (Sandbox Code Playgroud)

有一个私有方法可以为这些映射器创建特征并返回消息列表

private List<Message> mapToFeature() {
        mappers.stream()
                .map(mapper -> mapper.createFeature())
                .collect(Optionals.toList());
}
Run Code Online (Sandbox Code Playgroud)

Mapper 的界面如下所示:

public interface Mapper<T extends Message> {
    Optional<T> createFeature();
}
Run Code Online (Sandbox Code Playgroud)

Optionals.toList()方法返回一个收集器以将当前选项过滤到列表中。

我想更改接口(以及所有相应的类)以返回可选列表

public interface Mapper<T extends Message> {
    List<Optional<T>> createFeature();
}
Run Code Online (Sandbox Code Playgroud)

我在Options util 中没有方法从多个列表中过滤当前选项。我如何能够在不对 util 类进行任何更改的情况下执行相同的操作?

java java-8 java-stream collectors

10
推荐指数
2
解决办法
9339
查看次数

单元测试静态方法

我正在尝试为这里的方法解密编写测试用例。

    private static Codec codec;

    static {
        try {
            codec = new Codec(encryptionType, encryptionKey, false, true, false);
        } catch (CodecException e) {
            throw new RuntimeException("Codec initialisation failed", e);
        }
    }


    public static String decrypt(final String toDecrypt) throws CodecException {
        String decrypted = codec.decryptFromBase64(toDecrypt);
        if (decrypted.endsWith(":")) {
            decrypted = decrypted.substring(0, decrypted.length() - 1);
        }
        return decrypted;
    }
Run Code Online (Sandbox Code Playgroud)

测试用例:

    @Mock
    private Codec codec;
    @Test
    public void test_decrypt_Success() throws CodecException {
        when(codec.decryptFromBase64(TestConstants.toDecrypt)).thenReturn(TestConstants.decrypted);
        assertEquals(DocumentUtils.decrypt(TestConstants.toDecrypt), TestConstants.decrypted);
    }
Run Code Online (Sandbox Code Playgroud)

由于这是一个静态方法,我无法在测试套件中注入类的实例并模拟其编解码器。上面的代码按预期在 assert 处从编解码器库中抛出错误。

您测试此类静态方法的方法是什么?或者我根本不应该为此编写测试?

java testing static unit-testing mockito

7
推荐指数
1
解决办法
2万
查看次数

Redis Lua 区分空数组和对象

当我在 redis 3.2 中使用脚本在 json 对象中设置特定值时,我在 cjson lua 中遇到了这个错误。

目前,redis 中的 lua 不区分空的 json 数组或空的 json 对象。在序列化包含数组的 json 对象时,这会导致严重的问题。

eval "local json_str = '{\"items\":[],\"properties\":{}}' return cjson.encode(cjson.decode(json_str))" 0
Run Code Online (Sandbox Code Playgroud)

结果:

"{\"items\":{},\"properties\":{}}"
Run Code Online (Sandbox Code Playgroud)

我找到了这个解决方案https://github.com/mpx/lua-cjson/issues/11但我无法在 redis 脚本中实现。

这是一次失败的尝试:

eval 

"function cjson.mark_as_array(t) 
local mt = getmetatable(t) or {} 
mt.__is_cjson_array = true 
return setmetatable(t, mt) 
end 
function cjson.is_marked_as_array(t) 
local mt = getmetatable(t) 
return mt and mt.__is_cjson_array end 
local json_str = '{\"items\":[],\"properties\":{}}' 
return cjson.encode(cjson.decode(json_str))" 

0
Run Code Online (Sandbox Code Playgroud)

任何帮助或指针表示赞赏。

lua json redis cjson

6
推荐指数
1
解决办法
1589
查看次数

apache poi查找工作表中的行数

我一直在使用这些方法来计算工作表中的行数

getLastRowNum()
Run Code Online (Sandbox Code Playgroud)

getPhysicalNumberOfRows()
Run Code Online (Sandbox Code Playgroud)

在我的工作簿的两张纸上,它们工作正常。但是,包含5行的第三张表将同时返回两个函数。

Sheet NumberofRows getLastRowNum() getPhysicalNumberOfRows()
1     9            9               10
2     56           56              57
3     5            4               5
Run Code Online (Sandbox Code Playgroud)

这两个功能在工作上到底有什么区别,我该怎么做才能获得可靠的正确结果?

java excel apache-poi

3
推荐指数
1
解决办法
4010
查看次数

什么是rho形序列?

我在Saurabh Kr Vats在http://www.careercup.com/question?id=14990323提出的解决方案中遇到了这个问题.

他说:

# Finally, the sequence could be "rho-shaped." In this 
# case, the sequence looks something like this: 
# 
# x_0 -> x_1 -> ... x_k -> x_{k+1} ... -> x_{k+j} 
# ^ | 
# | | 
# +-----------------------+ 
# 
# That is, the sequence begins with a chain of elements that enters a cycle, 
# then cycles around indefinitely. We'll denote the first element of the cycle 
# that is reached in the …
Run Code Online (Sandbox Code Playgroud)

algorithm function cycle-detection

1
推荐指数
1
解决办法
338
查看次数

Java Lambda 用于在两个 for 循环内中断

我正在尝试将 Java 8 中的迭代代码块转换为函数式。函数式方法无法在共享的集合中找到匹配的消息。

List<Optional<Message>> allMessages = new ArrayList<>();

Set<Status> allStatuses = getAllStatuses();

//Iterative : Working
Set<StatusMessage> set = new HashSet<>(STATUS_MESSAGE.values());
for (StatusMessage statusMessage : set) {
    for (Status status : statusMessage.getStatusAndInfo().keySet()) {
        Optional<Message> message = MessageBuilder.createMessage(allStatuses, status, this::createMessage);
        if (message.isPresent()) {
            allMessages.add(message);
            break;
        }
    }
}

//Functional : Not working  - Never adds anything to the 
//map even when matching status is present
STATUS_MESSAGE.values().stream()
        .distinct()
        .map(statusMessage -> statusMessage.getStatusAndInfo().keySet())
        .flatMap(Collection::stream)
        .map(key -> MessageBuilder.createMessage(allStatuses, key, this::createMessage))
        .anyMatch(allMessages::add);
Run Code Online (Sandbox Code Playgroud)

MessageBuilder.createMessage如下所示:

Optional<Status> …
Run Code Online (Sandbox Code Playgroud)

java collections functional-programming java-8 java-stream

1
推荐指数
1
解决办法
74
查看次数