Skip to content

Commit

Permalink
feat(#64): Set id for CachedTojo and rename Tojos.KEY -> Tojos.ID_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jun 22, 2023
1 parent e9d8a22 commit bc59969
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 28 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/yegor256/tojos/CachedTojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* The cached wrapper around Tojo class.
Expand Down Expand Up @@ -73,11 +74,20 @@ private CachedTojo(

@Override
public boolean exists(final String key) {
return this.cache.containsKey(key);
final boolean result;
if (key.equals(Tojos.ID_KEY)) {
result = true;
} else {
result = this.cache.containsKey(key);
}
return result;
}

@Override
public String get(final String key) {
if (Tojos.ID_KEY.equals(key) && !this.cache.containsKey(Tojos.ID_KEY)) {
this.cache.put(key, this.origin.get(key));
}
return this.cache.get(key);
}

Expand All @@ -87,4 +97,8 @@ public Tojo set(final String key, final Object value) {
this.origin.set(key, value);
return this;
}

private Optional<String> identifier() {
return Optional.ofNullable(this.cache.get(Tojos.ID_KEY));
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/yegor256/tojos/MnCsv.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public void write(final Collection<Map<String, String>> rows) {
final List<String> header = new ArrayList<>(keys.size());
header.addAll(keys);
Collections.sort(header);
if (header.contains(Tojos.KEY)) {
Collections.swap(header, 0, header.indexOf(Tojos.KEY));
if (header.contains(Tojos.ID_KEY)) {
Collections.swap(header, 0, header.indexOf(Tojos.ID_KEY));
}
final String[] values = new String[header.size()];
this.file.toFile().getParentFile().mkdirs();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/yegor256/tojos/MnJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ public void write(final Collection<Map<String, String>> rows) {
final JsonArrayBuilder array = Json.createArrayBuilder();
for (final Map<String, String> row : rows) {
final JsonObjectBuilder obj = Json.createObjectBuilder();
if (row.containsKey(Tojos.KEY)) {
obj.add(Tojos.KEY, row.get(Tojos.KEY));
if (row.containsKey(Tojos.ID_KEY)) {
obj.add(Tojos.ID_KEY, row.get(Tojos.ID_KEY));
}
for (final Map.Entry<String, String> ent : row.entrySet()) {
if (ent.getKey().equals(Tojos.KEY)) {
if (ent.getKey().equals(Tojos.ID_KEY)) {
continue;
}
obj.add(ent.getKey(), ent.getValue());
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/yegor256/tojos/MonoTojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class MonoTojo implements Tojo {
public boolean exists(final String key) {
synchronized (this.mono) {
return this.mono.read().stream()
.filter(row -> row.get(Tojos.KEY).equals(this.name))
.filter(row -> row.get(Tojos.ID_KEY).equals(this.name))
.findFirst()
.get()
.containsKey(key);
Expand All @@ -71,7 +71,7 @@ public boolean exists(final String key) {
public String get(final String key) {
synchronized (this.mono) {
final String value = this.mono.read().stream()
.filter(row -> row.get(Tojos.KEY).equals(this.name))
.filter(row -> row.get(Tojos.ID_KEY).equals(this.name))
.findFirst()
.get()
.get(key);
Expand All @@ -89,17 +89,17 @@ public String get(final String key) {
@Override
public Tojo set(final String key, final Object value) {
synchronized (this.mono) {
if (key.equals(Tojos.KEY)) {
if (key.equals(Tojos.ID_KEY)) {
throw new IllegalArgumentException(
String.format(
"It's illegal to use #set() to change '%s' attribute",
Tojos.KEY
Tojos.ID_KEY
)
);
}
final Collection<Map<String, String>> rows = this.mono.read();
final Map<String, String> row = rows.stream().filter(
r -> r.get(Tojos.KEY).equals(this.name)
r -> r.get(Tojos.ID_KEY).equals(this.name)
).findFirst().get();
row.put(key, value.toString());
this.mono.write(rows);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/yegor256/tojos/TjDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public String toString() {
public Tojo add(final String name) {
final Collection<Map<String, String>> rows = this.mono.read();
final Optional<Map<String, String>> before = rows.stream().filter(
r -> r.get(Tojos.KEY).equals(name)
r -> r.get(Tojos.ID_KEY).equals(name)
).findFirst();
if (!before.isPresent()) {
final Map<String, String> row = new HashMap<>(1);
row.put(Tojos.KEY, name);
row.put(Tojos.ID_KEY, name);
rows.add(row);
this.mono.write(rows);
}
Expand All @@ -80,7 +80,7 @@ public List<Tojo> select(final Predicate<Tojo> filter) {
final Collection<Map<String, String>> rows = this.mono.read();
final List<Tojo> tojos = new ArrayList<>(rows.size());
for (final Map<String, String> row : rows) {
final Tojo tojo = new MonoTojo(this.mono, row.get(Tojos.KEY));
final Tojo tojo = new MonoTojo(this.mono, row.get(Tojos.ID_KEY));
if (filter.test(tojo)) {
tojos.add(tojo);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yegor256/tojos/TjSmart.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String toString() {
*/
public Tojo getById(final String name) {
return this.origin
.select(tojo -> name.equals(tojo.get(Tojos.KEY)))
.select(tojo -> name.equals(tojo.get(Tojos.ID_KEY)))
.iterator()
.next();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yegor256/tojos/Tojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public interface Tojo {
/**
* Set attribute.
*
* <p>You can't set {@link Tojos#KEY} attribute. If you try to do
* <p>You can't set {@link Tojos#ID_KEY} attribute. If you try to do
* so, you will get a runtime exception.</p>
*
* @param key The name of the attribute
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yegor256/tojos/Tojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface Tojos extends Closeable {
/**
* Name of ID attribute in all tojos.
*/
String KEY = "id";
String ID_KEY = "id";

/**
* Add new tojo with the given ID.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/yegor256/tojos/MnCsvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void checksSimpleScenario(@TempDir final Path temp) {
Matchers.equalTo(0)
);
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
final String key = Tojos.ID_KEY;
final String value = "привет,\t\n \"друг\"!";
row.put(key, value);
rows.add(row);
Expand Down Expand Up @@ -99,7 +99,7 @@ void putsKeyFirst(@TempDir final Path temp) throws IOException {
final Mono csv = new MnCsv(path);
final Collection<Map<String, String>> rows = csv.read();
final Map<String, String> row = new HashMap<>(0);
row.put(Tojos.KEY, "xyz");
row.put(Tojos.ID_KEY, "xyz");
row.put("_x", "");
row.put("zzzz", "");
rows.add(row);
Expand All @@ -108,7 +108,7 @@ void putsKeyFirst(@TempDir final Path temp) throws IOException {
new String(Files.readAllBytes(path), StandardCharsets.UTF_8),
Matchers.matchesPattern(
Pattern.compile(
String.format("^\"%s\",.*", Tojos.KEY),
String.format("^\"%s\",.*", Tojos.ID_KEY),
Pattern.MULTILINE | Pattern.DOTALL
)
)
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/yegor256/tojos/MnJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void checksSimpleScenario(@TempDir final Path temp) {
Matchers.equalTo(0)
);
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
final String key = Tojos.ID_KEY;
final String value = "привет,\t\r\n друг!";
row.put(key, value);
rows.add(row);
Expand All @@ -80,7 +80,7 @@ void prints(@TempDir final Path temp) throws IOException {
final Mono json = new MnJson(path);
final Collection<Map<String, String>> rows = json.read();
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
final String key = Tojos.ID_KEY;
final String value = "hello, world!";
row.put(key, value);
rows.add(row);
Expand All @@ -98,7 +98,7 @@ void retrievesKeyAtFirstPosition(@TempDir final Path temp) throws IOException {
final Mono json = new MnJson(path);
final Collection<Map<String, String>> rows = json.read();
final Map<String, String> row = new HashMap<>(0);
row.put(Tojos.KEY, "xyz");
row.put(Tojos.ID_KEY, "xyz");
row.put("_x", "");
row.put("zzzz", "");
rows.add(row);
Expand All @@ -107,7 +107,7 @@ void retrievesKeyAtFirstPosition(@TempDir final Path temp) throws IOException {
new String(Files.readAllBytes(path), StandardCharsets.UTF_8),
Matchers.matchesPattern(
Pattern.compile(
String.format(".*\\{\\s+\"%s\":.*", Tojos.KEY),
String.format(".*\\{\\s+\"%s\":.*", Tojos.ID_KEY),
Pattern.MULTILINE | Pattern.DOTALL
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/yegor256/tojos/MnMemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void readsEmpty() {
void checksSimpleScenario() {
final Mono mono = new MnMemory();
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
final String key = Tojos.ID_KEY;
final String value = "привет,\t\n \"друг\"!";
row.put(key, value);
final Collection<Map<String, String>> rows = new ArrayList<>(0);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/yegor256/tojos/MnStickyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void readsFromEmpty() {
void checksSimpleScenario(@TempDir final Path temp) {
final Mono sticky = new MnSticky(new MnCsv(temp.resolve("x.csv")));
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
final String key = Tojos.ID_KEY;
final String value = "привет,\t\n \"друг\"!";
row.put(key, value);
final Collection<Map<String, String>> rows = new ArrayList<>(0);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/yegor256/tojos/MnSynchronizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static Collection<Map<String, String>> rowsByThreads() {
return Collections.nCopies(
MnSynchronizedTest.THREADS,
Collections.singletonMap(
Tojos.KEY,
Tojos.ID_KEY,
String.valueOf(MnSynchronizedTest.THREADS)
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/yegor256/tojos/MnTabsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void checksSimpleScenario(@TempDir final Path temp) {
Matchers.equalTo(0)
);
final Map<String, String> row = new HashMap<>(0);
final String key = Tojos.KEY;
final String key = Tojos.ID_KEY;
final String value = "привет,\t\r\n друг!";
row.put(key, value);
rows.add(row);
Expand Down

0 comments on commit bc59969

Please sign in to comment.