Skip to main content
summaryrefslogtreecommitdiffstats
blob: fba728e57cc59e8e3494335c8b035e866d790848 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Copyright (c) 2008 Arno Haase.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.backend.syslib;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.xtend.backend.common.EfficientLazyString;
import org.eclipse.xtend.backend.common.ExecutionContext;
import org.eclipse.xtend.backend.common.Helpers;
import org.eclipse.xtend.middleend.javaannotations.AbstractExecutionContextAware;
import org.eclipse.xtend.middleend.javaannotations.M2tHidden;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public final class StringOperations extends AbstractExecutionContextAware {
    
    public String toString (Object o) {
        if (o == null) 
            return "";
        
        return String.valueOf (o);
    }
    
    public boolean startsWith (String s1, String s2) {
        if (s1 == null || s2 == null) {
            _ctx.logNullDeRef (null);
            return false;
        }
        
        return s1.startsWith (s2);
    }
    
    public boolean endsWith (String s1, String s2) {
        if (s1 == null || s2 == null) {
            _ctx.logNullDeRef (null);
            return false;
        }
        
        return s1.endsWith (s2);
    }
    
    public boolean contains (String s1, String s2) {
        if (s1 == null || s2 == null) {
            _ctx.logNullDeRef (null);
            return false;
        }
        
        return s1.contains (s2);
    }

    public String substring (String s, int indexFrom) {
        if (s == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.substring (indexFrom);
    }
    
    public CharSequence substring (CharSequence s, int indexFrom, int indexTo) {
        if (s == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.subSequence (indexFrom, indexTo);
    }

    public String toUpperCase (String s) {
        if (s == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.toUpperCase(); 
    }
    
    public String toLowerCase (String s) {
        if (s == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.toLowerCase(); 
    }
    
    public String toFirstUpper (String s) {
        if (s == null) {
            _ctx.logNullDeRef (null);
            return null;
        }

        if (s.length() < 2)
            return s.toUpperCase();
        
        return s.substring (0, 1).toUpperCase() + s.substring (1);
    }
    
    public String toFirstLower (String s) {
        if (s == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        if (s.length() < 2)
            return s.toLowerCase();
        
        return s.substring (0, 1).toLowerCase() + s.substring (1);
    }

    public List<String> toCharList (String s) {
        final List<String> result = new ArrayList<String> ();
        
        if (s == null) {
            _ctx.logNullDeRef (null);
            return result;
        }
        
        for (char ch: s.toCharArray())
            result.add ("" + ch);
        
        return result;
    }

    public String replace (String s, CharSequence searchString, CharSequence replaceString) {
        if (s == null || searchString == null || replaceString == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.replace (searchString, replaceString);
    }
    
    public String replaceFirstUsingRegex (String s, String searchString, String replaceString) {
        if (s == null || searchString == null || replaceString == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.replaceFirst (searchString, replaceString);
    }
    
    public String replaceAllUsingRegex (String s, String searchString, String replaceString) {
        if (s == null || searchString == null || replaceString == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        
        return s.replaceAll (searchString, replaceString);
    }
    
    public String[] split (String s, String regex) {
        if (s == null || regex == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
        return s.split (regex);
    }
    
    public Boolean matches (String s, String regex) {
        if (s == null || regex == null) {
            _ctx.logNullDeRef (null);
            return null;
        }
            
        return s.matches (regex);
    }
    
    public String trim (String s) {
        if (s == null)
            return null;
        
        return s.trim ();
    }
    
    public Long asInteger (String s) {
        if (s == null)
            return null;
        
        return Long.valueOf (s);
    }
    
    
    public CharSequence operatorPlus (Object o1, Object o2) {
        return concat (o1, o2);
    }

    /**
     * This method is duplicated with a different name so as to allow explicit concatenation
     *  even if '+' is defined for the concrete types.
     */
    public CharSequence concat (Object o1, Object o2) {
        if (o1 instanceof EfficientLazyString) 
            return EfficientLazyString.createAppendedString ((EfficientLazyString) o1, overridableToString (_ctx, o2));
        
        EfficientLazyString result = new EfficientLazyString ();

        result = EfficientLazyString.createAppendedString (result, overridableToString (_ctx, o1));
        result = EfficientLazyString.createAppendedString (result, overridableToString (_ctx, o2));
        
        return result;
    }

    /**
     * This method is public static so as to be available as a helper method for all code that needs to call "toString".
     *  It calls "toString" on an object, taking into account all potential overriding extensions.<br>
     *  
     * This method is however not itself intended to be published as an extension. It is a helper function, intended to
     *  be used by other extensions.
     */
    @M2tHidden
    public static CharSequence overridableToString (ExecutionContext ctx, Object o) {
        return Helpers.overridableToString (ctx, o);
    }
}

Back to the top