|
| 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.persistence.hashing; |
| 17 | + |
| 18 | +import io.serverlessworkflow.impl.marshaller.WorkflowInputBuffer; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +public class DefaultHashFactory implements HashFactory { |
| 22 | + |
| 23 | + @Override |
| 24 | + public Optional<HashItem> fromBuffer(byte id, WorkflowInputBuffer buffer) { |
| 25 | + return Optional.ofNullable( |
| 26 | + switch (id) { |
| 27 | + case MD5HashItem.ID -> new MD5HashItem(buffer); |
| 28 | + case IntegerHashItem.ID -> new IntegerHashItem(buffer); |
| 29 | + default -> null; |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Optional<HashItem> fromData(byte[] data) { |
| 35 | + if (md5Condition(data)) { |
| 36 | + return Optional.of(new MD5HashItem(data)); |
| 37 | + } else if (intCondition(data)) { |
| 38 | + return Optional.of(new IntegerHashItem(data)); |
| 39 | + } else { |
| 40 | + return Optional.empty(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + protected boolean intCondition(byte[] data) { |
| 45 | + return data.length > IntegerHashItem.SIZE_THRESHOLD; |
| 46 | + } |
| 47 | + |
| 48 | + protected boolean md5Condition(byte[] data) { |
| 49 | + return data.length > MD5HashItem.SIZE_THRESHOLD; |
| 50 | + } |
| 51 | +} |
0 commit comments