1 /*
2 * Copyright 2018 Andrew Rucker Jones.
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 com.opencsv.bean;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
24 import org.apache.commons.lang3.ObjectUtils;
25
26 /**
27 * A base class to collect all generalized components of a {@link FieldMap}.
28 * May be used by all as a base class for their own implementations of
29 * {@link FieldMap}.
30 *
31 * @param <I> The initializer type used to build the many-to-one mapping
32 * @param <K> Type of the field identifier (key)
33 * @param <C> Type of the ComplexFieldMapEntry used
34 * @param <T> Type of the bean being converted
35 *
36 * @author Andrew Rucker Jones
37 * @since 4.2
38 */
39 abstract public class AbstractFieldMap<I, K extends Comparable<K>, C extends ComplexFieldMapEntry<I, K, T>, T>
40 implements FieldMap<I, K, C, T> {
41
42 /** The locale for error messages. */
43 protected Locale errorLocale;
44
45 /**
46 * A map for all simple, that is one-to-one, mappings represented in this
47 * {@link FieldMap}.
48 */
49 protected final SortedMap<K, BeanField<T, K>> simpleMap = new TreeMap<>();
50
51 /**
52 * A list of entries representing all complex, that is many-to-one, mappings
53 * represented in this {@link FieldMap}.
54 */
55 protected final List<C> complexMapList = new ArrayList<>();
56
57 /**
58 * Initializes this {@link FieldMap}.
59 *
60 * @param errorLocale The locale to be used for error messages
61 */
62 protected AbstractFieldMap(final Locale errorLocale) {
63 this.errorLocale = ObjectUtils.defaultIfNull(errorLocale, Locale.getDefault());
64 }
65
66 @Override
67 public BeanField<T, K> get(final K key) {
68 BeanField<T, K> f = simpleMap.get(key);
69 if(f == null) {
70 f = complexMapList.stream()
71 .filter(r -> r.contains(key))
72 .map(ComplexFieldMapEntry::getBeanField)
73 .findAny().orElse(null);
74 // Would love to do .orElse(simpleMap.get(key)) and shorten this,
75 // but that changes the order of precedence.
76 }
77 return f;
78 }
79
80 @Override
81 public BeanField<T, K> put(final K key, final BeanField<T, K> value) {
82 return simpleMap.put(key, value);
83 }
84
85 @Override
86 public Collection<BeanField<T, K>> values() {
87 final List<BeanField<T, K>> l = new ArrayList<>(simpleMap.size() + complexMapList.size());
88 l.addAll(simpleMap.values());
89 complexMapList.forEach(r -> l.add(r.getBeanField()));
90 return l;
91 }
92
93 @Override
94 public void setErrorLocale(final Locale errorLocale) {
95 this.errorLocale = ObjectUtils.defaultIfNull(errorLocale, Locale.getDefault());
96 complexMapList.forEach(e -> e.setErrorLocale(this.errorLocale));
97 }
98 }