programming:java:java
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming:java:java [2023/10/30 07:46] – skipidar | programming:java:java [2023/11/02 14:33] (current) – [Records] skipidar | ||
|---|---|---|---|
| Line 1065: | Line 1065: | ||
| + | |||
| + | ===== JUnit 5 ===== | ||
| + | |||
| + | <sxh groovy> | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Hamcrest ===== | ||
| + | |||
| + | Has matches useful for unit tests. | ||
| + | |||
| + | Set the groovy dependencies | ||
| + | <sxh groovy> | ||
| + | dependencies { | ||
| + | testImplementation ' | ||
| + | testImplementation ' | ||
| + | |||
| + | // import here | ||
| + | testImplementation ' | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Now you can import " | ||
| + | |||
| + | <sxh java> | ||
| + | import static org.hamcrest.MatcherAssert.assertThat; | ||
| + | import static org.hamcrest.Matchers.*; | ||
| + | |||
| + | import org.junit.jupiter.api.Test; | ||
| + | |||
| + | public class MyTest { | ||
| + | |||
| + | @Test | ||
| + | void testExample() { | ||
| + | String value = " | ||
| + | | ||
| + | assertThat(value, | ||
| + | assertThat(value, | ||
| + | assertThat(value, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Text processing ===== | ||
| + | |||
| + | ==== Regular Expressions (java.util.regex) ==== | ||
| + | |||
| + | When using Regex - make sure you dont introduce too greedy patterns | ||
| + | |||
| + | https:// | ||
| + | < | ||
| + | Boundary matchers | ||
| + | ^ The beginning of a line | ||
| + | $ The end of a line | ||
| + | \b A word boundary | ||
| + | \B A non-word boundary | ||
| + | \A The beginning of the input | ||
| + | \G The end of the previous match | ||
| + | \Z The end of the input but for the final terminator, if any | ||
| + | \z The end of the input | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Tokenize strings ==== | ||
| + | |||
| + | Break into single words. | ||
| + | |||
| + | <sxh java> | ||
| + | |||
| + | String[] result = "this is a test" | ||
| + | for (int x=0; x< | ||
| + | System.out.println(result[x]); | ||
| + | |||
| + | /* this | ||
| + | is | ||
| + | a | ||
| + | test | ||
| + | */ | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | <sxh java> | ||
| + | @Test | ||
| + | void patternMatch() { | ||
| + | mymatch(" | ||
| + | mymatch(" | ||
| + | } | ||
| + | |||
| + | public static long mymatch(String regex, String text) { | ||
| + | Pattern pattern = Pattern.compile(regex); | ||
| + | Matcher matcher = pattern.matcher(text); | ||
| + | |||
| + | List< | ||
| + | .results() | ||
| + | .toList(); | ||
| + | |||
| + | System.out.printf(" | ||
| + | return res.size(); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== ReplaceAll | ||
| + | |||
| + | <sxh java> | ||
| + | @Test | ||
| + | void stringReplaceAll() { | ||
| + | String s = "bob and alice like to text each other"; | ||
| + | String john = s.replaceAll(" | ||
| + | System.out.println(john); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Records ===== | ||
| + | First: Java 14 | ||
| + | |||
| + | |||
| + | |||
| + | <sxh java> | ||
| + | // some interface | ||
| + | interface MyBarkInterface{ | ||
| + | default void bark(){ | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | record MyPoint(int x, int y) implements MyBarkInterface{}; | ||
| + | |||
| + | // Cant inherit from Record - its final | ||
| + | //record MyPoint2(int x, int y) extends MyPoint{}; | ||
| + | |||
| + | |||
| + | @Test | ||
| + | void recordTest1() { | ||
| + | MyPoint p = new MyPoint(1, | ||
| + | |||
| + | System.out.println(p); | ||
| + | System.out.println(p.x()); | ||
| + | System.out.println(p.x()); | ||
| + | | ||
| + | p.bark(); // can access hiearchy | ||
| + | } | ||
| + | |||
| + | </ | ||
programming/java/java.1698651982.txt.gz · Last modified: by skipidar
