View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.domain.person;
28  
29  import com.rapiddweller.common.CollectionUtil;
30  import com.rapiddweller.common.ConversionException;
31  import com.rapiddweller.common.LocaleUtil;
32  import com.rapiddweller.common.Locales;
33  import com.rapiddweller.common.StringUtil;
34  import com.rapiddweller.common.converter.ThreadSafeConverter;
35  
36  import java.util.Locale;
37  import java.util.Set;
38  
39  /**
40   * Formats {@link Person} objects.<br/><br/>
41   * Created: 22.02.2010 12:41:37
42   *
43   * @author Volker Bergmann
44   * @since 0.6.0
45   */
46  public abstract class PersonFormatter
47      extends ThreadSafeConverter<Person, String> {
48  
49    /**
50     * The constant WESTERN.
51     */
52    public static final PersonFormatter WESTERN = new Western();
53    /**
54     * The constant EASTERN.
55     */
56    public static final PersonFormatter EASTERN = new Eastern();
57    private static final Set<Locale> EASTERN_LOCALES = CollectionUtil.toSet(
58        Locales.CHINESE, Locales.JAPANESE, Locales.KOREAN, Locales.THAI,
59        Locales.VIETNAMESE
60    );
61  
62    /**
63     * Instantiates a new Person formatter.
64     */
65    public PersonFormatter() {
66      super(Person.class, String.class);
67    }
68  
69    /**
70     * Gets instance.
71     *
72     * @param locale the locale
73     * @return the instance
74     */
75    public static PersonFormatter getInstance(Locale locale) {
76      return (EASTERN_LOCALES.contains(LocaleUtil.language(locale)) ?
77          EASTERN : WESTERN);
78    }
79  
80    @Override
81    public String convert(Person person) throws ConversionException {
82      return format(person);
83    }
84  
85    /**
86     * Format string.
87     *
88     * @param person the person
89     * @return the string
90     */
91    public abstract String format(Person person);
92  
93    /**
94     * Append separated.
95     *
96     * @param part    the part
97     * @param builder the builder
98     */
99    protected void appendSeparated(String part, StringBuilder builder) {
100     if (!StringUtil.isEmpty(part)) {
101       if (builder.length() > 0) {
102         builder.append(' ');
103       }
104       builder.append(part);
105     }
106   }
107 
108   /**
109    * The type Western.
110    */
111   static class Western extends PersonFormatter {
112 
113     @Override
114     public String format(Person person) {
115       StringBuilder builder = new StringBuilder();
116       appendSeparated(person.getSalutation(), builder);
117       appendSeparated(person.getAcademicTitle(), builder);
118       appendSeparated(person.getNobilityTitle(), builder);
119       appendSeparated(person.getGivenName(), builder);
120       appendSeparated(person.getFamilyName(), builder);
121       return builder.toString();
122     }
123   }
124 
125   /**
126    * The type Eastern.
127    */
128   static class Eastern extends PersonFormatter {
129 
130     @Override
131     public String format(Person person) {
132       StringBuilder builder = new StringBuilder();
133       appendSeparated(person.getSalutation(), builder);
134       appendSeparated(person.getAcademicTitle(), builder);
135       appendSeparated(person.getNobilityTitle(), builder);
136       appendSeparated(person.getFamilyName(), builder);
137       appendSeparated(person.getGivenName(), builder);
138       return builder.toString();
139     }
140   }
141 
142 }