Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,63 @@
);
```

### Splitting Text

To split text using Java's standard behavior:

```java
Iterable<Text> parts = new Split("hello,world", ",");
// Result: ["hello", "world"]
```

### Splitting with Preserved Empty Tokens

Java's `String.split()` discards trailing empty tokens.
Use `SplitPreserveAllTokens` when you need to preserve ALL tokens,
including empty ones created by adjacent or trailing delimiters:

```java
// Standard split loses trailing empty token
"a,b,".split(",") // Returns: ["a", "b"] - trailing empty LOST!

// SplitPreserveAllTokens preserves it
Iterable<Text> parts = new SplitPreserveAllTokens("a,b,", ",");
// Result: ["a", "b", ""] - all tokens preserved!
```

This is essential for parsing CSV/TSV data where empty fields are meaningful:

```java
// Parse CSV with empty fields
Iterable<Text> fields = new SplitPreserveAllTokens(
"John,,Smith,,"
","
);
// Result: ["John", "", "Smith", "", ""] - all 5 fields!

// With spaces as delimiter
Iterable<Text> words = new SplitPreserveAllTokens(" hello world ");
// Result: ["", "hello", "", "world", ""]

// Default delimiter is space
Iterable<Text> tokens = new SplitPreserveAllTokens("a b c");
// Result: ["a", "b", "c"]

// With limit on number of tokens
Iterable<Text> limited = new SplitPreserveAllTokens("a,b,c,d", ",", 2);
// Result: ["a", "b"]
```

**Key guarantee**: With N delimiters, you always get exactly N+1 tokens.

| Input | Delimiter | `String.split()` | `SplitPreserveAllTokens` |
|-------|-----------|------------------|--------------------------|
| `"a,b,"` | `,` | `["a", "b"]` | `["a", "b", ""]` |
| `",,"` | `,` | `[]` | `["", "", ""]` |
| `","` | `,` | `[]` | `["", ""]` |

> **Note**: The delimiter is matched as a literal string, not a regex.

## Iterables/Collections/Lists/Sets

More about it here:
Expand Down Expand Up @@ -254,6 +311,53 @@
);
```

## Maps

To create a simple map:

```java
Map<String, Integer> map = new MapOf<>(
new MapEntry<>("one", 1),
new MapEntry<>("two", 2),
new MapEntry<>("three", 3)
);
```

### Immutable Maps

To create an immutable (read-only) map that prevents any modifications:

```java
Map<String, Integer> map = new org.cactoos.map.Immutable<>(
new MapOf<>(
new MapEntry<>("one", 1),
new MapEntry<>("two", 2)
)
);
map.get("one"); // returns 1
map.put("three", 3); // throws UnsupportedOperationException!
map.clear(); // throws UnsupportedOperationException!
```

The `Immutable` map decorator guarantees that:
- All mutating methods (`put`, `remove`, `putAll`, `clear`) throw `UnsupportedOperationException`

Check failure on line 343 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Lists should be surrounded by blank lines

README.md:343 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- All mutating methods (`put`,..."] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md032.md
- Views returned by `keySet()`, `values()`, and `entrySet()` are also immutable
- Even `Entry.setValue()` is blocked on entries from `entrySet()`

This is useful when you need to pass a map to untrusted code or ensure
a map cannot be accidentally modified:

```java
// Safe to pass to any method - cannot be modified
public Map<String, Config> getConfiguration() {
return new Immutable<>(this.config);
}
```

> **Note**: This is a decorator, not a copy. If the underlying map is modified
> through another reference, changes will be visible. For a true snapshot,
> copy the data first.

## Funcs and Procs

This is a traditional `foreach` loop:
Expand Down Expand Up @@ -326,11 +430,12 @@

## Our Objects vs. Their Static Methods

Cactoos | Guava | Apache Commons | JDK 8

Check failure on line 433 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:433:40 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing trailing pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md

Check failure on line 433 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:433:1 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing leading pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md
------ | ------ | ------ | ------

Check failure on line 434 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:434:33 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing trailing pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md

Check failure on line 434 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:434:1 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing leading pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md
`And` | `Iterables.all()` | - | -

Check failure on line 435 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:435:33 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing trailing pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md

Check failure on line 435 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:435:1 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing leading pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md
`Filtered` | `Iterables.filter()` | ? | -

Check failure on line 436 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:436:41 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing trailing pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md

Check failure on line 436 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:436:1 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing leading pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md
`FormattedText` | - | - | `String.format()`

Check failure on line 437 in README.md

View workflow job for this annotation

GitHub Actions / markdown-lint

Table pipe style

README.md:437:1 MD055/table-pipe-style Table pipe style [Expected: leading_and_trailing; Actual: no_leading_or_trailing; Missing leading pipe] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md055.md
`map.Immutable` | `ImmutableMap` | `UnmodifiableMap` | `Collections.unmodifiableMap()`
`IsBlank` | - | `StringUtils.isBlank()`| -
`Joined` | - | - | `String.join()`
`LengthOf` | - | - | `String#length()`
Expand All @@ -342,6 +447,7 @@
`Reversed` | - | - | `StringBuilder#reverse()`
`Rotated` | - | `StringUtils.rotate()`| -
`Split` | - | - | `String#split()`
`SplitPreserveAllTokens` | - | `StringUtils.splitPreserveAllTokens()` | -
`StickyList` | `Lists.newArrayList()` | ? | `Arrays.asList()`
`Sub` | - | - | `String#substring()`
`SwappedCase` | - | `StringUtils.swapCase()` | -
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/org/cactoos/func/TriFuncSplitPreserve.java

This file was deleted.

Loading
Loading