Here you can find the source of splitName(String name)
Parameter | Description |
---|---|
name | A class name or non-empty package name. |
Parameter | Description |
---|---|
IllegalArgumentException | if name has dots in wrong places(corresponding to empty name parts) or is empty. |
NullPointerException | if the specified name is null. |
public static String[] splitName(String name)
//package com.java2s; /*//from w w w .ja v a2 s .c o m * Copyright 2015-2016 Jeff Hain * * 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. */ import java.util.ArrayList; public class Main { private static final char CHAR_DOT = '.'; private static final String STRING_DOT = "."; private static final String REGEX_DOT = "\\."; /** * @param name A class name or non-empty package name. * @return New array of dot-separated (and non-empty) names. * @throws IllegalArgumentException if name has dots in wrong places * (corresponding to empty name parts) or is empty. * @throws NullPointerException if the specified name is null. */ public static String[] splitName(String name) { if (false) { /* * Also works, but a bit slower, and less elegant. */ if (name.startsWith(STRING_DOT) || name.endsWith(STRING_DOT)) { throwIAEEmptyNameIn(name); } // If name is empty, parts will contain a single empty string, // so we will throw in the for loop. final String[] parts = name.split(REGEX_DOT); for (String part : parts) { if (part.length() == 0) { throwIAEEmptyNameIn(name); } } return parts; } final ArrayList<String> list = new ArrayList<String>(); int i = 0; while (true) { // Implicit null check. final int j = name.indexOf(CHAR_DOT, i); if (j < 0) { if (i == name.length()) { throwIAEEmptyNameIn(name); } list.add(name.substring(i)); break; } if (j == i) { throwIAEEmptyNameIn(name); } list.add(name.substring(i, j)); i = j + 1; } return list.toArray(new String[list.size()]); } private static void throwIAEEmptyNameIn(String name) { throw new IllegalArgumentException("empty name in " + name); } }