4
4
import java .util .NoSuchElementException ;
5
5
6
6
/**
7
- * A collection that allows adding and iterating over elements but does not support element removal.
7
+ * A generic collection that allows adding and iterating over elements but does not support
8
+ * element removal. This class implements a simple bag data structure, which can hold duplicate
9
+ * elements and provides operations to check for membership and the size of the collection.
10
+ *
11
+ * <p>Bag is not thread-safe and should not be accessed by multiple threads concurrently.
8
12
*
9
13
* @param <E> the type of elements in this bag
10
14
*/
11
15
public class Bag <E > implements Iterable <E > {
12
16
13
- private Node <E > firstElement ; // First element in the bag
14
- private int size ; // Number of elements in the bag
17
+ private Node <E > firstElement ; // Reference to the first element in the bag
18
+ private int size ; // Count of elements in the bag
15
19
16
20
// Node class representing each element in the bag
17
21
private static final class Node <E > {
@@ -21,6 +25,7 @@ private static final class Node<E> {
21
25
22
26
/**
23
27
* Constructs an empty bag.
28
+ * <p>This initializes the bag with zero elements.
24
29
*/
25
30
public Bag () {
26
31
firstElement = null ;
@@ -30,7 +35,7 @@ public Bag() {
30
35
/**
31
36
* Checks if the bag is empty.
32
37
*
33
- * @return true if the bag is empty, false otherwise
38
+ * @return {@code true} if the bag contains no elements; {@code false} otherwise
34
39
*/
35
40
public boolean isEmpty () {
36
41
return size == 0 ;
@@ -39,7 +44,7 @@ public boolean isEmpty() {
39
44
/**
40
45
* Returns the number of elements in the bag.
41
46
*
42
- * @return the number of elements
47
+ * @return the number of elements currently in the bag
43
48
*/
44
49
public int size () {
45
50
return size ;
@@ -48,7 +53,10 @@ public int size() {
48
53
/**
49
54
* Adds an element to the bag.
50
55
*
51
- * @param element the element to add
56
+ * <p>This method adds the specified element to the bag. Duplicates are allowed, and the
57
+ * bag will maintain the order in which elements are added.
58
+ *
59
+ * @param element the element to add; must not be {@code null}
52
60
*/
53
61
public void add (E element ) {
54
62
Node <E > newNode = new Node <>();
@@ -61,8 +69,10 @@ public void add(E element) {
61
69
/**
62
70
* Checks if the bag contains a specific element.
63
71
*
64
- * @param element the element to check for
65
- * @return true if the bag contains the element, false otherwise
72
+ * <p>This method uses the {@code equals} method of the element to determine membership.
73
+ *
74
+ * @param element the element to check for; must not be {@code null}
75
+ * @return {@code true} if the bag contains the specified element; {@code false} otherwise
66
76
*/
67
77
public boolean contains (E element ) {
68
78
for (E value : this ) {
@@ -76,6 +86,8 @@ public boolean contains(E element) {
76
86
/**
77
87
* Returns an iterator over the elements in this bag.
78
88
*
89
+ * <p>The iterator provides a way to traverse the elements in the order they were added.
90
+ *
79
91
* @return an iterator that iterates over the elements in the bag
80
92
*/
81
93
@ Override
@@ -88,19 +100,35 @@ private static class ListIterator<E> implements Iterator<E> {
88
100
89
101
private Node <E > currentElement ;
90
102
103
+ /**
104
+ * Constructs a ListIterator starting from the given first element.
105
+ *
106
+ * @param firstElement the first element of the bag to iterate over
107
+ */
91
108
ListIterator (Node <E > firstElement ) {
92
109
this .currentElement = firstElement ;
93
110
}
94
111
112
+ /**
113
+ * Checks if there are more elements to iterate over.
114
+ *
115
+ * @return {@code true} if there are more elements; {@code false} otherwise
116
+ */
95
117
@ Override
96
118
public boolean hasNext () {
97
119
return currentElement != null ;
98
120
}
99
121
122
+ /**
123
+ * Returns the next element in the iteration.
124
+ *
125
+ * @return the next element in the bag
126
+ * @throws NoSuchElementException if there are no more elements to return
127
+ */
100
128
@ Override
101
129
public E next () {
102
130
if (!hasNext ()) {
103
- throw new NoSuchElementException ();
131
+ throw new NoSuchElementException ("No more elements in the bag." );
104
132
}
105
133
E element = currentElement .content ;
106
134
currentElement = currentElement .nextElement ;
0 commit comments