|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or 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 | + * https://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 | + |
| 17 | +package org.springframework.batch.item.file.transform; |
| 18 | + |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.lang.reflect.RecordComponent; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +/** |
| 29 | + * This is a field extractor for a Java record. By default, it will extract all record |
| 30 | + * components, unless a subset is selected using {@link #setNames(String...)}. |
| 31 | + * |
| 32 | + * @author Mahmoud Ben Hassine |
| 33 | + * @since 5.0 |
| 34 | + */ |
| 35 | +public class RecordFieldExtractor<T> implements FieldExtractor<T> { |
| 36 | + |
| 37 | + private List<String> names; |
| 38 | + |
| 39 | + private Class<? extends T> targetType; |
| 40 | + |
| 41 | + private RecordComponent[] recordComponents; |
| 42 | + |
| 43 | + public RecordFieldExtractor(Class<? extends T> targetType) { |
| 44 | + Assert.notNull(targetType, "target type must not be null"); |
| 45 | + Assert.isTrue(targetType.isRecord(), "target type must be a record"); |
| 46 | + this.targetType = targetType; |
| 47 | + this.recordComponents = this.targetType.getRecordComponents(); |
| 48 | + this.names = getRecordComponentNames(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Set the names of record components to extract. |
| 53 | + * @param names of record component to be extracted. |
| 54 | + */ |
| 55 | + public void setNames(String... names) { |
| 56 | + Assert.notNull(names, "Names must not be null"); |
| 57 | + Assert.notEmpty(names, "Names must not be empty"); |
| 58 | + validate(names); |
| 59 | + this.names = Arrays.stream(names).toList(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @see FieldExtractor#extract(Object) |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public Object[] extract(T item) { |
| 67 | + List<Object> values = new ArrayList<>(); |
| 68 | + for (String componentName : this.names) { |
| 69 | + RecordComponent recordComponent = getRecordComponentByName(componentName); |
| 70 | + Object value; |
| 71 | + try { |
| 72 | + value = recordComponent.getAccessor().invoke(item); |
| 73 | + values.add(value); |
| 74 | + } |
| 75 | + catch (IllegalAccessException | InvocationTargetException e) { |
| 76 | + throw new RuntimeException("Unable to extract value for record component " + componentName, e); |
| 77 | + } |
| 78 | + } |
| 79 | + return values.toArray(); |
| 80 | + } |
| 81 | + |
| 82 | + private List<String> getRecordComponentNames() { |
| 83 | + return Arrays.stream(this.recordComponents).map(recordComponent -> recordComponent.getName()).toList(); |
| 84 | + } |
| 85 | + |
| 86 | + private void validate(String[] names) { |
| 87 | + for (String name : names) { |
| 88 | + if (getRecordComponentByName(name) == null) { |
| 89 | + throw new IllegalArgumentException( |
| 90 | + "Component '" + name + "' is not defined in record " + targetType.getName()); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Nullable |
| 96 | + private RecordComponent getRecordComponentByName(String name) { |
| 97 | + return Arrays.stream(this.recordComponents).filter(recordComponent -> recordComponent.getName().equals(name)) |
| 98 | + .findFirst().orElse(null); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments