Java tutorial
/* * Copyright 2016 OPEN TONE Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.co.opentone.bsol.linkbinder.util; import java.io.Serializable; import java.text.DecimalFormat; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang.StringUtils; import jp.co.opentone.bsol.linkbinder.dto.CorresponGroupUser; import jp.co.opentone.bsol.linkbinder.dto.User; /** * ??. * @author opentone */ public class ValueFormatter implements Serializable { /** * SerialVersionUID. */ private static final long serialVersionUID = 4315647716120851740L; /** * ?????. */ public static final String MATCH = ""; /** * ?. */ public static final String DELIM_USER = "/"; /** * ?. */ public static final String DELIM_CODE = " : "; /** * ??. */ private static final String FORMAT_ROLE = " (%s)"; /** * ?. */ private static final String FORMAT_NUMBER = "#,###"; /** * (?). */ private static final String BRACKETS_LEFT = " ["; /** * (??). */ private static final String BRACKETS_RIGHT = "]"; /** * ?????????. */ private static final Long JOIN_NO_GROUP = -1L; /** * . */ private ValueFormatter() { } /** * ????????. * ?{@link #DELIM_CODE}. * @param code * @param name ??? * @return ? */ public static String formatCodeAndName(String code, String name) { return formatCodeAndName(code, name, DELIM_CODE); } /** * ????????. * ?. * @param code * @param name ??? * @param delim * @return ? */ public static String formatCodeAndName(String code, String name, String delim) { String str = convertNullValue(code) + delim + convertNullValue(name); if (delim.equals(str)) { return ""; } return str; } /** * ?NULL??????. * ?NULL?????????????. * @param value * @return or */ private static String convertNullValue(String value) { if (value == null) { return ""; } return value; } /** * ??????????. * ??????. * @param value * @param code ? * @return */ public static String formatLabel(Object value, Object code) { if (code == null || !code.equals(value)) { return ""; } return MATCH; } /** * ??????????. * @param user * @return */ public static String formatUserNameAndEmpNo(User user) { String str = convertNullValue(user.getNameE()) + DELIM_USER + convertNullValue(user.getEmpNo()); if (DELIM_USER.equals(str)) { return ""; } return str; } /** * ????????????. * @param user * @return */ public static String formatUserNameAndEmpNoAndRole(User user) { String str = formatUserNameAndEmpNo(user); if (!StringUtils.isEmpty(user.getRole())) { str += String.format(FORMAT_ROLE, user.getRole()); } return str; } /** * ???????????. * @param correspoUser * @param id ?ID * @return */ public static String formatUserNameAndEmpNoAndGroup(CorresponGroupUser correspoUser, Long id) { String str = convertNullValue(correspoUser.getUser().getNameE()) + DELIM_USER + convertNullValue(correspoUser.getUser().getEmpNo()); if (DELIM_USER.equals(str)) { return ""; } if (!id.equals(JOIN_NO_GROUP)) { str = str + BRACKETS_LEFT + convertNullValue(correspoUser.getCorresponGroup().getName()) + BRACKETS_RIGHT; } return str; } /** * ????????????. * @param correspoUser * @param id ?ID * @return */ public static String formatUserNameAndEmpNoAndRoleAndGroup(CorresponGroupUser correspoUser, Long id) { String str = convertNullValue(correspoUser.getUser().getNameE()) + DELIM_USER + convertNullValue(correspoUser.getUser().getEmpNo()); if (DELIM_USER.equals(str)) { return ""; } if (!StringUtils.isEmpty(correspoUser.getUser().getRole())) { str += String.format(FORMAT_ROLE, correspoUser.getUser().getRole()); } if (!id.equals(JOIN_NO_GROUP)) { str = str + BRACKETS_LEFT + convertNullValue(correspoUser.getCorresponGroup().getName()) + BRACKETS_RIGHT; } return str; } /** * ?(9,999,999)???. * @param number * @return */ public static String formatNumber(int number) { DecimalFormat formatter = new DecimalFormat(FORMAT_NUMBER); return formatter.format(number); } /** * ??SHA256??? * @param value ?? * @param key * @return ?? */ public static String formatValueToHash(String value, String key) { byte[] result; try { SecretKeySpec sk = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(sk); result = mac.doFinal(value.getBytes("UTF-8")); } catch (Exception e) { throw new RuntimeException(e); } return new String(Hex.encodeHex(result)); } }