001package io.ebean.util; 002 003/** 004 * Helper for dot notation property paths. 005 */ 006public class SplitName { 007 008 private static final char PERIOD = '.'; 009 010 /** 011 * Add the two name sections together in dot notation. 012 */ 013 public static String add(String prefix, String name) { 014 if (prefix != null) { 015 return prefix + "." + name; 016 } else { 017 return name; 018 } 019 } 020 021 /** 022 * Return the number of occurrences of char in name. 023 */ 024 public static int count(String name) { 025 026 int count = 0; 027 for (int i = 0; i < name.length(); i++) { 028 if (PERIOD == name.charAt(i)) { 029 count++; 030 } 031 } 032 return count; 033 } 034 035 /** 036 * Return the parent part of the path. 037 */ 038 public static String parent(String name) { 039 if (name == null) { 040 return null; 041 } else { 042 String[] s = split(name, true); 043 return s[0]; 044 } 045 } 046 047 /** 048 * Return the name split by last. 049 */ 050 public static String[] split(String name) { 051 return split(name, true); 052 } 053 054 /** 055 * Return the first part of the name. 056 */ 057 public static String begin(String name) { 058 return splitBegin(name)[0]; 059 } 060 061 public static String[] splitBegin(String name) { 062 return split(name, false); 063 } 064 065 private static String[] split(String name, boolean last) { 066 067 int pos = last ? name.lastIndexOf('.') : name.indexOf('.'); 068 if (pos == -1) { 069 if (last) { 070 return new String[]{null, name}; 071 } else { 072 return new String[]{name, null}; 073 } 074 } else { 075 String s0 = name.substring(0, pos); 076 String s1 = name.substring(pos + 1); 077 return new String[]{s0, s1}; 078 } 079 } 080 081}