Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/RasapiTest/src/com/ibm/dump/tests/StringUtils.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2016, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.dump.tests;
23
24
import java.util.ArrayList;
25
import java.util.NoSuchElementException;
26
import java.util.StringTokenizer;
27
28
29
public class StringUtils {
30
31
/**
32
* Check that the line contains a given value at a given position.
33
* Whether it does or not, write a line to System.err to trace progress.
34
*
35
* @param line the line to check
36
* @param whichWord which word in the line to check
37
* @param requiredValue the number to look for
38
* @return true or false depending on whether the line does indeed contain the given number at the given word
39
*/
40
static boolean linePassesCheckForStringAtWord(String line, int whichWord, String requiredValue) {
41
StringTokenizer st = new StringTokenizer(line);
42
String word = "";
43
try {
44
for (int i = 0;i < whichWord; i++) {
45
word = st.nextToken();
46
}
47
} catch (NoSuchElementException e) {
48
System.err.printf("***Failed to find the value %s at word %d in line \"%s\", no such element\n", requiredValue, whichWord, line, word) ;
49
return false;
50
}
51
if (word.equals(requiredValue)) {
52
System.err.printf("Successfully found the value %s at word %d in line \"%s\"\n", requiredValue, whichWord, line) ;
53
return true;
54
} else {
55
System.err.printf("***Failed to find the value %s at word %d in line \"%s\", found %s instead\n", requiredValue, whichWord, line, word) ;
56
return false;
57
}
58
59
}
60
61
/**
62
* Check that the line contains one of a number of given values at a given position.
63
* Whether it does or not, write a line to System.err to trace progress.
64
*
65
* @param line the line to check
66
* @param whichWord which word in the line to check
67
* @param requiredValue the number to look for
68
* @return true or false depending on whether the line does indeed contain the given number at the given word
69
*/
70
static boolean linePassesCheckForStringsAtWord(String line, int whichWord, String[] requiredValues) {
71
StringTokenizer st = new StringTokenizer(line);
72
String word = "";
73
try {
74
for (int i = 0;i < whichWord; i++) {
75
word = st.nextToken();
76
}
77
} catch (NoSuchElementException e) {
78
System.err.printf("***Failed to find any value at word %d in line \"%s\", no such element\n", whichWord, line, word) ;
79
return false;
80
}
81
for (String requiredValue : requiredValues) {
82
if (word.equals(requiredValue)) {
83
System.err.printf("Successfully found the value %s at word %d in line \"%s\"\n", requiredValue, whichWord, line) ;
84
return true;
85
}
86
}
87
System.err.printf("***Failed to find any of the required values at word %d in line \"%s\", found %s instead. ", whichWord, line, word) ;
88
System.err.printf("The required values were:") ;
89
for (String requiredValue : requiredValues) {
90
System.err.printf(requiredValue + " ");
91
}
92
System.err.printf("\n");
93
return false;
94
}
95
96
97
/**
98
* Given a line break it into an array of tokens (words)
99
* @param line
100
* @return arraylist of tokens (words)
101
*/
102
public static ArrayList<String> extractTokensFromLine(String line) {
103
ArrayList<String> tokens = new ArrayList<String>();
104
StringTokenizer st = new StringTokenizer(line);
105
while (st.hasMoreTokens()) {
106
String word = st.nextToken();
107
tokens.add(word);
108
}
109
return tokens;
110
}
111
112
113
}
114
115