Public and Private Locate entries
[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         private static final String[] EMPTY = new String[0];
35
36         public static String[] split(char c, String value) {
37                 if (value==null) {
38                         return EMPTY;
39                 }
40
41                 return split(c,value,0,value.length());
42         }
43
44         public static String[] split(char c, String value, int start, int end) {
45                 if (value==null) {
46                         return EMPTY;
47                 }
48
49                 // Count items to preallocate Array (memory alloc is more expensive than counting twice)
50                 int count,idx;
51                 for (count=1,idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,++idx),++count);
52                 String[] rv = new String[count];
53                 if (count==1) {
54                         rv[0]=value.substring(start,end);
55                 } else {
56                         int last=0;
57                         count=-1;
58                         for (idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,idx)) {
59                                 rv[++count]=value.substring(last,idx);
60                                 last = ++idx;
61                         }
62                         rv[++count]=value.substring(last,end);
63                 }
64                 return rv;
65         }
66
67         public static String[] splitTrim(char c, String value, int start, int end) {
68                 if (value==null) {
69                         return EMPTY;
70                 }
71
72                 // Count items to preallocate Array (memory alloc is more expensive than counting twice)
73                 int count,idx;
74                 for (count=1,idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,++idx),++count);
75                 String[] rv = new String[count];
76                 if (count==1) {
77                         rv[0]=value.substring(start,end).trim();
78                 } else {
79                         int last=0;
80                         count=-1;
81                         for (idx=value.indexOf(c,start);idx>=0 && idx<end;idx=value.indexOf(c,idx)) {
82                                 rv[++count]=value.substring(last,idx).trim();
83                                 last = ++idx;
84                         }
85                         rv[++count]=value.substring(last,end).trim();
86                 }
87                 return rv;
88         }
89
90         public static String[] splitTrim(char c, String value) {
91                 if (value==null) {
92                         return EMPTY;
93                 }
94                 return splitTrim(c,value,0,value.length());
95         }
96
97         public static String[] splitTrim(char c, String value, int size) {
98                 if (value==null) {
99                         return EMPTY;
100                 }
101
102                 int idx;
103                 String[] rv = new String[size];
104                 if (size==1) {
105                         rv[0]=value.trim();
106                 } else {
107                         int last=0;
108                         int count=-1;
109                         size-=2;
110                         for (idx=value.indexOf(c);idx>=0 && count<size;idx=value.indexOf(c,idx)) {
111                                 rv[++count]=value.substring(last,idx).trim();
112                                 last = ++idx;
113                         }
114                         if (idx>0) {
115                                 rv[++count]=value.substring(last,idx).trim();
116                         } else {
117                                 rv[++count]=value.substring(last).trim();
118                         }
119                 }
120                 return rv;
121         }
122
123 }