Keller Core Collection - ThorbenKuck/Keller GitHub Wiki

Disclaimer

The name of the wiki-page is missleading (KLICKBAIT!!!) and that i am sorry for. Currently, this package only contains one class. I am sorry and i will do repentance, i swear.

The MemoryCacheUnit

What is the MemoryCacheUnit?

This question is easy to explain. It is a way more fancy and way more difficult to use Queue. Well, it is not quit that simple.. Think about it this way:

You have 2 Queues. One represents the Memory, one represents the Cache.

Whenever you work on anything, you use the cache and once you are done, you copy everything from the memory to the cache queue again.

If you add any element, you add it to the memory, not to the cache, so that whatever class is working on the cache is not disrupted.

This is a MemoryCacheUnit.

Using a MemoryCacheUnit

To use such a collection, you would first instantiate it. You can to it by one of the following ways:

Collection<T> base = ...;

MemoryCacheUnit.queue();
MemoryCacheUnit.synchronizedQueue();
MemoryCacheUnit.queue(base);
MemoryCacheUnit.stack();
MemoryCacheUnit.synchronizedStack();
MemoryCacheUnit.stack(base);

This states, that there are 2 Implementation-types of the MemoryCacheUnit: The Queue and the Stack based MemoryCacheUnit. The Stack MemoryCacheUnit is realized using the Deque class.

Those 2 behave differently, because of the foundation those. The Stack one is a FiLo and the Queue one is a FiFo. But both are meant to be used as Iterator-Based classes. They have methods to add something to them, but not to remove something. You would use them in the following way (The Foo class is imaginary, bar returns the provided integer):

MemoryCacheUnit<Foo> unit = MemoryCacheUnit.queue();

unit.add(new Foo(1));
unit.add(new Foo(2));
unit.add(new Foo(3));

unit.resetCache();

for(Foo foo : unit) {
    System.out.println(foo.bar());
}

First off, you create and fill the MemoryCacheUnit. All of those Foos go directly into the Memory of the MemoryCacheUnit. Therefor, the second thing we have to do, is to reset the cache of the MemoryCacheUnit, which load all Foos stored within the Memory into the cache. The unit is now usable and the for-loop will iterate over the cache, popping the next, first element in it.

The output would be the following:

1
2
3

The same code, only with a stack base will have a completely different output. Let's have a look:

MemoryCacheUnit<Foo> unit = MemoryCacheUnit.stack();

unit.add(new Foo(1));
unit.add(new Foo(2));
unit.add(new Foo(3));

unit.resetCache();

for(Foo foo : unit) {
    System.out.println(foo.bar());
}

This will have the following output:

3
2
1

The reason is, that everything is the same as before, with the only difference, that not the first element will be popped from the cache, but the last.

⚠️ **GitHub.com Fallback** ⚠️