1 /*
2 * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package compiler.ciReplay;
25
26 import jdk.test.lib.Asserts;
27
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.nio.file.StandardOpenOption;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 public class ReplayFile {
37 private final Path replayFilePath;
38 private final List<String> replayFile;
39
40 public ReplayFile(String replayFileName) {
41 try {
42 this.replayFilePath = Paths.get(replayFileName);
43 this.replayFile = Files.readAllLines(replayFilePath);
44 } catch (IOException ioe) {
45 throw new Error("Failed to read/write replay data: " + ioe, ioe);
46 }
47 }
48
49 public void removeLineStartingWith(String oldLine) {
50 replaceLineStartingWith(oldLine, "");
51 }
52
53 public String findLineStartingWith(String toFind) {
54 return replayFile.stream()
55 .filter(line -> line.startsWith(toFind))
56 .findFirst()
57 .orElse("");
58 }
59
60 public void replaceLineStartingWith(String oldLine, String newLine) {
61 boolean foundOldLine = false;
62 List<String> newReplayFile = new ArrayList<>();
63 for (String line : replayFile) {
64 if (line.startsWith(oldLine)) {
65 foundOldLine = true;
66 if (!newLine.isEmpty()) {
67 // Only add if non-empty. Otherwise, line removal.
68 newReplayFile.add(newLine);
69 }
70 } else {
71 newReplayFile.add(line);
72 }
73 }
74 Asserts.assertTrue(foundOldLine, "Did not find oldLine \"" + oldLine + "\" in " + replayFilePath);
75 try {
76 Files.write(replayFilePath, newReplayFile, StandardOpenOption.TRUNCATE_EXISTING);
77 } catch (IOException ioe) {
78 throw new Error("Failed to read/write replay data: " + ioe, ioe);
79 }
80 }
81 }