CustomDataType.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package at.acdp.opcur.opc;
  2. /*
  3. * Copyright (c) 2017 Kevin Herron
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * and Eclipse Distribution License v1.0 which accompany this distribution.
  8. *
  9. * The Eclipse Public License is available at
  10. * http://www.eclipse.org/legal/epl-v10.html
  11. * and the Eclipse Distribution License is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.html.
  13. */
  14. import com.google.common.base.MoreObjects;
  15. import com.google.common.base.Objects;
  16. import org.eclipse.milo.opcua.stack.core.UaSerializationException;
  17. import org.eclipse.milo.opcua.stack.core.serialization.UaDecoder;
  18. import org.eclipse.milo.opcua.stack.core.serialization.UaEncoder;
  19. import org.eclipse.milo.opcua.stack.core.serialization.codecs.GenericDataTypeCodec;
  20. import org.eclipse.milo.opcua.stack.core.serialization.codecs.SerializationContext;
  21. import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
  22. import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
  23. public class CustomDataType {
  24. private final String foo;
  25. private final UInteger bar;
  26. private final boolean baz;
  27. public CustomDataType() {
  28. this(null, uint(0), false);
  29. }
  30. public CustomDataType(String foo, UInteger bar, boolean baz) {
  31. this.foo = foo;
  32. this.bar = bar;
  33. this.baz = baz;
  34. }
  35. public String getFoo() {
  36. return foo;
  37. }
  38. public UInteger getBar() {
  39. return bar;
  40. }
  41. public boolean isBaz() {
  42. return baz;
  43. }
  44. @Override
  45. public boolean equals(Object o) {
  46. if (this == o) return true;
  47. if (o == null || getClass() != o.getClass()) return false;
  48. CustomDataType that = (CustomDataType) o;
  49. return baz == that.baz &&
  50. Objects.equal(foo, that.foo) &&
  51. Objects.equal(bar, that.bar);
  52. }
  53. @Override
  54. public int hashCode() {
  55. return Objects.hashCode(foo, bar, baz);
  56. }
  57. @Override
  58. public String toString() {
  59. return MoreObjects.toStringHelper(this)
  60. .add("foo", foo)
  61. .add("bar", bar)
  62. .add("baz", baz)
  63. .toString();
  64. }
  65. public static class Codec extends GenericDataTypeCodec<CustomDataType> {
  66. @Override
  67. public Class<CustomDataType> getType() {
  68. return CustomDataType.class;
  69. }
  70. @Override
  71. public CustomDataType decode(
  72. SerializationContext context,
  73. UaDecoder decoder) throws UaSerializationException {
  74. String foo = decoder.readString("Foo");
  75. UInteger bar = decoder.readUInt32("Bar");
  76. boolean baz = decoder.readBoolean("Baz");
  77. return new CustomDataType(foo, bar, baz);
  78. }
  79. @Override
  80. public void encode(
  81. SerializationContext context,
  82. CustomDataType customDataType,
  83. UaEncoder encoder) throws UaSerializationException {
  84. encoder.writeString("Foo", customDataType.foo);
  85. encoder.writeUInt32("Bar", customDataType.bar);
  86. encoder.writeBoolean("Baz", customDataType.baz);
  87. }
  88. }
  89. }