有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java@Array(长度=?)怎么样OpenHFT中的注释/使用的值

这个问题是关于Chronicle-Values

该网站中的一个例子是:

interface SomeStats {
    @Array(length=100)
    long getPercentFreqAt(int index);
    void setPercentFreqAt(int index, long percentFreq);
    long addPercentFreqAt(int index, long addition);
}

这里,注释仅应用于其中一个方法。这是否意味着之后的所有方法都被视为处理数组数据

在我发现的one of the test个案例中

package net.openhft.chronicle.values;

public interface HasArraysInterface {
    @Array(length = 4)
    void setFlagAt(int idx, boolean flag);

    boolean getFlagAt(int idx);

    @Array(length = 4)
    void setByteAt(int idx, byte b);

    byte getByteAt(int idx);

    @Array(length = 4)
    void setShortAt(int idx, short s);

    short getShortAt(int idx);

    @Array(length = 4)
    void setCharAt(int idx, char ch);

    char getCharAt(int idx);

    @Array(length = 4)
    void setIntAt(int idx, int i);

    int getIntAt(int idx);

    @Array(length = 4)
    void setFloatAt(int idx, float f);

    float getFloatAt(int idx);

    @Array(length = 4)
    void setLongAt(int idx, long l);

    long getLongAt(int idx);

    @Array(length = 4)
    void setDoubleAt(int idx, double d);

    double getDoubleAt(int idx);

    @Array(length = 4)
    void setStringAt(int idx, @MaxUtf8Length(8) String s);

    String getStringAt(int idx);
}

我从中了解到,在这个接口中可以有多个数组,其中@Array(length = 4)应用于以At结尾的方法,直到下一个注释。是这样吗

此外,您还可以使用类似以下内容来模拟4个双精度数组和8个字符串数组:

    @Array(length = 4)
    void setDoubleAt(int idx, double d);

    double getDoubleAt(int idx);

    @Array(length = 8)
    void setStringAt(int idx, @MaxUtf8Length(8) String s);

    String getStringAt(int idx);

在一个接口中分配了多个@Array(length= ?)的多个阵列的内存布局是什么?您可以选择以列为导向还是以行为导向的布局?如果length不同,将如何处理布局

也不是:

interface SomeStats {
    @Array(length=100)
    long getPercentFreqAt(int index);
    void setPercentFreqAt(int index, long percentFreq);
    long addPercentFreqAt(int index, long addition);
}

你能把它写成:

@Array(length=100)
interface SomeStats {
    long getPercentFreqAt(int index);
    void setPercentFreqAt(int index, long percentFreq);
    long addPercentFreqAt(int index, long addition);
}

暗示@Array(length=100)适用于整个接口

您还可以推迟到创建时指定长度吗


共 (2) 个答案

  1. # 1 楼答案

    1. Here the annotation is only applied to the one of the methods. Does this imply all methods afterwards are treated as working on the array data?

    ^{} javadoc

    This annotation must be put on a single method accessing the array elements: getter, or setter, or adder, etc.

    也就是说,@Array应该放在一个(任何)方法上,访问一个字段。“字段”由标准访问器方法和名称定义,即在您的示例中,在SomeStats值接口中,有一个名为PercentFreq的单个(数组)字段。如果您向这个接口添加像getAnotherField()setAnotherField()这样的方法,它将是一个单独的字段,@Array注释不应用于该字段,也就是说,它将是一个“标量”字段,除非您将@Array放在getAnotherField()setAnotherField()上,并向这些方法添加int index参数和-At后缀

    二,

    What I understood from this is that you can have multiple arrays within this interface where @Array(length = 4) applies to methods ending with At until the next annotation. Is this right?

    不,不是“直到下一个注释”。注释应用于字段。字段访问器方法(由字段名标识)可以在接口定义中按任意顺序排列

    三,

    What is the memory layout of multiple arrays allocated with multiple @Array(length= ?) within one interface? Can you choose between column oriented or row oriented layout? How will the layouts be handled if the length is different?

    在您的示例中,布局为:

    Double element 0 (8 bytes)
    Double element 1 (8 bytes)
    Double element 2 (8 bytes)
    Double element 3 (8 bytes)
    String element 0 (9 bytes: 1 bytes to encode size + 8 bytes of reserved space)
    ...
    String element 7 (9 bytes)
    

    要进行基于行的对齐,应定义另一个接口:

    interface Row {
        void setDouble(double d);
        double getDouble();
    
        void setString(@MaxUtf8Length(8) String s);
        String getString();
    }
    

    然后定义数组或行:

    interface MyTopInterface {
        @Array(length=8)
        void setRowAt(int index, Row row);
        Row getRowAt(int index);
    }
    

    这将是同样有效的,并采取相同的内存量,作为“列定向”布局Read about nested structures in the tutorial.

    四,

    implying @Array(length=100) applies to the whole interface.

    不能这样做,请使用上面建议的模式(嵌套接口)

    五,

    Also can you defer specifying the length until the point of creation?

    不,编年史值的全部思想是,它们的总大小是静态已知的

  2. # 2 楼答案

    当空间分配到位时,需要阵列长度。如果指定长度为N,则表示只能有索引0<;=索引<;N