/mi-stack

Yet another stack

Primary LanguageJavaApache License 2.0Apache-2.0

Mi-Stack logo

License Apache 2.0 Java 11.0+ Maven central PayPal donation YooMoney donation

Changelog

1.0.3 (18-aug-2024)

  • fix and test improvements

1.0.2 (21-jul-2024)

  • added MiStackFlat to combine several stacks as a single chain

What is it?

Just abstract stack allows to mark its items by tags and get elements through either stream or iterator with possibility to filter items and stop iterator.

Implementations

Thread unsafe

  • MiStackArray, based on dynamically growing array, also growing can be disabled and in the case it works with internal fixed size array.
  • MiStackArrayList, based on java.util.ArrayList.
  • MiStackLinked, it uses own internal implementation of linked list.
  • MiStackLinkedList, based on java.util.LinkedList.

Thread safe

  • MiStackConcurrent, it uses as base java.util.concurrent.ConcurrentLinkedDeque.

Abstract ones

  • AbstractMiStackList, it allows build MiStack implementation around a java.util.List collection.
  • AbstractMiStackDeque, it allows build MiStack implementation around a java.util.Deque collection.

Use with Maven

Just add the snippet into the list of dependencies

<dependency>
    <groupId>com.igormaznitsa</groupId>
    <artifactId>mi-stack</artifactId>
    <version>1.0.3</version>
</dependency>

Example

The example below shows minimal use case with pushing several tagged items on the stack and iteration through them with predicated streams.

    var tagStar = MiStackTagImpl.tagsOf("star");
    var tagPlanet = MiStackTagImpl.tagsOf("planet");
    var tagPlanetoid = MiStackTagImpl.tagsOf("planetoid");
    var tagAsteroid = MiStackTagImpl.tagsOf("asteroid");
    var tagSatellite = MiStackTagImpl.tagsOf("satellite");

    try (var stack = new MiStackArrayList<>()) {
      stack.push(MiStackItemImpl.itemOf("Sun", tagStar));
      stack.push(MiStackItemImpl.itemOf("Mercury", tagPlanet));
      stack.push(MiStackItemImpl.itemOf("Venus", tagPlanet, tagSatellite));
      stack.push(MiStackItemImpl.itemOf("Earth", tagPlanet, tagSatellite));
      stack.push(MiStackItemImpl.itemOf("Moon", tagPlanetoid, tagSatellite));
      stack.push(MiStackItemImpl.itemOf("Mars", tagPlanet));
      stack.push(MiStackItemImpl.itemOf("Phobos", tagAsteroid, tagSatellite));
      stack.push(MiStackItemImpl.itemOf("Demos", tagAsteroid, tagSatellite));
      stack.push(MiStackItemImpl.itemOf("Jupiter", tagPlanet));
      stack.push(MiStackItemImpl.itemOf("Saturn", tagPlanet));
      stack.push(MiStackItemImpl.itemOf("Uranus", tagPlanet));
      stack.push(MiStackItemImpl.itemOf("Neptune", tagPlanet));
      stack.push(MiStackItemImpl.itemOf("Pluto", tagPlanetoid));

      assertArrayEquals(new Object[] {"Sun"}, stack.stream(MiStack.allTags(tagStar)).map(MiStackItem::getValue).toArray());
      assertArrayEquals(new Object[] {"Pluto", "Moon"},stack.stream(MiStack.allTags(tagPlanetoid)).map(MiStackItem::getValue).toArray());
      assertArrayEquals(new Object[] {"Neptune", "Uranus", "Saturn", "Jupiter", "Mars", "Earth", "Venus","Mercury"}, stack.stream(MiStack.allTags(tagPlanet)).map(MiStackItem::getValue).toArray());
      assertArrayEquals(new Object[] {"Pluto", "Neptune", "Uranus", "Saturn", "Jupiter", "Mars", "Moon", "Earth","Venus", "Mercury"}, stack.stream(MiStack.anyTag(tagPlanet, tagPlanetoid)).map(MiStackItem::getValue).toArray());
    }