All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.moaxcp.x11client.protocol.sync.CreateAlarm Maven / Gradle / Ivy

There is a newer version: 0.18.2
Show newest version
package com.github.moaxcp.x11client.protocol.sync;

import com.github.moaxcp.x11client.protocol.OneWayRequest;
import com.github.moaxcp.x11client.protocol.X11Input;
import com.github.moaxcp.x11client.protocol.X11Output;
import java.io.IOException;
import java.lang.Byte;
import java.lang.Override;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;

@Value
@Builder
public class CreateAlarm implements OneWayRequest {
  public static final byte OPCODE = 8;

  private int id;

  private int valueMask;

  private int counter;

  private int valueType;

  private Int64 value;

  private int testType;

  private Int64 delta;

  private int events;

  public byte getOpCode() {
    return OPCODE;
  }

  public static CreateAlarm readCreateAlarm(X11Input in) throws IOException {
    in.readPad(1);
    short length = in.readCard16();
    int id = in.readCard32();
    int valueMask = in.readCard32();
    CreateAlarm.CreateAlarmBuilder javaBuilder = CreateAlarm.builder();
    javaBuilder.id(id);
    javaBuilder.valueMask(valueMask);
    if(javaBuilder.isValueMaskEnabled(Ca.COUNTER)) {
      javaBuilder.counter(in.readCard32());
    }
    if(javaBuilder.isValueMaskEnabled(Ca.VALUE_TYPE)) {
      javaBuilder.valueType(in.readCard32());
    }
    if(javaBuilder.isValueMaskEnabled(Ca.VALUE)) {
      javaBuilder.value(Int64.readInt64(in));
    }
    if(javaBuilder.isValueMaskEnabled(Ca.TEST_TYPE)) {
      javaBuilder.testType(in.readCard32());
    }
    if(javaBuilder.isValueMaskEnabled(Ca.DELTA)) {
      javaBuilder.delta(Int64.readInt64(in));
    }
    if(javaBuilder.isValueMaskEnabled(Ca.EVENTS)) {
      javaBuilder.events(in.readCard32());
    }
    in.readPadAlign(javaBuilder.getSize());
    return javaBuilder.build();
  }

  @Override
  public void write(byte offset, X11Output out) throws IOException {
    out.writeCard8((byte)(Byte.toUnsignedInt(OPCODE) + Byte.toUnsignedInt(offset)));
    out.writePad(1);
    out.writeCard16((short) getLength());
    out.writeCard32(id);
    out.writeCard32(valueMask);
    if(isValueMaskEnabled(Ca.COUNTER)) {
      out.writeCard32(counter);
    }
    if(isValueMaskEnabled(Ca.VALUE_TYPE)) {
      out.writeCard32(valueType);
    }
    if(isValueMaskEnabled(Ca.VALUE)) {
      value.write(out);
    }
    if(isValueMaskEnabled(Ca.TEST_TYPE)) {
      out.writeCard32(testType);
    }
    if(isValueMaskEnabled(Ca.DELTA)) {
      delta.write(out);
    }
    if(isValueMaskEnabled(Ca.EVENTS)) {
      out.writeCard32(events);
    }
    out.writePadAlign(getSize());
  }

  public boolean isValueMaskEnabled(@NonNull Ca... maskEnums) {
    for(Ca m : maskEnums) {
      if(!m.isEnabled(valueMask)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public int getSize() {
    return 28 + (isValueMaskEnabled(Ca.COUNTER) ? 4 : 0) + (isValueMaskEnabled(Ca.VALUE_TYPE) ? 4 : 0) + (isValueMaskEnabled(Ca.TEST_TYPE) ? 4 : 0) + (isValueMaskEnabled(Ca.EVENTS) ? 4 : 0);
  }

  public static class CreateAlarmBuilder {
    private CreateAlarm.CreateAlarmBuilder valueMask(int valueMask) {
      this.valueMask = valueMask;
      return this;
    }

    public boolean isValueMaskEnabled(@NonNull Ca... maskEnums) {
      for(Ca m : maskEnums) {
        if(!m.isEnabled(valueMask)) {
          return false;
        }
      }
      return true;
    }

    private CreateAlarm.CreateAlarmBuilder valueMaskEnable(Ca... maskEnums) {
      for(Ca m : maskEnums) {
        valueMask((int) m.enableFor(valueMask));
      }
      return this;
    }

    private CreateAlarm.CreateAlarmBuilder valueMaskDisable(Ca... maskEnums) {
      for(Ca m : maskEnums) {
        valueMask((int) m.disableFor(valueMask));
      }
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder counter(int counter) {
      this.counter = counter;
      valueMaskEnable(Ca.COUNTER);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder valueType(int valueType) {
      this.valueType = valueType;
      valueMaskEnable(Ca.VALUE_TYPE);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder valueType(Valuetype valueType) {
      this.valueType = (int) valueType.getValue();
      valueMaskEnable(Ca.VALUE_TYPE);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder value(Int64 value) {
      this.value = value;
      valueMaskEnable(Ca.VALUE);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder testType(int testType) {
      this.testType = testType;
      valueMaskEnable(Ca.TEST_TYPE);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder testType(Testtype testType) {
      this.testType = (int) testType.getValue();
      valueMaskEnable(Ca.TEST_TYPE);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder delta(Int64 delta) {
      this.delta = delta;
      valueMaskEnable(Ca.DELTA);
      return this;
    }

    public CreateAlarm.CreateAlarmBuilder events(int events) {
      this.events = events;
      valueMaskEnable(Ca.EVENTS);
      return this;
    }

    public int getSize() {
      return 28 + (isValueMaskEnabled(Ca.COUNTER) ? 4 : 0) + (isValueMaskEnabled(Ca.VALUE_TYPE) ? 4 : 0) + (isValueMaskEnabled(Ca.TEST_TYPE) ? 4 : 0) + (isValueMaskEnabled(Ca.EVENTS) ? 4 : 0);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy