View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.stat;
18  
19  import java.util.Locale;
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  
23  import org.hipparchus.exception.Localizable;
24  import org.hipparchus.exception.UTF8Control;
25  
26  /**
27   * Enumeration for localized messages formats used in exceptions messages.
28   * <p>
29   * The constants in this enumeration represent the available formats as
30   * localized strings. These formats are intended to be localized using simple
31   * properties files, using the constant name as the key and the property value
32   * as the message format. The source English format is provided in the constants
33   * themselves to serve both as a reminder for developers to understand the
34   * parameters needed by each format, as a basis for translators to create
35   * localized properties files, and as a default format if some translation is
36   * missing.
37   * </p>
38   */
39  public enum LocalizedStatFormats implements Localizable {
40  
41      /** TIES_ARE_NOT_ALLOWED. */
42      TIES_ARE_NOT_ALLOWED("Ties are not allowed."),
43  
44      /** INSUFFICIENT_DATA_FOR_T_STATISTIC. */
45      INSUFFICIENT_DATA_FOR_T_STATISTIC("insufficient data for t statistic, needs at least 2, got {0}"),
46  
47      /** NOT_ENOUGH_DATA_REGRESSION. */
48      NOT_ENOUGH_DATA_REGRESSION("the number of observations is not sufficient to conduct regression"),
49  
50      /** INVALID_REGRESSION_OBSERVATION. */
51      INVALID_REGRESSION_OBSERVATION("length of regressor array = {0} does not match the number of variables = {1} in the model"),
52  
53      /** NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS. */
54      NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS("not enough data ({0} rows) for this many predictors ({1} predictors)"),
55  
56      /** NOT_SUPPORTED_NAN_STRATEGY. */
57      NOT_SUPPORTED_NAN_STRATEGY("NaN strategy {0} not supported"),
58  
59      /** NO_REGRESSORS. */
60      NO_REGRESSORS("Regression model must include at least one regressor"),
61  
62      /** COVARIANCE_MATRIX. */
63      COVARIANCE_MATRIX("covariance matrix"),
64  
65      /** OUT_OF_BOUNDS_QUANTILE_VALUE. */
66      OUT_OF_BOUNDS_QUANTILE_VALUE("out of bounds quantile value: {0}, must be in (0, 100]"),
67  
68      /** OUT_OF_BOUNDS_CONFIDENCE_LEVEL. */
69      OUT_OF_BOUNDS_CONFIDENCE_LEVEL("out of bounds confidence level {0}, must be between {1} and {2}"),
70  
71      /** OUT_OF_BOUND_SIGNIFICANCE_LEVEL. */
72      OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"),
73  
74      /** SIGNIFICANCE_LEVEL. */
75      SIGNIFICANCE_LEVEL("significance level ({0})"),
76  
77      /** TOO_MANY_REGRESSORS. */
78      TOO_MANY_REGRESSORS("too many regressors ({0}) specified, only {1} in the model"),
79  
80      /** TWO_OR_MORE_CATEGORIES_REQUIRED. */
81      TWO_OR_MORE_CATEGORIES_REQUIRED("two or more categories required, got {0}"),
82  
83      /** TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED. */
84      TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED("two or more values required in each category, one has {0}"),
85  
86      /** ILLEGAL_STATE_PCA. */
87      ILLEGAL_STATE_PCA("you must fit the PCA projection before calling {0}");
88  
89      /** Source English format. */
90      private final String sourceFormat;
91  
92      /**
93       * Simple constructor.
94       *
95       * @param sourceFormat source English format to use when no localized
96       *        version is available
97       */
98      LocalizedStatFormats(final String sourceFormat) {
99          this.sourceFormat = sourceFormat;
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public String getSourceString() {
105         return sourceFormat;
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public String getLocalizedString(final Locale locale) {
111         try {
112             final String path = LocalizedStatFormats.class.getName()
113                 .replaceAll("\\.", "/");
114             ResourceBundle bundle = ResourceBundle
115                 .getBundle("assets/" + path, locale, new UTF8Control());
116             if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
117                 final String translated = bundle.getString(name());
118                 if ((translated != null) && (translated.length() > 0) &&
119                     (!translated.toLowerCase(locale).contains("missing translation"))) {
120                     // the value of the resource is the translated format
121                     return translated;
122                 }
123             }
124 
125         } catch (MissingResourceException mre) { // NOPMD
126             // do nothing here
127         }
128 
129         // either the locale is not supported or the resource is unknown
130         // don't translate and fall back to using the source format
131         return sourceFormat;
132 
133     }
134 
135 }