java - 如何在 Java 中制作只读数据类

标签 java containers readonly

我正在构建一个模拟器,我有一个名为 Emulator 的类,其中包含与字体相关的数据和一组稍后放入 CPU 的指令。但是,我想要一个包含所有这些信息的“容器”类,以便 Emulator 更短且更易读。这是面向对象的吗?什么是好的方法?

final int[] fontSet = {
        0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
        0x20, 0x60, 0x20, 0x20, 0x70, // 1
        0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
        0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
        0x90, 0x90, 0xF0, 0x10, 0x10, // 4
        0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
        0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
        0xF0, 0x10, 0x20, 0x40, 0x40, // 7
        0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
        0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
        0xF0, 0x90, 0xF0, 0x90, 0x90, // A
        0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
        0xF0, 0x80, 0x80, 0x80, 0xF0, // C
        0xE0, 0x90, 0x90, 0x90, 0xE0, // D
        0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
        0xF0, 0x80, 0xF0, 0x80, 0x80  // F
};

private List<Instruction> initInstructions() {
    List<Instruction> instructions = new ArrayList<>(INSTRUCTIONS_NUM);

    add0Instructions(instructions);
    add1Instructions(instructions);
    add2Instructions(instructions);
    add3Instructions(instructions);
    add4Instructions(instructions);
    add5Instructions(instructions);
    add6Instructions(instructions);
    add7Instructions(instructions);
    add8Instructions(instructions);
    add9Instructions(instructions);
    addAInstructions(instructions);
    addBInstructions(instructions);
    addCInstructions(instructions);
    addDInstructions(instructions);
    addEInstructions(instructions);
    addFInstructions(instructions);
    return instructions;
}

private void add0Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Clears Display
        public void execute(final CPU cpu) {
            cpu.getMemoryMap().getMemory(MemoryType.DISPLAY).clear();
            Platform.runLater(new Runnable() { // TODO: NO SE SI HACE FALTA
                @Override
                public void run() {
                    display.paint(cpu.getMemoryMap().getMemory(MemoryType.DISPLAY));
                }
            });
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.get() == 0x00E0;
        }
    });

    instructions.add(new Instruction() { //Return from a subroutine
        public void execute(CPU cpu) {
            cpu.getInstPointer().set(cpu.getStack().pop() + 2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.get() == 0x00EE;
        }
    });
}

private void add1Instructions(List<Instruction> instructions) { // Set InstPointer to KKK // CHEQUEADA
    instructions.add(new Instruction() {
        public void execute(CPU cpu) {
            cpu.getInstPointer().set(cpu.getOpCode().get() & 0x0FFF);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x01;
        }
    });
}

private void add2Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Call subroutine at nnn
        public void execute(CPU cpu) {
            cpu.getStack().push(cpu.getInstPointer().get() & 0x0000FFFF);
            cpu.getInstPointer().set(cpu.getOpCode().get() & 0x0FFF);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x02;
        }
    });
}

private void add3Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Skip next instruction if Vx = kk
        public void execute(CPU cpu) {
            int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
            int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);

            if(cpu.getRegistry(position).get() == data)
                cpu.getInstPointer().add(2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x03;
        }
    });
}

private void add4Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Skip next instruction if Vx != kk
        public void execute(CPU cpu) {
            int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
            int data = Bitwise.getByte(cpu.getOpCode().get(), 1);

            if (cpu.getRegistry(position).get() != data)
                cpu.getInstPointer().add(2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x04;
        }
    });
}

private void add5Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Skip next instruction if Vx = Vy
        public void execute(CPU cpu) {
            int position1 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
            int position2 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 2);

            if(cpu.getRegistry(position1).equals(cpu.getRegistry(position2)))
                cpu.getInstPointer().add(2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x05 && opCode.getNibble(3) == 0x00;
        }
    });
}

private void add6Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Set Vx = kk
        public void execute(CPU cpu) {
            int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
            int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);

            cpu.getRegistry(position).set(data);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x6;
        }
    });
}

private void add7Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Set Vx = Vx + kk
        public void execute(CPU cpu) {
            int position = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
            int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);

            cpu.getRegistry(position).set(Bitwise.getByteAsInt(cpu.getRegistry(position).get() + data, 1));
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x07;
        }
    });
}

