|
| 1 | +/* |
| 2 | + * Copyright 2020 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 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 org.utplsql.sqldev.model; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.StringJoiner; |
| 23 | + |
| 24 | +import org.springframework.core.style.DefaultValueStyler; |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | +import org.springframework.util.ClassUtils; |
| 27 | +import org.springframework.util.ObjectUtils; |
| 28 | + |
| 29 | +public class UtplsqlValueStyler extends DefaultValueStyler { |
| 30 | + private static final String EMPTY = "[[empty]]"; |
| 31 | + private static final String COLLECTION = "collection"; |
| 32 | + private static final String SET = "set"; |
| 33 | + private static final String LIST = "list"; |
| 34 | + private static final String MAP = "map"; |
| 35 | + private static final String EMPTY_MAP = MAP + EMPTY; |
| 36 | + private static final String ARRAY = "array"; |
| 37 | + |
| 38 | + @Override |
| 39 | + public String style(@Nullable Object value) { |
| 40 | + if (value == null) { |
| 41 | + return super.style(value); |
| 42 | + } else if (value instanceof Map) { |
| 43 | + return styleMap((Map<?, ?>) value); |
| 44 | + } else if (value instanceof Map.Entry) { |
| 45 | + return styleMapEntry((Map.Entry<?, ?>) value); |
| 46 | + } else if (value instanceof Collection) { |
| 47 | + return styleCollection((Collection<?>) value); |
| 48 | + } else if (value.getClass().isArray()) { |
| 49 | + return styleArray(ObjectUtils.toObjectArray(value)); |
| 50 | + } else { |
| 51 | + return super.style(value); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private <K, V> String styleMap(Map<K, V> value) { |
| 56 | + if (value.isEmpty()) { |
| 57 | + return EMPTY_MAP; |
| 58 | + } |
| 59 | + |
| 60 | + StringJoiner result = new StringJoiner(",\n", "[", "]"); |
| 61 | + for (Map.Entry<K, V> entry : value.entrySet()) { |
| 62 | + result.add(styleMapEntry(entry)); |
| 63 | + } |
| 64 | + return MAP + result; |
| 65 | + } |
| 66 | + |
| 67 | + private String styleMapEntry(Map.Entry<?, ?> value) { |
| 68 | + return style(value.getKey()) + " -> " + style(value.getValue()); |
| 69 | + } |
| 70 | + |
| 71 | + private String styleCollection(Collection<?> value) { |
| 72 | + String collectionType = getCollectionTypeString(value); |
| 73 | + |
| 74 | + if (value.isEmpty()) { |
| 75 | + return collectionType + EMPTY; |
| 76 | + } |
| 77 | + |
| 78 | + StringJoiner result = new StringJoiner(",\n", "[", "]"); |
| 79 | + for (Object o : value) { |
| 80 | + result.add(style(o)); |
| 81 | + } |
| 82 | + return collectionType + result; |
| 83 | + } |
| 84 | + |
| 85 | + private String getCollectionTypeString(Collection<?> value) { |
| 86 | + if (value instanceof List) { |
| 87 | + return LIST; |
| 88 | + } else if (value instanceof Set) { |
| 89 | + return SET; |
| 90 | + } else { |
| 91 | + return COLLECTION; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private String styleArray(Object[] array) { |
| 96 | + if (array.length == 0) { |
| 97 | + return ARRAY + '<' + ClassUtils.getShortName(array.getClass().getComponentType()) + '>' + EMPTY; |
| 98 | + } |
| 99 | + |
| 100 | + StringJoiner result = new StringJoiner(",\n ", "[", "]"); |
| 101 | + for (Object o : array) { |
| 102 | + result.add(style(o)); |
| 103 | + } |
| 104 | + return ARRAY + '<' + ClassUtils.getShortName(array.getClass().getComponentType()) + '>' + result; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments