java中找奇数的方法,在java中使用点运算符访问通用列表的奇数方法

I came across some advanced java code (advanced for me :) ) I need help understanding.

In a class there is a nested class as below:

private final class CoverageCRUDaoCallable implements

Callable>

{

private final long oid;

private final long sourceContextId;

private CoverageCRUDaoCallable(long oid, long sourceContextId)

{

this.oid = oid;

this.sourceContextId = sourceContextId;

}

@Override

public List call() throws Exception

{

return coverageCRUDao.getCoverageCRUData(oid, sourceContextId);

}

}

Later in the outer class, there is an instance of the callable class being created.

I have no idea what this is:

ConnectionHelper.> tryExecute(coverageCRUDaoCallable);

It doesn't look like java syntax to me. Could you please elaborate what's going on in this cryptic syntax? You can see it being used below in the code excerpt.

CoverageCRUDaoCallable coverageCRUDaoCallable = new CoverageCRUDaoCallable(

dalClient.getOid(), sourceContextId);

// use Connection helper to make coverageCRUDao call.

List coverageCRUList = ConnectionHelper

.> tryExecute(coverageCRUDaoCallable);

EDITED

added the ConnectionHelper class.

public class ConnectionHelper

{

private static final Logger logger =

LoggerFactory.getLogger(ConnectionHelper.class);

private static final int CONNECTION_RETRIES = 3;

private static final int MIN_TIMEOUT = 100;

public static T tryExecute(Callable command)

{

T returnValue = null;

long delay = 0;

for (int retry = 0; retry < CONNECTION_RETRIES; retry++)

{

try

{

// Sleep before retry

Thread.sleep(delay);

if (retry != 0)

{

logger.info("Connection retry #"+ retry);

}

// make the actual connection call

returnValue = command.call();

break;

}

catch (Exception e)

{

Throwable cause = e.getCause();

if (retry == CONNECTION_RETRIES - 1)

{

logger.info("Connection retries have exhausted. Not trying "

+ "to connect any more.");

throw new RuntimeException(cause);

}

// Delay increased exponentially with every retry.

delay = (long) (MIN_TIMEOUT * Math.pow(2, retry));

String origCause = ExceptionUtils.getRootCauseMessage(e);

logger.info("Connection retry #" + (retry + 1)

+ " scheduled in " + delay + " msec due to "

+ origCause);

+ origCause);

}

}

return returnValue;

}

解决方案

You more often think of classes as being generic, but methods can be generic too. A common example is Arrays.asList.

Most of the time, you don't have to use the syntax with angle brackets <...>, even when you're invoking a generic method, because this is the one place in which the Java compiler is actually capable of doing basic type inference in some circumstances. For example, the snippet given in the Arrays.asList documentation omits the type:

List stooges = Arrays.asList("Larry", "Moe", "Curly");

But it's equivalent to this version in which the generic type is given explicitly:

List stooges = Arrays.asList("Larry", "Moe", "Curly");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值