private void add8Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Set Vx = Vy
        public void execute(CPU cpu) {
            int position1 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 1);
            int position2 = Bitwise.getNibbleAsInt(cpu.getOpCode().get(), 2);

            cpu.getRegistry(position1).set(cpu.getRegistry(position2).get());
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x00;
        }
    });

    instructions.add(new Instruction() { //MUCHAS OPERACIONES LOGICAS
        public void execute(CPU cpu) { //Set Vx = Vx OR Vy
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);

            cpu.getRegistry(position1).or(cpu.getRegistry(position2));
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x01;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vx AND Vy
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);

            cpu.getRegistry(position1).and(cpu.getRegistry(position2));
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x02;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vx XOR Vy
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);

            cpu.getRegistry(position1).xor(cpu.getRegistry(position2));
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x03;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vx + Vy, set VF = carry
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);

            cpu.getRegistry(position1).add(cpu.getRegistry(position2));
            cpu.getRegistry(0xF).set(cpu.getRegistry(position1).get() > 255 ? 0x1 : 0x0);
            cpu.getInstPointer().add(2);
        }

        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x04;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vx - Vy, set VF = NOT borrow
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);

            cpu.getRegistry(position1).sub(cpu.getRegistry(position2));
            cpu.getRegistry(0xF).set(cpu.getRegistry(position1).get() > 0 ? 0x1 : 0x0);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x05;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vx SHR 1
            int position = cpu.getOpCode().getNibble(1);

            cpu.getRegistry(0xF).set(cpu.getRegistry(position  & 0x0001).get());
            cpu.getRegistry(position).set(cpu.getRegistry(position).get() / 2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x06;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vy - Vx, set VF = NOT borrow
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);
            int sub = cpu.getRegistry(position2).get() - cpu.getRegistry(position1).get();

            cpu.getRegistry(position1).set(sub);
            cpu.getRegistry(0xF).set(sub > 0 ? 0x1 : 0x0);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x07;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = Vx SHL 1
            int position = cpu.getOpCode().getNibble(1);

            cpu.getRegistry(0x0F).set((cpu.getRegistry((position & 0x8000) == 0x8000 ? 1 : 0).get()));
            cpu.getRegistry(position).set(cpu.getRegistry(position).get() * 2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x08 && opCode.getNibble(3) == 0x0E;
        }
    });
}

private void add9Instructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Skip next instruction if Vx != Vy
        public void execute(CPU cpu) {
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);

            if(!cpu.getRegistry(position1).equals(cpu.getRegistry(position2)))
                cpu.getInstPointer().add(2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x09;
        }
    });
}

private void addAInstructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Set I = nnn
        public void execute(CPU cpu) {
            cpu.getRegisterI().set(cpu.getOpCode().get() & 0x0FFF);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0A;
        }
    });
}

private void addBInstructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Jump to location nnn + V0
        public void execute(CPU cpu) {
            int sum = Bitwise.getByteAsInt(cpu.getRegistry(0x0).get(), 1) + (cpu.getOpCode().get() & 0x0FFF);
            cpu.getInstPointer().set(sum);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0B;
        }
    });
}

private void addCInstructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Set Vx = random byte AND kk
        public void execute(CPU cpu) {
            int position = cpu.getOpCode().getNibble(1);
            int data = Bitwise.getByteAsInt(cpu.getOpCode().get(), 1);
            data = Bitwise.and(data, rand.nextInt(255 + 1)); // TODO: TAMBIEN NECESITA UN RAND
            cpu.getRegistry(position).set(data);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0C;
        }
    });
}

private void addDInstructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision
        public void execute(final CPU cpu) {
            int position1 = cpu.getOpCode().getNibble(1);
            int position2 = cpu.getOpCode().getNibble(2);
            int height = cpu.getOpCode().getNibble(3);
            int x = Bitwise.getByteAsInt(cpu.getRegistry(position1).get(), 1);
            int y = Bitwise.getByteAsInt(cpu.getRegistry(position2).get(), 1);
            cpu.getRegistry(0x0F).set(0);

            for(int offsetY = 0; offsetY < height; offsetY++) {
                int line = cpu.getMemoryMap().getMemory(MemoryType.RAM).get(cpu.getRegisterI().get() + offsetY);
                for(int offsetX = 0; offsetX < 8; offsetX++) {
                    int pixel =  line & (0x80 >> offsetX);
                    if(pixel != 0) {
                        int totalX = x + offsetX;
                        int totalY = y + offsetY;
                        int index;

                        totalX = totalX % 64;
                        totalY = totalY % 32;
                        index = (totalY * 64) + totalX;
                        if(cpu.getMemoryMap().getMemory(MemoryType.RAM).get(index) == 1)
                            cpu.getRegistry(0x0F).set(1);
                        cpu.getMemoryMap().getMemory(MemoryType.RAM).set(index, cpu.getMemoryMap().getMemory(MemoryType.RAM).get(index) ^ 1);
                    }
                }
            }
            cpu.getInstPointer().add(2);
            //screen.setNeedRedraw(true); //ESTE BOOLEAN TIENE QUE IR EN ALGUN LADO
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    display.paint(cpu.getMemoryMap().getMemory(MemoryType.DISPLAY));
                }
            });
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0D;
        }
    });
}

