Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 22, 2022
2 parents b9f8247 + 863829f commit 45196d0
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 deletions.
73 changes: 73 additions & 0 deletions src/main/java/com/yegor256/tojos/CachedTojos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.yegor256.tojos;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* The wrapper which caches underlying tojos.
* Modify operation clears the cache.
* This class is NOT thread-safe.
* @since 1.0
*/
public final class CachedTojos implements Tojos {

/**
* Underlying tojos.
*/
private final Tojos wrapped;

/**
* Cache for tojos.
*/
private final List<Tojo> cache;

/**
* Ctor.
* @param wrapped Tojos which need to be cached
*/
public CachedTojos(final Tojos wrapped) {
this.wrapped = wrapped;
this.cache = new ArrayList<>(0);
}

@Override
public Tojo add(final String name) {
this.cache.clear();
return this.wrapped.add(name);
}

@Override
public List<Tojo> select(final Predicate<Tojo> filter) {
if (this.cache.isEmpty()) {
this.cache.addAll(this.wrapped.select(x -> true));
}
return this.cache.stream()
.filter(filter)
.collect(Collectors.toList());
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/yegor256/tojos/MonoTojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* All tojos in a {@link Mono}.
Expand Down Expand Up @@ -75,12 +75,12 @@ public Tojo add(final String name) {
}

@Override
public List<Tojo> select(final Function<Tojo, Boolean> filter) {
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));
if (filter.apply(tojo)) {
if (filter.test(tojo)) {
tojos.add(tojo);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/yegor256/tojos/SmartTojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.yegor256.tojos;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* All file-objects.
Expand Down Expand Up @@ -80,7 +80,7 @@ public Tojo add(final String name) {
}

@Override
public List<Tojo> select(final Function<Tojo, Boolean> filter) {
public List<Tojo> select(final Predicate<Tojo> filter) {
return this.origin.select(filter);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/yegor256/tojos/Tojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package com.yegor256.tojos;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Text Object Java Object (TOJO) in a storage.
Expand Down Expand Up @@ -61,6 +61,6 @@ public interface Tojos {
* @param filter The filter
* @return Collection of them
*/
List<Tojo> select(Function<Tojo, Boolean> filter);
List<Tojo> select(Predicate<Tojo> filter);

}
70 changes: 70 additions & 0 deletions src/test/java/com/yegor256/tojos/CachedTojosTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.yegor256.tojos;

import java.nio.file.Path;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Test for cached tojos. Not thread-safe.
* @since 1.0
* @checkstyle MagicNumberCheck (70 lines)
*/
class CachedTojosTest {

@Test
void testAddClearsCache(@TempDir final Path temp) {
final Tojos tojos = new MonoTojos(new Csv(temp.resolve("my-tojos-1.csv")));
final String[] keys = new String[] {"k10", "k20"};
tojos.add("A0").set(keys[0], "v10").set(keys[1], "vv10");
tojos.add("B0").set(keys[0], "v20").set(keys[1], "vv20");
tojos.add("C0").set(keys[0], "v30").set(keys[1], "vv30");
final Tojos cached = new CachedTojos(tojos);
cached.select(x -> true);
cached.add("D0").set(keys[0], "v40").set(keys[1], "vv40");
MatcherAssert.assertThat(
cached.select(x -> true).size(),
Matchers.equalTo(4)
);
}

@Test
void testSelectFromCached(@TempDir final Path temp) {
final Tojos tojos = new MonoTojos(new Csv(temp.resolve("my-tojos-2.csv")));
final String[] keys = new String[] {"k11", "k21"};
tojos.add("A1").set(keys[0], "v11").set(keys[1], "vv11");
tojos.add("B1").set(keys[0], "v21").set(keys[1], "vv21");
tojos.add("C1").set(keys[0], "v31").set(keys[1], "vv31");
final Tojos cached = new CachedTojos(tojos);
cached.select(x -> true);
tojos.add("D1").set(keys[0], "v41").set(keys[1], "vv41");
MatcherAssert.assertThat(
cached.select(x -> true).size(),
Matchers.equalTo(3)
);
}
}

0 comments on commit 45196d0

Please sign in to comment.