diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/nbproject/project.properties diff --git a/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java b/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java index 4ca0bd6..efd07d8 100644 --- a/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java +++ b/src/main/java/ua/net/uid/utils/helpers/CommonHelper.java @@ -53,11 +53,14 @@ public static boolean isEmpty(T[] array) { return array == null || array.length == 0; } - + + @SafeVarargs public static Set setOf(T ... items) { - Set result = new HashSet<>(); - if (!isEmpty(items)) + if (!isEmpty(items)) { + Set result = new HashSet<>(items.length); Collections.addAll(result, items); - return result; + return result; + } + return new HashSet<>(); } } diff --git a/src/main/java/ua/net/uid/utils/helpers/StringHelper.java b/src/main/java/ua/net/uid/utils/helpers/StringHelper.java index c767bee..2b9e512 100644 --- a/src/main/java/ua/net/uid/utils/helpers/StringHelper.java +++ b/src/main/java/ua/net/uid/utils/helpers/StringHelper.java @@ -16,6 +16,7 @@ package ua.net.uid.utils.helpers; import java.io.IOException; +import java.util.Iterator; public class StringHelper { private static final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; @@ -160,7 +161,6 @@ return string == null ? null : string.trim(); } - @SuppressWarnings("SpellCheckingInspection") public static String ltrim(String string) { if (string == null) return null; int i = 0, length = string.length(); @@ -169,7 +169,6 @@ return i == 0 ? string : (i < length ? string.substring(i) : ""); } - @SuppressWarnings("SpellCheckingInspection") public static String rtrim(String string) { if (string == null) return null; int i = string.length(); @@ -184,6 +183,42 @@ return offset; } + public static void join(Appendable builder, CharSequence div, Iterator iterator) throws IOException { + join(builder, div, iterator, true, true); + } + + public static void join(Appendable builder, CharSequence div, Iterator iterator, boolean skipEmpty) throws IOException { + join(builder, div, iterator, skipEmpty, skipEmpty); + } + + public static void join(Appendable builder, CharSequence div, Iterator iterator, boolean skipEmpty, boolean skipNull) throws IOException { + boolean first = true; + while (iterator.hasNext()) { + Object item = iterator.next(); + if (!skipNull || item != null) { + String str = String.valueOf(item); + if (!(skipEmpty && CommonHelper.isEmpty(str))) { + if (first) first = false; else builder.append(div); + builder.append(item.toString()); + } + } + } + } + + public static CharSequence join(CharSequence div, Iterator iterator) { + return join(div, iterator, true, true); + } + + public static CharSequence join(CharSequence div, Iterator iterator, boolean skipEmpty) { + return join(div, iterator, skipEmpty, skipEmpty); + } + + public static CharSequence join(CharSequence div, Iterator iterator, boolean skipEmpty, boolean skipNull) { + StringBuilder builder = new StringBuilder(); + try { join(builder, div, iterator, skipEmpty, skipNull); } catch (IOException ignore) {} + return builder; + } + public static boolean isAscii(int chr) { return ((chr & 0xFFFFFF80) == 0); } diff --git a/src/main/java/ua/net/uid/utils/io/ExpiringCache.java b/src/main/java/ua/net/uid/utils/io/ExpiringCache.java index 6ad3a73..5030b7e 100644 --- a/src/main/java/ua/net/uid/utils/io/ExpiringCache.java +++ b/src/main/java/ua/net/uid/utils/io/ExpiringCache.java @@ -17,351 +17,125 @@ import java.util.Date; import java.util.Objects; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; import java.util.function.Function; public class ExpiringCache { //////////////////////////////////////////////////////////////////////////// - public static final int MAX_TABLE_SIZE = Integer.MAX_VALUE >>> 3; - public static final int INIT_SIZE = 64; - public static final float LOAD_FACTOR = 7F/3F; + private final Item EMPTY = new Item<>(null, null, 0); + private final ConcurrentHashMap> cache; + private final DelayQueue> queue = new DelayQueue<>(); //////////////////////////////////////////////////////////////////////////// - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - private final AtomicInteger count = new AtomicInteger(0); - private final AtomicInteger modified = new AtomicInteger(0); - private final float loadFactor; - private int nextResize; - private Chain[] table; - - public ExpiringCache() { - this(INIT_SIZE, LOAD_FACTOR); + cache = new ConcurrentHashMap<>(); } + public ExpiringCache(int initSize) { - this(initSize, LOAD_FACTOR); + cache = new ConcurrentHashMap<>(initSize); } - public ExpiringCache(float loadFactor) { - this(INIT_SIZE, loadFactor); - } - public ExpiringCache(int initSize, float loadFactor) { - this.loadFactor = loadFactor; - this.nextResize = (int)(loadFactor * initSize); - this.table = create(initSize); - } - - public int getSize() { return count.get(); } - - public int getTableSize() { return table.length; } - - int getModifiedCount() { return modified.get(); } + //////////////////////////////////////////////////////////////////////////// + public int getSize() { return cache.size(); } public V get(K key) { - return get(key, now()); - } - - public V get(K key, long now) { - return chain(key.hashCode()).get(key, now); + return cache.getOrDefault(key, EMPTY).value; } public V get(K key, Function callback, long ttl) { - return get(key, callback, ttl, now()); + return cache.computeIfAbsent(key, (k) -> { + Item item = new Item<>(key, callback.apply(key), now() + ttl); + queue.put(item); + return item; + }).value; } public V get(K key, Function callback, Date expiry) { - return get(key, callback, expiry, now()); + return cache.computeIfAbsent(key, (k) -> { + Item item = new Item<>(key, callback.apply(key), expiry.getTime()); + queue.put(item); + return item; + }).value; } - public V get(K key, Function callback, long ttl, long now) { - return chain(key.hashCode()).get(key, callback, now + ttl, now); - } - - public V get(K key, Function callback, Date expiry, long now) { - return chain(key.hashCode()).get(key, callback, expiry.getTime(), now); - } - public void set(K key, V value, long ttl) { - set(key, value, ttl, now()); + Item item = new Item<>(key, value, now() + ttl); + queue.put(item); + cache.put(item.key, item); } - + public void set(K key, V value, Date expiry) { - set(key, value, expiry, now()); + Item item = new Item<>(key, value, expiry.getTime()); + queue.put(item); + cache.put(item.key, item); } - public void set(K key, V value, long ttl, long now) { - chain(key.hashCode()).set(key, value, ttl + now, now); - } - - public void set(K key, V value, Date expiry, long now) { - chain(key.hashCode()).set(key, value, expiry.getTime(), now); - } - public void remove(K key) { - remove(key, now()); - } - - public void remove(K key, long now) { - chain(key.hashCode()).remove(key, now); - } - - public V extract(K key) { - return extract(key, now()); - } - - public V extract(K key, long now) { - return chain(key.hashCode()).extract(key, now); - } - - public void gc() { - lock.readLock().lock(); - try { - long now = now(); - for(Chain chain: table) - chain.gc(now); - modified.set(0); - } finally { - lock.readLock().unlock(); - } - } - /* - //TODO public void pack() - public void pack() { - lock.writeLock().lock(); - try { - throw new UnsupportedOperationException("TODO: Not supported yet."); - } finally { - lock.writeLock().unlock(); - } - } - */ - public void empty() { - lock.readLock().lock(); - try { - for(Chain chain: table) - chain.next = null; - modified.set(0); - } finally { - lock.readLock().unlock(); - } + queue.remove(cache.remove(key)); } - public void clear(int initSize) { - lock.writeLock().lock(); - try { - table = create(initSize); - modified.set(0); - } finally { - lock.writeLock().unlock(); + public void gc() { + Item item; + while((item = queue.poll()) != null) + cache.remove(item.key, item); + } + + public void runGC() { + while (!Thread.currentThread().isInterrupted()) { + try { + Item item = queue.take(); + cache.remove(item.key, item); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } } public void clear() { - clear(INIT_SIZE); + cache.clear(); } //////////////////////////////////////////////////////////////////////////// private static long now() { return System.currentTimeMillis(); } - - private Chain[] create(int size) { - Chain[] result = (Chain[]) new Chain[size]; - for(int i = 0; i < size; ++i) - result[i] = new Chain<>(this); - return result; - } - - private static int index(int hash, int length) { - return (hash & MAX_TABLE_SIZE) % length; - } - - private Chain chain(int hash) { - lock.readLock().lock(); - try { - return table[index(hash, table.length)]; - } finally { - lock.readLock().unlock(); - } - } - - private void resize(int delta) { - int newSize = Math.min(MAX_TABLE_SIZE, count.addAndGet(delta)); - if (newSize > nextResize) { - lock.writeLock().lock(); - try { - Chain[] result = create(newSize); - int newCount = 0; - long now = now(); - for(Chain src : table) { - Entry old = src.next; - while (old != null && old.expiry > now) { - result[index(old.key.hashCode(), newSize)].add(old.key, old.value, old.expiry); - ++newCount; - old = old.next; - } - src.next = null; - } - table = result; - count.set(newCount); - modified.set(0); - nextResize = Math.min(MAX_TABLE_SIZE, (int)(loadFactor * newSize)); - } finally { - lock.writeLock().unlock(); - } - } else { - if (modified.incrementAndGet() > nextResize) - gc(); - } - } + private static int compare(long lv, long rv) { return lv < rv ? -1 : (lv > rv ? 1 : 0); } //////////////////////////////////////////////////////////////////////////// - private static abstract class Item { - Entry next; - Item(Entry next) { this.next = next; } - } - //////////////////////////////////////////////////////////////////////////// - private static final class Entry extends Item { + private static final class Item implements Delayed { final K key; final V value; final long expiry; - Entry(K key, V value, long expiry, Entry next) { - super(next); - this.expiry = expiry; + + public Item(K key, V value, long expiry) { this.key = key; this.value = value; + this.expiry = expiry; } - } - //////////////////////////////////////////////////////////////////////////// - private static final class Chain extends Item { - final ExpiringCache cache; - int count = 0; - - Chain(ExpiringCache cache) { - super(null); - this.cache = cache; + + @Override + public long getDelay(TimeUnit unit) { + return unit.convert(expiry - now(), TimeUnit.MILLISECONDS); + } + + @Override + @SuppressWarnings("unchecked") + public int compareTo(Delayed other) { + return compare(expiry, ((Item)other).expiry); + } + + @Override + public int hashCode() { + return 23 * (23 * (23 * 7 + Objects.hashCode(key)) + Objects.hashCode(value)) + (int) (expiry ^ (expiry >>> 32)); + } + + @Override + public boolean equals(Object other) { + return other instanceof Item && equals((Item) other); } - private void count(int value) { - if (count != value) { - int old = count; - count = value; - cache.resize(value - old); - } - } - - private boolean checkNext(Item current, long now) { - if (current.next != null) { - if (current.next.expiry <= now) { - current.next = null; - } else { - return true; - } - } - return false; - } - - void add(K key, V value, long expiry) { - Item item = this; - while (item.next != null && item.next.expiry > expiry) { - item = item.next; - } - item.next = new Entry<>(key, value, expiry, item.next); - ++count; - } - - synchronized V get(K key, long now) { - Item item = this; - int cnt = 0; - V result = null; - while (checkNext(item, now)) { - if (Objects.equals(key, item.next.key)) - result = item.next.value; - ++cnt; - item = item.next; - } - count(cnt); - return result; - } - - synchronized V get(K key, Function callback, long expiry, long now) { - Item item = this, last = this; - int cnt = 0; - while (checkNext(item, now)) { - ++cnt; - if (Objects.equals(key, item.next.key)) { - V value = item.next.value; - while (checkNext(item = item.next, now)) ++cnt; - count(cnt); - return value; - } - if (item.next.expiry > expiry) - last = item.next; - item = item.next; - } - V value = callback.apply(key); - last.next = new Entry<>(key, value, expiry, last.next); - count(cnt + 1); - return value; - } - - synchronized void set(K key, V value, long expiry, long now) { - Item item = this, last = this; - int cnt = 0; - while (checkNext(item, now)) { - if (Objects.equals(key, item.next.key)) { - item.next = item.next.next; - while (checkNext(item, now)) { - ++cnt; - if (item.next.expiry > expiry) - last = item.next; - item = item.next; - } - break; - } - ++cnt; - if (item.next.expiry > expiry) - last = item.next; - item = item.next; - } - last.next = new Entry<>(key, value, expiry, last.next); - count(cnt + 1); - } - - synchronized void remove(K key, long now) { - Item item = this; - int cnt = 0; - while (checkNext(item, now)) { - if (Objects.equals(key, item.next.key)) { - item.next = item.next.next; - } else { - ++cnt; - item = item.next; - } - } - count(cnt); - } - - synchronized V extract(K key, long now) { - Item item = this; - int cnt = 0; - V result = null; - while (checkNext(item, now)) { - if (Objects.equals(key, item.next.key)) { - result = item.next.value; - item.next = item.next.next; - } else { - ++cnt; - item = item.next; - } - } - count(cnt); - return result; - } - - synchronized void gc(long now) { - Item item = this; - int cnt = 0; - while (checkNext(item, now)) { - ++cnt; - item = item.next; - } - count(cnt); + public boolean equals(Item other) { + return other != null + && Objects.equals(key, other.key) + && expiry == other.expiry + && Objects.equals(value, other.value); } } //////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/ua/net/uid/utils/io/FifoCache.java b/src/main/java/ua/net/uid/utils/io/FifoCache.java new file mode 100644 index 0000000..3f3f074 --- /dev/null +++ b/src/main/java/ua/net/uid/utils/io/FifoCache.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019 nightfall. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ua.net.uid.utils.io; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.function.Function; + +/** + * + * @author nightfall + */ +public class FifoCache { + private final ConcurrentHashMap cache = new ConcurrentHashMap<>(); + private final ConcurrentLinkedDeque fifo = new ConcurrentLinkedDeque<>(); + private final int limit; + + public FifoCache(int limit) { + this.limit = limit; + } + + public V get(K key, Function callback) { + return cache.computeIfAbsent(key, (k) -> { + try { + fifo.addLast(k); + return callback.apply(k); + } finally { + if (fifo.size() > limit) + cache.remove(fifo.removeFirst()); + } + }); + } +} diff --git a/src/test/java/ua/net/uid/utils/concurrent/ReadyFutureTest.java b/src/test/java/ua/net/uid/utils/concurrent/ReadyFutureTest.java index c1e15cc..67975cf 100644 --- a/src/test/java/ua/net/uid/utils/concurrent/ReadyFutureTest.java +++ b/src/test/java/ua/net/uid/utils/concurrent/ReadyFutureTest.java @@ -10,32 +10,32 @@ @Test void testCancel() { - ReadyFuture instance = new ReadyFuture(null); + ReadyFuture instance = new ReadyFuture<>(null); assertFalse(instance.cancel(true)); assertFalse(instance.cancel(false)); } @Test void testIsCancelled() { - ReadyFuture instance = new ReadyFuture(null); + ReadyFuture instance = new ReadyFuture<>(null); assertFalse(instance.isCancelled()); } @Test void testIsDone() { - ReadyFuture instance = new ReadyFuture(null); + ReadyFuture instance = new ReadyFuture<>(null); assertTrue(instance.isDone()); } @Test void testGet() { - ReadyFuture instance = new ReadyFuture(3); + ReadyFuture instance = new ReadyFuture<>(3); assertEquals(3, instance.get()); } @Test void testGet_long_TimeUnit() { - ReadyFuture instance = new ReadyFuture(4.4); + ReadyFuture instance = new ReadyFuture<>(4.4); assertEquals(4.4, instance.get(1, TimeUnit.NANOSECONDS)); } } diff --git a/src/test/java/ua/net/uid/utils/helpers/CommonHelperTest.java b/src/test/java/ua/net/uid/utils/helpers/CommonHelperTest.java index a2e8b33..44db5fd 100644 --- a/src/test/java/ua/net/uid/utils/helpers/CommonHelperTest.java +++ b/src/test/java/ua/net/uid/utils/helpers/CommonHelperTest.java @@ -45,9 +45,9 @@ @Test void testIsEmptyCollection() { - Collection src = null; + Collection src = null; assertTrue(CommonHelper.isEmpty(src)); - src = new ArrayList(); + src = new ArrayList<>(); assertTrue(CommonHelper.isEmpty(src)); src.add("1"); assertFalse(CommonHelper.isEmpty(src)); @@ -55,9 +55,9 @@ @Test void testIsEmptyMap() { - Map src = null; + Map src = null; assertTrue(CommonHelper.isEmpty(src)); - src = new HashMap(); + src = new HashMap<>(); assertTrue(CommonHelper.isEmpty(src)); src.put(1, "1"); assertFalse(CommonHelper.isEmpty(src)); diff --git a/src/test/java/ua/net/uid/utils/io/ExpiringCacheTest.java b/src/test/java/ua/net/uid/utils/io/ExpiringCacheTest.java index afb3214..c76a221 100644 --- a/src/test/java/ua/net/uid/utils/io/ExpiringCacheTest.java +++ b/src/test/java/ua/net/uid/utils/io/ExpiringCacheTest.java @@ -9,70 +9,7 @@ @Test public void testSavingOfItems() throws Exception { - ExpiringCache instance = new ExpiringCache<>(2, 4f/3f); - - assertEquals(Integer.valueOf(1), instance.get(1, (key) -> key * key, 1500)); - assertEquals(Integer.valueOf(4), instance.get(2, (key) -> key * key, 2000)); - assertEquals(2, instance.getSize()); - assertEquals(2, instance.getTableSize()); - assertEquals(2, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(4), instance.get(2, (key) -> key, 2000)); - assertEquals(2, instance.getSize()); - assertEquals(2, instance.getTableSize()); - assertEquals(2, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(9), instance.get(3, (key) -> key * key, 2000)); - assertEquals(3, instance.getSize()); - assertEquals(3, instance.getTableSize()); - assertEquals(0, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(16), instance.get(4, (key) -> key * key, 2000)); - assertEquals(4, instance.getSize()); - assertEquals(3, instance.getTableSize()); - assertEquals(1, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(25), instance.get(-5, (key) -> key * key, 1000)); - assertEquals(5, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(0, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(25), instance.get(-5)); - assertEquals(5, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(0, instance.getModifiedCount()); - - assertNull(instance.get(-5, System.currentTimeMillis() + 1000)); - assertEquals(4, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(1, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(25), instance.get(5, (key) -> key * key, 1)); - assertEquals(5, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(2, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(2), instance.get(1, (key) -> key + 1, 1, System.currentTimeMillis() + 1500)); - assertEquals(5, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(2, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(9), instance.get(3)); - assertEquals(Integer.valueOf(4), instance.get(2)); - instance.remove(3); - assertNull(instance.get(3)); - assertEquals(Integer.valueOf(4), instance.get(2)); - - assertEquals(4, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(3, instance.getModifiedCount()); - - assertEquals(Integer.valueOf(3), instance.get(3, (key) -> key, 2000)); - assertEquals(5, instance.getSize()); - assertEquals(5, instance.getTableSize()); - assertEquals(4, instance.getModifiedCount()); - - + //TODO } } \ No newline at end of file diff --git a/src/test/java/ua/net/uid/utils/io/FifoCacheTest.java b/src/test/java/ua/net/uid/utils/io/FifoCacheTest.java new file mode 100644 index 0000000..8f2d5b8 --- /dev/null +++ b/src/test/java/ua/net/uid/utils/io/FifoCacheTest.java @@ -0,0 +1,49 @@ +package ua.net.uid.utils.io; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +/** + * + * @author nightfall + */ +public class FifoCacheTest { + @Test + public void testGet() { + FifoCache cache = new FifoCache<>(5); + for (int i = 0; i < 5; ++i) { // new values + assertEquals(Integer.valueOf(i * i + 1), cache.get(i, (t) -> t * t + 1)); + } + for (int i = 0; i < 5; ++i) { // exists values + assertEquals(Integer.valueOf(i * i + 1), cache.get(i, (t) -> 0)); + } + assertEquals(Integer.valueOf(6 * 6), cache.get(6, (t) -> t * t)); + assertEquals(Integer.valueOf(0), cache.get(0, (t) -> 0)); + } + /* + @Test + public void testTmp() { + TimeZone zone = TimeZone.getTimeZone("Europe/Kiev"); + Calendar calendar = Calendar.getInstance(zone); + calendar.setTimeInMillis(1566223311001L); + + assertEquals(19, calendar.get(Calendar.DAY_OF_MONTH)); + assertEquals(17, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(1, calendar.get(Calendar.MINUTE)); + assertEquals(51, calendar.get(Calendar.SECOND)); + assertEquals(1, calendar.get(Calendar.MILLISECOND)); + + calendar.set(Calendar.MILLISECOND, 0); + assertEquals(0, calendar.get(Calendar.MILLISECOND)); + assertEquals(1566223311000L, calendar.getTimeInMillis()); + + calendar.set(Calendar.SECOND, 0); + assertEquals(0, calendar.get(Calendar.SECOND)); + assertEquals(1566223260000L, calendar.getTimeInMillis()); + + calendar.set(Calendar.MINUTE, 0); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(1566223200000L, calendar.getTimeInMillis()); + } + */ +} diff --git a/src/test/java/ua/net/uid/utils/io/LocalCacheTest.java b/src/test/java/ua/net/uid/utils/io/LocalCacheTest.java index 2cfef8b..39453e9 100644 --- a/src/test/java/ua/net/uid/utils/io/LocalCacheTest.java +++ b/src/test/java/ua/net/uid/utils/io/LocalCacheTest.java @@ -7,29 +7,29 @@ import static org.junit.jupiter.api.Assertions.*; -@SuppressWarnings("unchecked") +//@SuppressWarnings("unchecked") class LocalCacheTest { @Test void testFuture_GenericType() throws InterruptedException, ExecutionException { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.set("test", 123); - Future result = instance.future("test"); + Future result = instance.future("test"); assertNotNull(result); assertEquals(123, result.get()); } @Test void testFuture_GenericType_Callable() throws InterruptedException, ExecutionException { - LocalCache instance = new LocalCache(); - Future result = instance.future("test", () -> 456); + LocalCache instance = new LocalCache<>(); + Future result = instance.future("test", () -> 456); assertNotNull(result); assertEquals(456, result.get()); } @Test void testGet_GenericType() { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.set("test", 789); Object result = instance.get("test"); assertNotNull(result); @@ -38,7 +38,7 @@ @Test void testGet_GenericType_Callable() { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); Object result = instance.get("test", () -> 65535); assertNotNull(result); assertEquals(65535, result); @@ -46,27 +46,27 @@ @Test void testSet_GenericType_GenericType() throws InterruptedException, ExecutionException { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.set("test", 2345); - Future result = instance.future("test"); + Future result = instance.future("test"); assertNotNull(result); assertEquals(2345, result.get()); } @Test void testSet_GenericType_Callable() throws InterruptedException, ExecutionException { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.put("testSet_GenericType_Callable", () -> "testSet_GenericType_Callable"); - Future result = instance.future("testSet_GenericType_Callable"); + Future result = instance.future("testSet_GenericType_Callable"); assertNotNull(result); assertEquals("testSet_GenericType_Callable", result.get()); } @Test void testExtractFuture() throws InterruptedException, ExecutionException { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.put("testExtractFuture", () -> "testExtractFuture"); - Future result = instance.extractFuture("testExtractFuture"); + Future result = instance.extractFuture("testExtractFuture"); assertNotNull(result); assertNull(instance.future("testExtractFuture")); assertEquals("testExtractFuture", result.get()); @@ -74,7 +74,7 @@ @Test void testExtract() { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.put("testExtract", () -> "testExtract"); Object result = instance.extract("testExtract"); assertNotNull(result); @@ -84,7 +84,7 @@ @Test void testRemove() { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.put("testRemove", () -> "testRemove"); instance.remove("testExtract"); assertNull(instance.future("testExtract")); @@ -92,7 +92,7 @@ @Test void testClear() { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.put("testClear", () -> "testClear"); instance.clear(); assertNull(instance.future("testClear")); @@ -100,7 +100,7 @@ @Test void testCount() { - LocalCache instance = new LocalCache(); + LocalCache instance = new LocalCache<>(); instance.set(0, 0); instance.set(1, 1); instance.set(2, 2); diff --git a/src/test/java/ua/net/uid/utils/io/SimpleWriterTest.java b/src/test/java/ua/net/uid/utils/io/SimpleWriterTest.java index cd3bb91..cb6d57a 100644 --- a/src/test/java/ua/net/uid/utils/io/SimpleWriterTest.java +++ b/src/test/java/ua/net/uid/utils/io/SimpleWriterTest.java @@ -11,124 +11,139 @@ @Test void testWriteChar() { - SimpleWriter instance = new SimpleWriter(); - instance.write(32); - assertEquals(" ", instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(32); + assertEquals(" ", instance.toString()); + } } @Test void testWriteCharsFromOffsetWithLength() { char[] buf = "aaa0123456789bbb".toCharArray(); - SimpleWriter instance = new SimpleWriter(); - instance.write(buf, 3, 10); - assertEquals("0123456789", instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(buf, 3, 10); + assertEquals("0123456789", instance.toString()); + } } @Test void testWriteString() { String str = "prototype"; - SimpleWriter instance = new SimpleWriter(); - instance.write(str); - assertEquals(str, instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(str); + assertEquals(str, instance.toString()); + } } @Test void testWriteStringFromOffsetWithLength() { String buf = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - instance.write(buf, 3, 10); - assertEquals("0123456789", instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(buf, 3, 10); + assertEquals("0123456789", instance.toString()); + } } @Test void testAppendCharSequence() { - SimpleWriter instance = new SimpleWriter(); - Writer result = instance.append("test"); - assertEquals(result, instance); - assertEquals("test", result.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + Writer result = instance.append("test"); + assertEquals(result, instance); + assertEquals("test", result.toString()); + } } @Test void testAppendCharSequenceFromOffsetWithLength() { CharSequence data = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - Writer result = instance.append(data, 3, 10); - assertEquals(result, instance); - assertEquals("0123456", result.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + Writer result = instance.append(data, 3, 10); + assertEquals(result, instance); + assertEquals("0123456", result.toString()); + } } @Test void testAppendChar() { - SimpleWriter instance = new SimpleWriter(); - Writer result = instance.append(' '); - assertEquals(result, instance); - assertEquals(" ", instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + Writer result = instance.append(' '); + assertEquals(result, instance); + assertEquals(" ", instance.toString()); + } } @Test void testFlush() { - SimpleWriter instance = new SimpleWriter(); - instance.write("The test case is a prototype."); - instance.flush(); - assertEquals("The test case is a prototype.", instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write("The test case is a prototype."); + instance.flush(); + assertEquals("The test case is a prototype.", instance.toString()); + } } @Test void testClose() { - SimpleWriter instance = new SimpleWriter(); - instance.write("The test case is a prototype."); - instance.close(); - assertEquals("The test case is a prototype.", instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write("The test case is a prototype."); + instance.close(); + assertEquals("The test case is a prototype.", instance.toString()); + } } @Test void testLength() { String data = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - instance.write(data); - assertEquals(data.length(), instance.length()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(data); + assertEquals(data.length(), instance.length()); + } } @Test void testCharAt() { String data = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - instance.write(data); - assertEquals(data.charAt(3), instance.charAt(3)); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(data); + assertEquals(data.charAt(3), instance.charAt(3)); + } } @Test void testSubSequence() { String data = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - instance.write(data); - assertEquals("0123456", instance.subSequence(3, 10)); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(data); + assertEquals("0123456", instance.subSequence(3, 10)); + } } @Test void testToString() { String data = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - instance.write(data); - assertEquals(data, instance.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(data); + assertEquals(data, instance.toString()); + } } @Test void testGetBuilder() { String data = "aaa0123456789bbb"; - SimpleWriter instance = new SimpleWriter(); - instance.write(data); - StringBuilder result = instance.getBuilder(); - assertEquals(data, result.toString()); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(data); + StringBuilder result = instance.getBuilder(); + assertEquals(data, result.toString()); + } } @Test void testToCharArray() { char[] data = "aaa0123456789bbb".toCharArray(); - SimpleWriter instance = new SimpleWriter(); - instance.write(data); - char[] result = instance.toCharArray(); - assertArrayEquals(data, result); + try (SimpleWriter instance = new SimpleWriter()) { + instance.write(data); + char[] result = instance.toCharArray(); + assertArrayEquals(data, result); + } } }