
com.appslandia.common.base.Language Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of appslandia-common-cp Show documentation
Show all versions of appslandia-common-cp Show documentation
AppsLandia Common - Java Utilities
The newest version!
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.appslandia.common.base;
import java.util.Locale;
import java.util.Objects;
import com.appslandia.common.utils.AssertUtils;
import com.appslandia.common.utils.StringUtils;
/**
*
* @author Loc Ha
*
*/
public class Language extends InitializeObject {
public static final String FORMAT_TIME_24H = "HH:mm";
public static final String FORMAT_TIME_24H_Z = "HH:mmXXX";
public static final String FORMAT_TIME = "HH:mm:ss.SSS";
public static final String FORMAT_TIME_Z = "HH:mm:ss.SSSXXX";
public static final Language EN = new Language().setLocale(Locale.US).setDateFormat("MM/dd/yyyy").initialize();
public static final Language VI = new Language().setLocale(new Locale("vi", "VN")).setDateFormat("dd/MM/yyyy").initialize();
private Locale locale;
private String dateFormat;
private String dateTimeFormat;
private String dateTimeZFormat;
private String dateTime24HFormat;
private String dateTime24HZFormat;
@Override
protected void init() throws Exception {
AssertUtils.assertNotNull(this.locale, "locale is required.");
AssertUtils.assertNotNull(this.dateFormat, "dateFormat is required.");
this.dateTimeFormat = String.format("%s %s", this.dateFormat, FORMAT_TIME);
this.dateTimeZFormat = String.format("%s %s", this.dateFormat, FORMAT_TIME_Z);
this.dateTime24HFormat = String.format("%s %s", this.dateFormat, FORMAT_TIME_24H);
this.dateTime24HZFormat = String.format("%s %s", this.dateFormat, FORMAT_TIME_24H_Z);
}
public String getId() {
this.initialize();
return this.locale.getLanguage();
}
public String getDisplayLanguage() {
this.initialize();
return this.locale.getDisplayLanguage();
}
public Locale getLocale() {
this.initialize();
return this.locale;
}
public Language setLocale(Locale locale) {
this.assertNotInitialized();
this.locale = locale;
return this;
}
public String getDateFormat() {
this.initialize();
return this.dateFormat;
}
public Language setDateFormat(String dateFormat) {
this.assertNotInitialized();
this.dateFormat = StringUtils.trimToNull(dateFormat);
return this;
}
public String getDateTimeFormat() {
this.initialize();
return this.dateTimeFormat;
}
public String getDateTimeZFormat() {
this.initialize();
return this.dateTimeZFormat;
}
public String getDateTime24HFormat() {
this.initialize();
return this.dateTime24HFormat;
}
public String getDateTime24HZFormat() {
this.initialize();
return this.dateTime24HZFormat;
}
public String getTime24HFormat() {
this.initialize();
return FORMAT_TIME_24H;
}
public String getTime24HZFormat() {
this.initialize();
return FORMAT_TIME_24H_Z;
}
public String getTimeFormat() {
this.initialize();
return FORMAT_TIME;
}
public String getTimeZFormat() {
this.initialize();
return FORMAT_TIME_Z;
}
@Override
public int hashCode() {
return Objects.hash(this.getId().toLowerCase(Locale.ENGLISH));
}
@Override
public boolean equals(Object obj) {
if ((obj == null) || ((obj instanceof Language) == false)) {
return false;
}
Language another = (Language) obj;
return this.getId().equalsIgnoreCase(another.getId());
}
private static volatile Language current;
private static final Object __mutex = new Object();
public static Language getCurrent() {
Language obj = current;
if (obj == null) {
synchronized (__mutex) {
if ((obj = current) == null) {
current = obj = initLanguage();
}
}
}
return obj;
}
public static void setCurrent(Language obj) {
AssertUtils.assertNull(current, "Language.current is already set.");
current = obj;
}
public static final String SYSTEM_PROP_LANGUAGE = "__language";
private static Language initLanguage() {
try {
String implName = System.getProperty(SYSTEM_PROP_LANGUAGE);
if (implName == null) {
return EN;
}
Class> implClass = null;
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl != null) {
implClass = cl.loadClass(implName);
}
} catch (ClassNotFoundException ignore) {
}
if (implClass == null) {
try {
implClass = Language.class.getClassLoader().loadClass(implName);
} catch (ClassNotFoundException ignore) {
}
}
Object impl = implClass.newInstance();
return Language.class.cast(impl);
} catch (Exception ex) {
throw new InitializeException(ex);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy