|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright (c) 2016. David Sowerby |
| 4 | + * * |
| 5 | + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 6 | + * * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * * |
| 8 | + * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | + * * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | + * * specific language governing permissions and limitations under the License. |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +package uk.q3c.util.data.collection; |
| 15 | + |
| 16 | +import com.google.common.base.Splitter; |
| 17 | +import com.google.common.collect.ImmutableList; |
| 18 | +import org.apache.commons.lang3.StringEscapeUtils; |
| 19 | +import uk.q3c.util.data.ClassConverter; |
| 20 | +import uk.q3c.util.data.ConversionException; |
| 21 | + |
| 22 | +import javax.annotation.Nonnull; |
| 23 | +import java.lang.annotation.Annotation; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static com.google.common.base.Preconditions.*; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Converts a OptionList<?><X> where X is an element with its own Converter, to and from a String representation (specifically a single String with elements |
| 32 | + * quoted and comma-separated as defined by {@link StringEscapeUtils#escapeCsv(String)}) |
| 33 | + * <p> |
| 34 | + * Created by David Sowerby on 04/08/15. |
| 35 | + */ |
| 36 | +public class AnnotationListConverter { |
| 37 | + |
| 38 | + public final static String separator = "~~"; |
| 39 | + |
| 40 | + public AnnotationListConverter() { |
| 41 | + super(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Converts {@code model} to String |
| 46 | + * |
| 47 | + * @param model the model to convert |
| 48 | + * @return Comma separated String representation of the list contents |
| 49 | + */ |
| 50 | + public String convertToString(@Nonnull AnnotationList model) { |
| 51 | + checkNotNull(model); |
| 52 | + if (model.isEmpty()) { |
| 53 | + return ""; |
| 54 | + } |
| 55 | + StringBuilder buf = new StringBuilder(); |
| 56 | + ImmutableList<Class<? extends Annotation>> modelList = model.getList(); |
| 57 | + ClassConverter classConverter = new ClassConverter(); |
| 58 | + boolean first = true; |
| 59 | + for (Class<? extends Annotation> e : modelList) { |
| 60 | + if (!first) { |
| 61 | + buf.append(separator); |
| 62 | + } else { |
| 63 | + first = false; |
| 64 | + } |
| 65 | + buf.append(StringEscapeUtils.escapeCsv(classConverter.convertToString(e))); |
| 66 | + } |
| 67 | + return buf.toString(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Converts String representation, {@code value}, to model |
| 72 | + * |
| 73 | + * @param value the String to convert |
| 74 | + * @return Comma separated String representation of the list contents |
| 75 | + * @throws ConversionException if conversion fails |
| 76 | + */ |
| 77 | + public AnnotationList convertToModel(@Nonnull String value) { |
| 78 | + checkNotNull(value); |
| 79 | + if (value.isEmpty()) { |
| 80 | + return new AnnotationList(); |
| 81 | + } |
| 82 | + final List<String> strings = Splitter.on(separator) |
| 83 | + .splitToList(value); |
| 84 | + List<Class<? extends Annotation>> elementList = new ArrayList<>(); |
| 85 | + strings.forEach(s -> { |
| 86 | + String unescaped = StringEscapeUtils.unescapeCsv(s); |
| 87 | + Class<?> element = new ClassConverter().convertToModel(unescaped); |
| 88 | + if (Annotation.class.isAssignableFrom(element)) { |
| 89 | + //noinspection unchecked |
| 90 | + elementList.add((Class<? extends Annotation>) element); |
| 91 | + } else { |
| 92 | + throw new ConversionException("Class " + element + " is not an Annotation"); |
| 93 | + } |
| 94 | + }); |
| 95 | + return new AnnotationList(elementList); |
| 96 | + } |
| 97 | +} |
0 commit comments