3fa9a3f11e0071f9992da009613c1cd48c29b5e3
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / 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.cadi.util;
23
24 /**
25  * Split by Char, optional Trim
26  *
27  * Note: Copied from Inno to avoid linking issues.
28  * Note: I read the String split and Pattern split code, and we can do this more efficiently for a single Character
29  * 
30  * 8/20/2015
31  */
32
33 public class Split {
34           public static String[] split(char c, String value) {
35                   return split(c,value,0,value.length());
36           }
37
38           public static String[] split(char c, String value, int start, int end) {
39                   if(value==null) {
40                           return new String[0];
41                   }
42
43                   // Count items to preallocate Array (memory alloc is more expensive than counting twice)
44                   int count,idx;
45                   for(count=1,idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,++idx),++count);
46                   String[] rv = new String[count];
47                   if(count==1) {
48                           rv[0]=value.substring(start,end);
49                   } else {
50                           int last=0;
51                           count=-1;
52                           for(idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,idx)) {
53                                   rv[++count]=value.substring(last,idx);
54                                   last = ++idx;
55                           }
56                           rv[++count]=value.substring(last,end);
57                   }
58                   return rv;
59         }
60
61           public static String[] splitTrim(char c, String value, int start, int end) {
62                   if(value==null) {
63                           return new String[0];
64                   }
65
66                   // Count items to preallocate Array (memory alloc is more expensive than counting twice)
67                   int count,idx;
68                   for(count=1,idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,++idx),++count);
69                   String[] rv = new String[count];
70                   if(count==1) {
71                           rv[0]=value.substring(start,end).trim();
72                   } else {
73                           int last=0;
74                           count=-1;
75                           for(idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,idx)) {
76                                   rv[++count]=value.substring(last,idx).trim();
77                                   last = ++idx;
78                           }
79                           rv[++count]=value.substring(last,end).trim();
80                   }
81                   return rv;
82         }
83
84           public static String[] splitTrim(char c, String value) {
85                   return splitTrim(c,value,0,value.length());
86           }
87
88           public static String[] splitTrim(char c, String value, int size) {
89                   if(value==null) {
90                           return new String[0];
91                   }
92
93                   int idx;
94                   String[] rv = new String[size];
95                   if(size==1) {
96                           rv[0]=value.trim();
97                   } else {
98                           int last=0;
99                           int count=-1;
100                           size-=2;
101                           for(idx=value.indexOf(c);idx>=0 && count<size;idx=value.indexOf(c,idx)) {
102                                   rv[++count]=value.substring(last,idx).trim();
103                                   last = ++idx;
104                           }
105                           if(idx>0) {
106                                 rv[++count]=value.substring(last,idx).trim();
107                           } else {
108                                 rv[++count]=value.substring(last).trim();
109                           }
110                   }
111                   return rv;
112           }
113
114 }