patternjavaMinor
FixedStack of the Container hierarchy in Java
Viewed 0 times
thecontainerhierarchyjavafixedstack
Problem
Introduction/Disclaimer
This is a project for educational purposes. I'm not intending to replace the "award-winning" Collections Framework in
I'd like to see someone break my code. Preferably in a multi-threaded environment. (My code is not thread-safe)
Overview
The
```
package astrobleme.core.datastructures;
import java.util.Arrays;
import java.util.StringJoiner;
/**
* This is the root of the Container hierarchy. This contains methods
* that provide a common skeletal structure to all implementations. The
* modifying methods will have different names, depending on the type of
* data structure it is.
*
* @author Subhomoy Haldar
* @version 2017.02.15
*/
public abstract class Container {
/**
* Returns the number of elements currently in the Container. It is
* guaranteed to be a non-negative number.
*
* NOTE: If the number of elements exceeds
* {@link Long#MAX_VALUE Long#MAX_VALUE}, then it will return
* {@code Long#MAX_VALUE}.
*
* @return The number of elements in this Container.
*/
abstract long size();
/**
* Returns {@code true} if the number of elements in this Container is
* within the allowed maximum size for arrays, and hopefully, it might
* be able to create an array out of it.
*
* However, it depends on the amount of memory allocated by the VM and
* even smaller sizes may cause a {@link OutOfMemoryError}. It is advised
* to re-start the VM with different arguments to allow for allocation
* of
This is a project for educational purposes. I'm not intending to replace the "award-winning" Collections Framework in
java.util. I'm just looking for constructive criticism. For obvious reasons, I'm tagging it as reinventing-the-wheel.I'd like to see someone break my code. Preferably in a multi-threaded environment. (My code is not thread-safe)
Overview
The
Container class defines the minimal skeletal structure for all Data Structures that I implement. Depending on the requirement, I'm providing two implementations: Fixed capacity implementations and implementations using Linked Lists. For this question, I'm limiting the discussion to FixedStack and all the related classes.Container.java```
package astrobleme.core.datastructures;
import java.util.Arrays;
import java.util.StringJoiner;
/**
* This is the root of the Container hierarchy. This contains methods
* that provide a common skeletal structure to all implementations. The
* modifying methods will have different names, depending on the type of
* data structure it is.
*
* @author Subhomoy Haldar
* @version 2017.02.15
*/
public abstract class Container {
/**
* Returns the number of elements currently in the Container. It is
* guaranteed to be a non-negative number.
*
* NOTE: If the number of elements exceeds
* {@link Long#MAX_VALUE Long#MAX_VALUE}, then it will return
* {@code Long#MAX_VALUE}.
*
* @return The number of elements in this Container.
*/
abstract long size();
/**
* Returns {@code true} if the number of elements in this Container is
* within the allowed maximum size for arrays, and hopefully, it might
* be able to create an array out of it.
*
* However, it depends on the amount of memory allocated by the VM and
* even smaller sizes may cause a {@link OutOfMemoryError}. It is advised
* to re-start the VM with different arguments to allow for allocation
* of
Solution
2 crashing use cases:
Performance: clearing could be much faster when implemented in
new FixedStack(-1);
new FixedStack(Integer.MAX_VALUE);Performance: clearing could be much faster when implemented in
FixedStack:@Override
public void clear() {
top = -1;
}Code Snippets
new FixedStack(-1);
new FixedStack(Integer.MAX_VALUE);@Override
public void clear() {
top = -1;
}Context
StackExchange Code Review Q#156151, answer score: 2
Revisions (0)
No revisions yet.