Created
August 15, 2024 02:52
-
-
Save kencoba/58bf3304e48980f770c2e0794d5fc5cf to your computer and use it in GitHub Desktop.
Cover all patterns of transactions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* | |
* Cover all patterns of transactions. | |
* | |
* @author kencoba | |
* | |
*/ | |
public class PatternsOfTransactions { | |
public static void main(String[] args) { | |
List<String> tranA = List.of("a.begin()","a.read(x)", "a.write(x)", "a.commit()"); | |
List<String> tranB = List.of("b.begin()","b.read(x)", "b.commit()"); | |
List<String> output = new ArrayList<String>(); | |
rec(tranA,tranB,output); | |
} | |
private static void rec(List<String> tranA, List<String> tranB, List<String> output) { | |
if(tranA.isEmpty() & tranB.isEmpty()) { | |
System.out.println(String.join("; ",output)); | |
return; | |
} | |
if (!tranA.isEmpty()) { | |
String a = tranA.getFirst(); | |
rec(removeFirst(tranA),tranB,appendElements(output,a)); | |
} | |
if (!tranB.isEmpty()) { | |
String b = tranB.getFirst(); | |
rec(tranA,removeFirst(tranB),appendElements(output,b)); | |
} | |
} | |
@SafeVarargs | |
static <T> List<T> appendElements(List<T> immutableList, T... elements) { | |
List<T> tmpList = new ArrayList<>(immutableList); | |
tmpList.addAll(Arrays.asList(elements)); | |
return Collections.unmodifiableList(tmpList); | |
} | |
static <T> List<T> removeFirst(List<T> immutableList) { | |
List<T> tmpList = new ArrayList<>(immutableList); | |
tmpList.removeFirst(); | |
return Collections.unmodifiableList(tmpList); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment