001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.util;
015
016import ch.qos.logback.core.CoreConstants;
017
018import java.util.ArrayList;
019import java.util.List;
020
021/**
022 * Utility class for analysing logger names.
023 */
024public class LoggerNameUtil {
025
026    public static int getFirstSeparatorIndexOf(String name) {
027        return getSeparatorIndexOf(name, 0);
028    }
029
030    /**
031     * Get the position of the separator character, if any, starting at position
032     * 'fromIndex'.
033     *
034     * @param name
035     * @param fromIndex
036     * @return
037     */
038    public static int getSeparatorIndexOf(String name, int fromIndex) {
039        int dotIndex = name.indexOf(CoreConstants.DOT, fromIndex);
040        int dollarIndex = name.indexOf(CoreConstants.DOLLAR, fromIndex);
041
042        if (dotIndex == -1 && dollarIndex == -1)
043            return -1;
044        if (dotIndex == -1)
045            return dollarIndex;
046        if (dollarIndex == -1)
047            return dotIndex;
048
049        return dotIndex < dollarIndex ? dotIndex : dollarIndex;
050    }
051
052    public static List<String> computeNameParts(String loggerName) {
053        List<String> partList = new ArrayList<String>();
054
055        int fromIndex = 0;
056        while (true) {
057            int index = getSeparatorIndexOf(loggerName, fromIndex);
058            if (index == -1) {
059                partList.add(loggerName.substring(fromIndex));
060                break;
061            }
062            partList.add(loggerName.substring(fromIndex, index));
063            fromIndex = index + 1;
064        }
065        return partList;
066    }
067}