|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.marshaller; |
| 17 | + |
| 18 | +import java.time.Instant; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +public abstract class AbstractOutputBuffer implements WorkflowOutputBuffer { |
| 23 | + |
| 24 | + private final Collection<CustomObjectMarshaller> customMarshallers; |
| 25 | + |
| 26 | + protected AbstractOutputBuffer(Collection<CustomObjectMarshaller> customMarshallers) { |
| 27 | + this.customMarshallers = customMarshallers; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public WorkflowOutputBuffer writeInstant(Instant instant) { |
| 32 | + writeLong(instant.getEpochSecond()); |
| 33 | + return this; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public <T extends Enum<T>> WorkflowOutputBuffer writeEnum(T value) { |
| 38 | + writeString(value.name()); |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public WorkflowOutputBuffer writeMap(Map<String, Object> map) { |
| 44 | + writeInt(map.size()); |
| 45 | + map.forEach( |
| 46 | + (k, v) -> { |
| 47 | + writeString(k); |
| 48 | + writeObject(v); |
| 49 | + }); |
| 50 | + |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public WorkflowOutputBuffer writeCollection(Collection<Object> col) { |
| 56 | + writeInt(col.size()); |
| 57 | + col.forEach(this::writeObject); |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + private enum TYPE { |
| 62 | + BYTE, |
| 63 | + BYTES, |
| 64 | + INT, |
| 65 | + SHORT, |
| 66 | + LONG, |
| 67 | + FLOAT, |
| 68 | + DOUBLE, |
| 69 | + BOOLEAN, |
| 70 | + STRING, |
| 71 | + INSTANT, |
| 72 | + MAP, |
| 73 | + COLLECTION, |
| 74 | + NULL |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public WorkflowOutputBuffer writeObject(Object object) { |
| 79 | + if (object == null) { |
| 80 | + writeType(TYPE.NULL); |
| 81 | + } else if (object instanceof Short number) { |
| 82 | + writeType(TYPE.SHORT); |
| 83 | + writeShort(number); |
| 84 | + } else if (object instanceof Integer number) { |
| 85 | + writeType(TYPE.INT); |
| 86 | + writeInt(number); |
| 87 | + } else if (object instanceof Long number) { |
| 88 | + writeType(TYPE.LONG); |
| 89 | + writeLong(number); |
| 90 | + } else if (object instanceof Byte number) { |
| 91 | + writeType(TYPE.BYTE); |
| 92 | + writeLong(number); |
| 93 | + } else if (object instanceof Float number) { |
| 94 | + writeType(TYPE.FLOAT); |
| 95 | + writeFloat(number); |
| 96 | + } else if (object instanceof Double number) { |
| 97 | + writeType(TYPE.DOUBLE); |
| 98 | + writeDouble(number); |
| 99 | + } else if (object instanceof Boolean bool) { |
| 100 | + writeType(TYPE.BOOLEAN); |
| 101 | + writeBoolean(bool); |
| 102 | + } else if (object instanceof String str) { |
| 103 | + writeType(TYPE.STRING); |
| 104 | + writeString(str); |
| 105 | + } else if (object instanceof Map value) { |
| 106 | + writeType(TYPE.MAP); |
| 107 | + writeMap(value); |
| 108 | + } else if (object instanceof Collection value) { |
| 109 | + writeType(TYPE.COLLECTION); |
| 110 | + writeCollection(value); |
| 111 | + } else if (object instanceof Instant value) { |
| 112 | + writeType(TYPE.INSTANT); |
| 113 | + writeInstant(value); |
| 114 | + } else if (object instanceof byte[] bytes) { |
| 115 | + writeType(TYPE.BYTES); |
| 116 | + writeBytes(bytes); |
| 117 | + } else { |
| 118 | + writeCustomObject(object); |
| 119 | + } |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + protected void writeClass(Class<?> objectClass) { |
| 124 | + writeString(objectClass.getCanonicalName()); |
| 125 | + } |
| 126 | + |
| 127 | + protected void writeCustomObject(Object object) { |
| 128 | + customMarshallers.stream() |
| 129 | + .filter(m -> m.getObjectClass().isAssignableFrom(object.getClass())) |
| 130 | + .findFirst() |
| 131 | + .ifPresentOrElse( |
| 132 | + m -> { |
| 133 | + writeClass(m.getObjectClass()); |
| 134 | + m.write(this, m.getObjectClass().cast(object)); |
| 135 | + }, |
| 136 | + () -> new IllegalArgumentException("Unsupported type " + object.getClass())); |
| 137 | + } |
| 138 | + |
| 139 | + private void writeType(TYPE type) { |
| 140 | + writeByte((byte) type.ordinal()); |
| 141 | + } |
| 142 | +} |
0 commit comments