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