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.lang;
28  
29  import com.rapiddweller.common.LocaleUtil;
30  import com.rapiddweller.common.NullSafeComparator;
31  import com.rapiddweller.common.StringUtil;
32  import com.rapiddweller.format.DataContainer;
33  import com.rapiddweller.format.csv.CSVLineIterator;
34  
35  import java.io.IOException;
36  import java.util.Collection;
37  import java.util.HashSet;
38  import java.util.Locale;
39  import java.util.Set;
40  
41  /**
42   * Represents a Noun.<br/>
43   * <br/>
44   * Created at 15.07.2009 22:46:33
45   *
46   * @author Volker Bergmann
47   * @since 0.6.0
48   */
49  public class Noun {
50  
51    private final Language language;
52  
53    private final String singular;
54    private final String plural;
55    private final int gender;
56  
57    /**
58     * Instantiates a new Noun.
59     *
60     * @param singular the singular
61     * @param plural   the plural
62     * @param gender   the gender
63     * @param language the language
64     */
65    public Noun(String singular, String plural, int gender, Language language) {
66      this.singular = singular;
67      this.plural = plural;
68      this.gender = gender;
69      this.language = language;
70    }
71  
72    /**
73     * Gets instances.
74     *
75     * @param locale the locale
76     * @return the instances
77     * @throws IOException the io exception
78     */
79    public static Collection<Noun> getInstances(Locale locale)
80        throws IOException {
81      Language language = Language.getInstance(locale);
82      Set<Noun> nouns = new HashSet<>(500);
83      String url = LocaleUtil
84          .availableLocaleUrl("/com/rapiddweller/domain/lang/noun",
85              locale, ".csv");
86      CSVLineIterator iterator = new CSVLineIterator(url, ',', true);
87      DataContainer<String[]> container = new DataContainer<>();
88      while ((container = iterator.next(container)) != null) {
89        String[] line = container.getData();
90        String singular =
91            (StringUtil.isEmpty(line[0]) ? null : line[0].trim());
92        String plural;
93        if (line.length > 1 && !StringUtil.isEmpty(line[1])) {
94          plural = line[1].trim();
95          if (plural.startsWith("-")) {
96            plural = singular + plural.substring(1);
97          }
98        } else {
99          plural = null;
100       }
101       int gender = (line.length >= 3 ? Integer.parseInt(line[2]) : 0);
102       nouns.add(new Noun(singular, plural, gender, language));
103     }
104     return nouns;
105   }
106 
107   /**
108    * Gets singular.
109    *
110    * @return the singular
111    */
112   public String getSingular() {
113     return singular;
114   }
115 
116   /**
117    * Gets plural.
118    *
119    * @return the plural
120    */
121   public String getPlural() {
122     return plural;
123   }
124 
125   @Override
126   public String toString() {
127     StringBuilder builder = new StringBuilder();
128     if (singular != null) {
129       builder.append(language.definiteArticle(gender, false)).append(' ')
130           .append(singular);
131       if (plural != null) {
132         builder.append(", ");
133       }
134     }
135     if (plural != null) {
136       builder.append(language.definiteArticle(gender, true)).append(' ')
137           .append(plural);
138     }
139     return builder.toString();
140   }
141 
142   @Override
143   public int hashCode() {
144     int pHash = (plural == null ? 0 : plural.hashCode());
145     int sHash = (singular == null ? 0 : singular.hashCode());
146     return (pHash * 31 + sHash) * 31 + gender;
147   }
148 
149   @Override
150   public boolean equals(Object obj) {
151     if (this == obj) {
152       return true;
153     }
154     if (obj == null) {
155       return false;
156     }
157     if (getClass() != obj.getClass()) {
158       return false;
159     }
160     Nounef="../../../../com/rapiddweller/domain/lang/Noun.html#Noun">Noun that = (Noun) obj;
161     return NullSafeComparator.equals(this.singular, that.singular) &&
162         NullSafeComparator.equals(this.plural, that.plural) &&
163         this.gender == that.gender;
164   }
165 
166 }