小编Jak*_*lsh的帖子

如何在java中创建一个完整的未来

在Java中构建完整未来的最佳方法是什么?我已经CompletedFuture在下面实现了自己,但希望这样的东西已经存在.

public class CompletedFuture<T> implements Future<T> {
    private final T result;

    public CompletedFuture(final T result) {
        this.result = result;
    }

    @Override
    public boolean cancel(final boolean b) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return true;
    }

    @Override
    public T get() throws InterruptedException, ExecutionException {
        return this.result;
    }

    @Override
    public T get(final long l, final TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
        return get();
    }
}
Run Code Online (Sandbox Code Playgroud)

java future

69
推荐指数
4
解决办法
4万
查看次数

Mockito mock在尝试存根包保护方法时调用实际方法实现

我正在尝试使用Mockito 1.8.5存根方法,但这样做会调用真正的方法实现(使用""作为parm值)会抛出异常.

package background.internal; //located in trunk/tests/java/background/internal

public class MoveStepTest {

    @Test
    public void testMoveUpdate() {
        final String returnValue = "value";
        final FileAttachmentContainer file = mock(FileAttachmentContainer.class);
        doReturn(returnValue).when(file).moveAttachment(anyString(), anyString(), anyString());
        //this also fails
        //when(file.moveAttachment(anyString(), anyString(), anyString())).thenReturn(returnValue);

        final AttachmentMoveStep move = new AttachmentMoveStep(file);
        final Action moveResult = move.advance(1, mock(Context.class));
        assertEquals(Action.done, moveResult);
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图模拟的方法看起来像这样.没有最终的方法或类.

package background.internal; //located in trunk/src/background/internal


   public class FileAttachmentContainer {
        String moveAttachment(final String arg1, final String arg2, final String arg3) 
                throws CustomException {
            ...
        }

        String getPersistedValue(final Context context) …
Run Code Online (Sandbox Code Playgroud)

java mockito

28
推荐指数
1
解决办法
4万
查看次数

番石榴表的原始替代品

是否有一种替代Guava Tables,它使用基元而不是泛型类型作为键?

我想使用原语来避免因使用Java Numbers和Java Maps创建的其他入口对象而导致的自动装箱.

我使用Trove TLongObjectMap编译了我自己的基本LongLongObjectTable ,但是如果有可用的话,我更愿意使用标准库.

private static class LongLongObjectTable<T> {
    private final TLongObjectMap<TLongObjectMap<T>> backingMap = new TLongObjectHashMap<>();

    T get(final long rowKey, final long columnKey) {
        final TLongObjectMap<T> map = this.backingMap.get(rowKey);
        if (map == null) {
            return null;
        }
        return map.get(columnKey);
    }

    void put(final long rowKey, final long columnKey, final T value) {
        TLongObjectMap<T> map = this.backingMap.get(rowKey);
        if (map == null) {
            map = new TLongObjectHashMap<>();
            this.backingMap.put(rowKey, map);
        }
        map.put(columnKey, value);
    }

    Collection<T> values() …
Run Code Online (Sandbox Code Playgroud)

java trove4j guava

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

将两个地图组合成一个MultiMap

将两个地图组合成Java中的单个Guava MultiMap的最佳方法是什么?

例如:

  • Map1包含(1,a)和(2,b)
  • Map2包含(2,c)和(3,d)

然后生成的组合多图将包含

  • (1,{a}),(2,{b,c})和(3,{d})

这是我目前的解决方案:

Multimap<T, K> combineMaps(Map<T, K> map1, Map<T, K> map2) {
    Multimap<T, K> multimap = new MultiMap();
    for (final Map.Entry<T, K> entry : map1.entrySet()) {
        multimap.put(entry.getKey(), entry.getValue());
    }
    for (final Map.Entry<T, K> entry : map2.entrySet()) {
        multimap.put(entry.getKey(), entry.getValue());
    }
    return multimap;
}
Run Code Online (Sandbox Code Playgroud)

java multimap guava

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

休息保证 - 无法使用参数和正文进行POST

我正在使用Rest Assured测试REST api.尝试使用url和body内容中的参数进行POST时,我遇到了错误.这在手动测试时可以正常工作.从URL中删除参数不是一个选项

测试代码:

String endpoint = http://localhost:8080/x/y/z/id?custom=test;
String body = "[{\"boolField\":true,\"intField\":991},
                {\"boolField\":false,\"intField\":998}]";
expect().spec(OK).given().body(body).post(endpoint);
Run Code Online (Sandbox Code Playgroud)

运行时会抛出以下错误

You can either send parameters OR body content in the POST, not both!

java.lang.IllegalStateException: You can either send parameters OR body content in the POST, not both!
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:198)
at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:282)
at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendRequest(RequestSpecificationImpl.groovy)
at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$sendRequest.callCurrent(Unknown Source)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:83)
...
Run Code Online (Sandbox Code Playgroud)

为什么Rest Assured不允许POST中的参数和正文内容?

java rest rest-assured

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

如何从 Kotlin 中的 Channel 读取所有可用元素

我想从通道中读取所有可用元素,以便如果我的接收器比发送器慢,我可以对它们进行批处理(希望处理批处理将具有更高的性能并允许接收器赶上)。我只想在通道为空时挂起,而不是挂起直到我的批次已满或超时,这与这个问题不同。

标准 kotlin 库中是否内置了任何内容来实现此目的?

kotlin

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

设置Java值

我是新手,我有这个:

public Builder id (String val)
{
    id = val;
    return this;
}
Run Code Online (Sandbox Code Playgroud)

从下面if语句我想设置构建器id = 1ac.我怎样才能做到这一点.谢谢

if (blah blah)
{
    id = "1ac";
}
Run Code Online (Sandbox Code Playgroud)

java

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

标签 统计

java ×6

guava ×2

future ×1

kotlin ×1

mockito ×1

multimap ×1

rest ×1

rest-assured ×1

trove4j ×1