小编And*_*eas的帖子

无法使用私有变量对非静态字段memberVariable进行静态引用

我用一个私有成员变量创建了一个枚举.当我尝试访问成员变量时,编译状态'无法对非静态字段memberVariable进行静态引用'.

如果变量不是私有的(例如受保护或包保护),则编译很好.我不明白变量的范围与实现的抽象函数的类型(静态,非静态)有什么关系.

有人可以开导我吗?

public enum EnumWithAbstractMethodAndMembers {
    TheOneAndOnly(1) {
        @Override
        public int addValue(final int value) {
            return memberVariable + value;
        }
    };

    private final int memberVariable;

    private EnumWithAbstractMethodAndMembers(final int memberVariable) {
        this.memberVariable = memberVariable;
    }

    abstract int addValue(int value);

}
Run Code Online (Sandbox Code Playgroud)

java enums

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

是否存在用于递归比较目录的匹配器?

我正在编写IM和导出文件的单元测试.我需要逐个字节地递归测试生成的目录.我自己实现了平面目录的例程,并且知道如何递归地执行此操作.但我不想重新发明轮子.

那么有类似下面的例子吗?

Matchers.matches(Path actual, equalsRecursive(Path value));
Run Code Online (Sandbox Code Playgroud)

要么

FileAssertions.equalsRecursive(Path actual, Path value);
Run Code Online (Sandbox Code Playgroud)

java recursion junit hamcrest

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

有限的SortedSet

我正在寻找具有有限数量元素的SortedSet的实现.因此,如果添加了更多元素,则指定的最大值比较器决定是否添加项目并从集合中删除最后一个项目.

SortedSet<Integer> t1 = new LimitedSet<Integer>(3);
t1.add(5);
t1.add(3);
t1.add(1);
// [1,3,5]
t1.add(2);
// [1,2,3]
t1.add(9);
// [1,2,3]
t1.add(0);
// [0,1,2]
Run Code Online (Sandbox Code Playgroud)

标准API中是否有一种优雅的方法来实现这一目标?

我写了一个JUnit Test来检查实现:

@Test
public void testLimitedSortedSet() {
final LimitedSortedSet<Integer> t1 = new LimitedSortedSet<Integer>(3);
t1.add(5);
t1.add(3);
t1.add(1);
System.out.println(t1);
// [1,3,5]
t1.add(2);
System.out.println(t1);
// [1,2,3]
t1.add(9);
System.out.println(t1);
// [1,2,3]
t1.add(0);
System.out.println(t1);
// [0,1,2]
Assert.assertTrue(3 == t1.size());
Assert.assertEquals(Integer.valueOf(0), t1.first());
}
Run Code Online (Sandbox Code Playgroud)

java api sortedset

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

Freemarker模型将时间戳转换为以毫秒为单位

我有一个csv文件,我想用fmpp(freemarker)进行转换.第一列是一个长值(自1970年1月1日以来的毫秒),我想将其转换为日期并将其格式化为datetime.

src格式:

timeStamp,elapsed,label,responseCode,threadName,dataType,success,bytes,URL,Latency
1319115474244,40142,Login,200,Login 1-2,text,true,862184,http://localhost:8080/xxx,5378
Run Code Online (Sandbox Code Playgroud)

理想的目标格式:

timeStamp;elapsed;label;responseCode;threadName;dataType;success;bytes;URL;Latency
20.12.2011 13:45;40142;Login;200;Login 1-2;text;true;862184;http://localhost:8080/xxx;5378
Run Code Online (Sandbox Code Playgroud)

我的(运行)模板:

<#list csv.headers as h>${h}<#if h_has_next>;</#if></#list>
<#list csv as row>
<#list csv.headers as h><#if h_index == 0>Do the date magic<#else>${(row[h]!"N/A")?string}</#if>$<#if h_has_next>;</#if></#list>
</#list>
Run Code Online (Sandbox Code Playgroud)

对于第0列,我想进行转换.我不想写一个包含日期的新模型.我的问题是,这可以在模板中完成,而无需修改freemarker或fmpp.

有任何想法吗?

java freemarker fmpp

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

Camel拆分后,Exception不会升级

我们在Camel中定义了一个路由,并且如果在处理器中抛出异常,则必须找出它.当我们只有一个处理器时,Camel会在sendBody()方法中重新抛出异常.如果存在先前的分割/聚合,则不会抛出异常.所以下面例子的结果是

before throwing Exception

after sendBody
Run Code Online (Sandbox Code Playgroud)

如果我省略从.split到.completionSize(1)的所有内容,则输出为

before throwing Exception

Exception thrown
Run Code Online (Sandbox Code Playgroud)

任何想法如何找出,如果分裂后发生异常?

private static final String DIRECT_START = "direct:start";

public static void main(String[] args) throws Exception {
    CamelContext context = new DefaultCamelContext();

    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {

            from(DIRECT_START)
            .split(body())
                .aggregate(constant(true), new AggregationStrategy() {
                    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                        return oldExchange == null ? newExchange : oldExchange;
                    }
                })
                .completionSize(1)
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    System.out.println("before throwing Exception"); …
Run Code Online (Sandbox Code Playgroud)

java split apache-camel

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

标签 统计

java ×5

apache-camel ×1

api ×1

enums ×1

fmpp ×1

freemarker ×1

hamcrest ×1

junit ×1

recursion ×1

sortedset ×1

split ×1