private void addEInstructions(List<Instruction> instructions) {
    instructions.add(new Instruction() { //Skip next instruction if key with the value of Vx is pressed
        public void execute(CPU cpu) {
            int position = cpu.getOpCode().getNibble(1);
            int data = cpu.getRegistry(position).get();

            if(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(data) == 1)
                cpu.getInstPointer().add(2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0E && (opCode.get() & 0x00FF) == 0x9E;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Skip next instruction if key with the value of Vx is not pressed
            int position = cpu.getOpCode().getNibble(1);
            int data = cpu.getRegistry(position).get();

            if(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(data) == 0)
                cpu.getInstPointer().add(2);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0E && (opCode.get() & 0x00FF) == 0xA1;
        }
    });
}

private void addFInstructions(List<Instruction> instructions) {
    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set Vx = delay timer value
            int position = cpu.getOpCode().getNibble(1);

            cpu.getRegistry(position).set(cpu.getDelayTimer().get());
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x07;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Wait for a key press, store the value of the key in Vx
            int position = cpu.getOpCode().getNibble(1);
            int keyboardSize = cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).size();

            for (int i = 0; i < keyboardSize; i++) {
                if (cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(i) == 1) {
                    cpu.getRegistry(position).set(cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).get(i));
                    cpu.getInstPointer().add(2);
                    cpu.getMemoryMap().getMemory(MemoryType.KEYBOARD).clear();
                    return;
                }
            }
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x0A;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set delay timer = Vx
            int position = cpu.getOpCode().getNibble(1);

            cpu.getDelayTimer().set(cpu.getRegistry(position).get());
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && ((opCode.get() & 0x00FF) == 0x15);
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set sound timer = Vx
            int position = cpu.getOpCode().getNibble(1);

            cpu.getSoundTimer().set(cpu.getRegistry(position).get());
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x18;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set I = I + Vx
            int position = cpu.getOpCode().getNibble(1);

            cpu.getRegisterI().set(cpu.getRegisterI().get() + cpu.getRegistry(position).get());
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x1E;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Set I = location of sprite for digit Vx
            int position = cpu.getOpCode().getNibble(1);
            int character = cpu.getRegistry(position).get();

            cpu.getRegisterI().set(0x0050 + character*5);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x29;
        }
    });

    instructions.add(new Instruction() { //Store BCD representation of Vx in memory locations I, I+1, and I+2
        public void execute(CPU cpu) {
            int position = cpu.getOpCode().getNibble(1);
            int data = cpu.getRegistry(position).get();
            int hundreds = (data - (data % 100)) / 100;
            int tens;

            data -= hundreds * 100;
            tens =  (data - (data % 10)) / 10;
            data -= tens * 10;
            cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get(), (byte)hundreds);
            cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + 1, (byte)tens);
            cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + 2, (byte)data);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x33;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Store registers V0 through Vx in memory starting at location I
            int index = cpu.getOpCode().getNibble(1);

            for(int i=0; i <= index; i++)
                cpu.getMemoryMap().getMemory(MemoryType.RAM).set(cpu.getRegisterI().get() + i, (byte)cpu.getRegistry(i).get());
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x55;
        }
    });

    instructions.add(new Instruction() {
        public void execute(CPU cpu) { //Read registers V0 through Vx from memory starting at location I
            int index = cpu.getOpCode().getNibble(1);

            for(int i=0; i <= index; i++)
                cpu.getRegistry(i).set(cpu.getMemoryMap().getMemory(MemoryType.RAM).get(cpu.getRegisterI().get() + i));
            cpu.getRegisterI().set(cpu.getRegisterI().get() + index + 1);
            cpu.getInstPointer().add(2);
        }
        public boolean validate(OpCode opCode) {
            return opCode.getNibble(0) == 0x0F && (opCode.get() & 0x00FF) == 0x65;
        }
    });
}

最佳答案

如果您将数据存储在变量中:

方法一:

让它们最终

示例:public static final FieldType MYFIELD;

方法二:

您可以将所有变量设为私有(private)并为它们编写 getter。

示例 2:

private static FieldType myField;
public static FieldType getMyField() {
    return myField;
}

关于java - 如何在 Java 中制作只读数据类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26520129/

相关文章:

java - 根据数据值显示按钮

java - 如何通过过滤计算 Vaadin 8 网格页脚中的总数

docker - docker 和 cricontainerd 可以共存吗

java - 与 Hibernate 的只读数据库连接

grails - Grails-@Transactional,连接为只读

java - 导出包含超过 256 列且带有 primefaces 的 Excel

java - 使用 Spring Boot 从 Kafka 队列消费时获取序列化异常

c++ - 在 C++ 中将函数作为参数传递给模板?

apache - 在 docker 容器中运行 apache tomcat 服务器

javascript - 文本输入只读属性在 IE7 中无法识别?