07332dd68a8c419c5581cdc001fe1220d80313e5
[aaf/cadi.git] / core / src / main / java / com / att / cadi / util / Split.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.util;\r
24 \r
25 /**\r
26  * Split by Char, optional Trim\r
27  *\r
28  * Note: Copied from Inno to avoid linking issues.\r
29  * Note: I read the String split and Pattern split code, and we can do this more efficiently for a single Character\r
30  * \r
31  * 8/20/2015\r
32  */\r
33 \r
34 public class Split {\r
35           public static String[] split(char c, String value) {\r
36                   // Count items to preallocate Array (memory alloc is more expensive than counting twice)\r
37                   int count,idx;\r
38                   for(count=1,idx=value.indexOf(c);idx>=0;idx=value.indexOf(c,++idx),++count);\r
39                   String[] rv = new String[count];\r
40                   if(count==1) {\r
41                           rv[0]=value;\r
42                   } else {\r
43                           int last=0;\r
44                           count=-1;\r
45                           for(idx=value.indexOf(c);idx>=0;idx=value.indexOf(c,idx)) {\r
46                                   rv[++count]=value.substring(last,idx);\r
47                                   last = ++idx;\r
48                           }\r
49                           rv[++count]=value.substring(last);\r
50                   }\r
51                   return rv;\r
52           }\r
53 \r
54           public static String[] splitTrim(char c, String value) {\r
55                   // Count items to preallocate Array (memory alloc is more expensive than counting twice)\r
56                   int count,idx;\r
57                   for(count=1,idx=value.indexOf(c);idx>=0;idx=value.indexOf(c,++idx),++count);\r
58                   String[] rv = new String[count];\r
59                   if(count==1) {\r
60                           rv[0]=value.trim();\r
61                   } else {\r
62                           int last=0;\r
63                           count=-1;\r
64                           for(idx=value.indexOf(c);idx>=0;idx=value.indexOf(c,idx)) {\r
65                                   rv[++count]=value.substring(last,idx).trim();\r
66                                   last = ++idx;\r
67                           }\r
68                           rv[++count]=value.substring(last).trim();\r
69                   }\r
70                   return rv;\r
71           }\r
72 \r
73           public static String[] splitTrim(char c, String value, int size) {\r
74                   int idx;\r
75                   String[] rv = new String[size];\r
76                   if(size==1) {\r
77                           rv[0]=value.trim();\r
78                   } else {\r
79                           int last=0;\r
80                           int count=-1;\r
81                           size-=2;\r
82                           for(idx=value.indexOf(c);idx>=0 && count<size;idx=value.indexOf(c,idx)) {\r
83                                   rv[++count]=value.substring(last,idx).trim();\r
84                                   last = ++idx;\r
85                           }\r
86                           rv[++count]=value.substring(last).trim();\r
87                   }\r
88                   return rv;\r
89           }\r
90 \r
91 }\r