Java tutorial
/** * Copyright (c) 2014 Baidu, Inc. All Rights Reserved. * * 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 com.baidu.rigel.biplatform.ac.util; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.baidu.rigel.biplatform.ac.minicube.MiniCubeDimension; import com.baidu.rigel.biplatform.ac.model.Measure; import com.baidu.rigel.biplatform.ac.model.Member; import com.baidu.rigel.biplatform.ac.model.OlapElement; /** * ???? * * @author xiaoming.chen * */ public class MetaNameUtil { /** * UNIQUE_NAME_PATTERN ?UniqueName? */ public static final String UNIQUE_NAME_FORMAT = "[%s]"; /** * SUMMARY_MEMBER_NAME_PRE all?? \\[ [^\\]\\[] \\] */ public static final String SUMMARY_MEMBER_NAME_PRE = "All_"; /** * UNIQUE_NAME_REGEX uniqueName */ public static final String UNIQUE_NAME_REGEX = "^\\[[^\\]\\[]+\\](\\.\\[[^\\]\\[]+\\])*$"; /** * LOGGER */ private static final Logger LOGGER = LoggerFactory.getLogger(MetaNameUtil.class); /** * UniqueName * * @param parent ? * @param name ??? * @return ?UniqueName */ public static String makeUniqueName(OlapElement parent, String name) { // all if (parent == null) { return makeUniqueName(name); } else { if (isAllMemberName(parent.getName()) && (parent instanceof Member)) { Member member = (Member) parent; parent = member.getLevel().getDimension(); } StringBuilder buf = new StringBuilder(64); buf.append(parent.getUniqueName()); buf.append('.'); buf.append(makeUniqueName(name)); return buf.toString(); } } /** * generateMeasureUniqueName * @param name * @return */ public static String generateMeasureUniqueName(String name) { return MetaNameUtil.makeUniqueName(Measure.MEASURE_DIMENSION_NAME) + "." + MetaNameUtil.makeUniqueName(name); } /** * ??UniqueName? * * @param uniqueName UniqueName * @return ?UniqueName */ public static boolean isUniqueName(String uniqueName) { if (StringUtils.isNotBlank(uniqueName) && Pattern.matches(UNIQUE_NAME_REGEX, uniqueName)) { return true; } return false; } /** * ?Membername? * * @param metaName membername * @return ?UniqueName */ public static String makeUniqueName(String metaName) { if (StringUtils.isBlank(metaName)) { throw new IllegalArgumentException("metaName can not be empty"); } return String.format(UNIQUE_NAME_FORMAT, metaName); } /** * UniqueName?? * * @param uniqueName UniqueName * @return */ public static String[] parseUnique2NameArray(String uniqueName) { if (!isUniqueName(uniqueName)) { throw new IllegalArgumentException("uniqueName is illegal:" + uniqueName); } String preSplitUniqueName = uniqueName; if (preSplitUniqueName.startsWith("[")) { preSplitUniqueName = preSplitUniqueName.substring(1); } if (preSplitUniqueName.endsWith("]")) { preSplitUniqueName = preSplitUniqueName.substring(0, preSplitUniqueName.length() - 2); } // ].[?? return StringUtils.split(uniqueName, "].["); } /** * getNameFromMetaName ??????? * @param metaName * @return */ public static String getNameFromMetaName(String metaName) { if (isUniqueName(metaName)) { String[] nameArr = parseUnique2NameArray(metaName); return nameArr[nameArr.length - 1]; } else { return metaName; } } /** * UniqueName?allUniqueName * * @param uniqueName UniqueName * @return ?all * @throws IllegalArgumentException unique?? */ public static boolean isAllMemberUniqueName(String uniqueName) { if (!isUniqueName(uniqueName)) { LOGGER.warn("uniqueName is illegal:" + uniqueName); return false; } String[] names = parseUnique2NameArray(uniqueName); if (names.length == 2 && isAllMemberName(names[1])) { return true; } return false; } /** * ?Allname * * @param name name * @return ?allname */ public static boolean isAllMemberName(String name) { if (StringUtils.startsWith(name, SUMMARY_MEMBER_NAME_PRE)) { return true; } return false; } /** * ?UniqueName?UniqueName * * @param uniqueName UniqueName * @return UniqueNamenull * @throws IllegalArgumentException unique?? */ public static String getParentUniqueName(String uniqueName) { if (!isUniqueName(uniqueName)) { throw new IllegalArgumentException("uniqueName is illegal:" + uniqueName); } String[] names = parseUnique2NameArray(uniqueName); if (names.length == 2) { return makeUniqueName(names[0]) + "." + makeUniqueName(String.format(MiniCubeDimension.ALL_DIMENSION_NAME_PATTERN, names[0])); } else if (names.length < 2) { return null; } return uniqueName.substring(0, uniqueName.lastIndexOf(".[")); } /** * UniqueName??? * @param uniqueName * @return */ public static String getDimNameFromUniqueName(String uniqueName) { String[] metaNames = parseUnique2NameArray(uniqueName); return metaNames[0]; } // public static void main(String[] args) { // String unique = "[trade].[1]"; // // System.out.println(Pattern.matches("^\\[[^\\]\\[]+\\](\\.\\[[^\\]\\[]+\\])*$", unique)); // System.out.println(getParentUniqueName(unique)); // // } }